简体   繁体   English

如何将特定的ListTile变量传递给onTap的新页面路由?

[英]How to pass specific ListTile variables to new page route onTap?

My code builds a ListView of ListTiles, with each ListTile getting data from a cloud firestore database. 我的代码构建了一个ListTiles的ListView,每个ListTile从云Firestore数据库获取数据。

When I activate the onTap of a ListTile I would like to route to a new page and pass the specific data for that tile to the new page. 当我激活ListTile的onTap时,我想路由到新页面并将该磁贴的特定数据传递到新页面。

The page which I am building the list view on I have passed variables to without problems, however I cannot get the same method to work on this page. 我在其上构建列表视图的页面已将变量传递至,没有问题,但是无法在该页面上使用相同的方法。

I am fairly new to flutter and dart. 我对扑打和飞镖还挺陌生的。 My first assumption is that maybe it requires a stateful widget in order to accomplish this? 我的第一个假设是,可能需要有状态的小部件才能完成此任务? However I am not exactly sure how to implement this. 但是我不确定如何实现这一点。

import 'package:flutter/material.dart';

import 'package:menu/screens/menu/detail.dart';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';

class Menu extends StatelessWidget {
    final String barcode;

  // receive data from the FirstScreen as a parameter
  Menu({Key key, @required this.barcode}) : super(key: key);

  Widget _buildListItem(BuildContext context, DocumentSnapshot document) {
    return ListTile(
      leading: Icon(Icons.fastfood),
      title: Text(document['item'] ?? '*name*'),
      subtitle: Text( document['description'] ?? '*description*'),
      isThreeLine: true,
      onTap: (){
        Navigator.push(
            context,
            MaterialPageRoute(
            builder: (context) => Detail(),
        ),);
        },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Menu'),
      ),
      body: StreamBuilder(
          stream: Firestore.instance.collection(barcode).snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) return const Text('loading...');
            return ListView.builder(
              itemExtent: 80.0,
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) =>
                  _buildListItem(context, snapshot.data.documents[index]),
            );
          }),
    );
  }
}

Pass document as parameter to Detail document作为参数传递给Detail

    Navigator.push(
        context,
        MaterialPageRoute(
        builder: (context) => Detail(document), // <-- document instance
    ),);

Detail widget of course need to take document as parameter: 当然Detail部件需要以document为参数:

class Detail extends ... {
  Detail(this.document);
  final DocumentSnapshot document;
}

as described in https://flutter.io/docs/cookbook/navigation/passing-data https://flutter.io/docs/cookbook/navigation/passing-data中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 条件语句不适用于 ListTile onTap Flutter - Conditional statement not working on ListTile onTap Flutter 如何使用angularjs和firebase将用户路由到新页面 - How to route the user to a new page with angularjs and firebase 如何添加 ontap 功能? (我想导航到另一个页面) - How to add a ontap funtion? (I want to navigate to another page) 选择特定奖励后,如何将与特定奖励关联的项目 ID 传递到新页面 - How do I pass the item ID associated with a particular reward to a new page once the specific reward has been selected Flutter - 我如何查询相同的文档 ID - 在下一页 - 作为先前单击的 ListTile - Flutter - How can i query the same document id - on the next page - as a ListTile that was previously clicked 如何在 flutter 列表磁贴中的 ontap 操作后访问一个特定文档(在 Firestore 中)以显示 - how to access one specific document (in firestore) to display after ontap operation in flutter list tile 如何将 props 传递给 React Router 路由 - How to pass props to a React Router route 如何通过特定 IP 路由 API 请求? - How to route API Requests through a specific IP? 如何将数据传递给新类 - How to pass data to a new class Flutter/Firestore:如何在 ListTile 中显示来自 doc 的数据? - Flutter/Firestore : How do I display data from doc in a ListTile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM