0

我正在尝试用颤振创建我的第一个应用程序,它是一个在列表中显示项目的基本应用程序,您可以点击它们来获取信息。我已经创建了列表和联系人页面,我可以从一个页面导航到另一个页面,但我现在卡在如何根据我在列表中选择的联系人显示信息,所以我不需要写每个联系人一堂课。有没有办法做到这一点?

import 'package:flutter/material.dart';
import 'package:whatsapp_casimiro/contacts/contact_page.dart';
import 'package:whatsapp_casimiro/listas/lanchonetes.dart';

class LanchonetesScreen extends StatelessWidget {
final List<Lanchonetes> _allLanchonetes = 
Lanchonetes.allLanchonetes();

@override
Widget build(BuildContext context) {
return Scaffold(body: getLanchonetesPageBody(context));
}

getLanchonetesPageBody(BuildContext context) {
return ListView.builder(
  itemCount: _allLanchonetes.length,
  itemBuilder: _getItemUI,
);
}

Widget _getItemUI(BuildContext context, index) {
  return Column(children: <Widget>[
    Padding(
     padding: EdgeInsets.only(right: 20.0),
      child: Divider(
      color: Colors.black,
      indent: 80.0,
      height: 8.0,
    ),
  ),
  ListTile(
    contentPadding: EdgeInsets.fromLTRB(15.0, 10.0, 10.0, 10.0),
    leading: Container(
      width: 55.0,
      height: 60.0,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        image: DecorationImage(
            fit: BoxFit.cover,
            image: AssetImage(
            "images/" + _allLanchonetes[index].img)),
      ),
    ),
    title: Text(
      _allLanchonetes[index].name,
      style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
    ),
    onTap: () {
      _onLanchonetesButtonPressed(context);
    },
  )
  ]);
  }
}

void _onLanchonetesButtonPressed(BuildContext context) {
  Navigator.push(
  context, MaterialPageRoute(builder: (context) => ContactPage()));
}






class Lanchonetes {


  final String name;
  final String img;




  Lanchonetes({this.name, this.img});


  static List<Lanchonetes> allLanchonetes() {

   var listOfLanchonetes = List<Lanchonetes>();

   listOfLanchonetes.add(
    Lanchonetes(name: "Esquina do Açaí",
        img: "esquina_do_acai.jpg"));
   listOfLanchonetes.add(
    Lanchonetes(name: "Lanchonete Água na boca",
    img: "esquina_do_acai.jpg"));
   listOfLanchonetes.add(
    Lanchonetes(name: "Kídelicia", img: "esquina_do_acai.jpg"));
   listOfLanchonetes.add(
    Lanchonetes(name: "Godinho Lanches", img: "godinho.jpeg" ));
   return listOfLanchonetes;
  }
}


import 'package:flutter/material.dart';

class ContactPage extends StatelessWidget {
 @override
Widget build(BuildContext context) {
return SafeArea(
    child: Scaffold(
      backgroundColor: Colors.blueGrey[100],
        body: ListView(children: <Widget>[
          Column(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                child: Stack(children: <Widget>[

                  Image.asset("images/esquina_do_acai.jpg"),
                  Container(
                    height: 420.0,
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                        begin: FractionalOffset.center,
                          end: FractionalOffset.bottomCenter,
                          colors: [
                            Colors.grey.withOpacity(0.0),
                            Colors.black,
                          ])
                    ),
                  ),

                  Positioned(
                    bottom: 10.0,
                    left: 15.0,
                    child: Text(
                      "Esquina do Açaí",
                      style: TextStyle(
                          fontSize: 40.0, 
                          fontWeight: FontWeight.normal, 
                          color: Colors.white),
                    ),
                  )
                ]
                ),

              )
            ],
          ),
          Padding(
            padding: EdgeInsets.only(top: 5.0),
            child: GestureDetector(
              child: Card(
                elevation: 1.0,
                child: Container(
                  height: 100.0,
                  color: Colors.white,
                  child: Padding(
                    padding: EdgeInsets.symmetric(
                        vertical: 30.0, horizontal: 15.0),
                    child: Text(
                      "Cardápio",
                      style: TextStyle(
                          fontSize: 30.0, 
                          fontWeight: FontWeight.bold),
                    ),
                  ),

                ),
              ),
              onTap: () {},
            ),

          ),

          Card(
              elevation: 1.0,
              child: Container(
                  height: 100.0,
                  color: Colors.white,
                  child: Padding(
                    padding: EdgeInsets.symmetric(
                        vertical: 30.0, horizontal: 15.0),
                    child: Text(
                      "Pedir",
                      style: TextStyle(
                          fontSize: 30.0, fontWeight:
                          FontWeight.bold),
                    ),
                  )))
        ])));
  }
}
4

1 回答 1

0

在您的导航功能中,您可以传递一个对象

在您的 onTap 上,您应该在导航功能中像这样传递您的对象

onTap: () {
      _onLanchonetesButtonPressed(context, _allLanchonetes[index]);
}

并在函数中接收它并将其传递到新页面

void _onLanchonetesButtonPressed(BuildContext context,Lanchonetes lanchonetes) {
      Navigator.push(
      context, MaterialPageRoute(builder: (context) => ContactPage(lanchonetes)));
    }

on new page create constructor 



 class ContactPage extends StatelessWidget {
        Lanchonetes lanchonetes;

        ContactPage({this.lanchonetes});

        //

        Your code 

        //

    }

有了这个,您可以在新页面中使用您的 lanchonetes 对象

于 2019-05-03T06:27:59.490 回答