如何导航到另一个页面但根据我的列表中按下的项目显示信息?
How to navigate to another page but show information according to the items on my list that was pressed?
我正在尝试用 flutter 创建我的第一个应用程序,它是一个基本的应用程序,用于显示列表中的项目,你可以点击它们来获取信息。我已经创建了列表和联系人页面,我可以从一个页面导航到另一个页面,但我现在被困在如何根据我在列表中选择的联系人让 flutter 显示信息,所以我不需要写每个联系人一个 class。有办法吗?
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),
),
)))
])));
}
}
在您的导航函数中,您可以传递一个对象
在你的 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 对象
我正在尝试用 flutter 创建我的第一个应用程序,它是一个基本的应用程序,用于显示列表中的项目,你可以点击它们来获取信息。我已经创建了列表和联系人页面,我可以从一个页面导航到另一个页面,但我现在被困在如何根据我在列表中选择的联系人让 flutter 显示信息,所以我不需要写每个联系人一个 class。有办法吗?
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),
),
)))
])));
}
}
在您的导航函数中,您可以传递一个对象
在你的 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 对象