有没有办法从ListView项目更改屏幕?
我必须制作一个颤振应用程序,在我的主屏幕页面中填充一个ListView(这个小部件填充了来自firebase数据库的数据),我已经完成了这项工作,但现在我必须制作一个onPressed事件。我已经用InkWell完成了这项工作,但现在我不知道如何使每个项目都具有不同的功能 这是我的代码: //主屏幕有没有办法从ListView项目更改屏幕?,listview,flutter,listviewitem,Listview,Flutter,Listviewitem,我必须制作一个颤振应用程序,在我的主屏幕页面中填充一个ListView(这个小部件填充了来自firebase数据库的数据),我已经完成了这项工作,但现在我必须制作一个onPressed事件。我已经用InkWell完成了这项工作,但现在我不知道如何使每个项目都具有不同的功能 这是我的代码: //主屏幕 class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { List&
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
List<Function> itemFunctions = [
() {
print("1");
Navigator.pop(context, MaterialPageRoute(builder: (context) => Antique()));},
() {Navigator.pop(context, MaterialPageRoute(builder: (context) => Mediterana()));},
() {Navigator.pop(context, MaterialPageRoute(builder: (context) => SuperKebab()));},
];
return new Scaffold(
backgroundColor: Colors.white,
appBar: new AppBar(
title: new Text("e-Radauti Restaurante"),
),
body: Container(
child: StreamBuilder(
stream: _firebaseRef.onValue,
builder: (context, snap) {
if (snap.hasData &&
!snap.hasError &&
snap.data.snapshot.value != null) {
Map data = snap.data.snapshot.value;
List item = [];
data.forEach(
(index, data) => item.add({"key": index, ...data}));
return ListView.builder(
itemCount: item.length,
itemBuilder: (context, index) {
return Card(
child: Container(
child: InkWell(
child: MediaQuery.removePadding(context: context,removeTop: true,removeBottom: true, child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
width: 100,
child: CachedNetworkImage(
placeholder: (context, url) =>
CircularProgressIndicator(),
imageUrl:
item[index]["photoUrl"].toString(),
errorWidget: (context, url, error) =>
Icon(Icons.error),
),
),
Column(
children: <Widget>[
Text(item[index]["name"]),
Text(item[index]["description"]),
],
)
]
),),
onTap: () {itemFunctions[index]; print("Clicked item nr $index");}
),
),
);
},
);
}
return CircularProgressIndicator();
})
),
);
}
}
编辑:我的点击项目似乎有效,但列表中的功能代码>未被调用
这是我的日志输出
这是我的firebase数据库结构:
所以基本上我的第一件是古董,第二件是麦迪塔纳,最后一件是超级烤肉串
当我点击第一个项目(古董)导航到古董屏幕时,当我点击第二个项目时,我希望它导航到Mediterana屏幕,依此类推
我认为一种方法是创建单个函数,并根据具体情况使用firebase存储中的数据填充该函数
任何想法都会对我有帮助。提前感谢您可以手动或通过编程将函数定义到列表中,然后使用ListView.builder中的索引调用该列表,如下所示
List<Function> itemfunctions = [
(){print('Button Pressed!');},
(){Navigator.of(context).pop()},
(){Navigator.popAndPushNamed(context, NewScreen.routeName)},
];
//Where you initialize this depends on whether you need context or not
ListView.builder(
shrinkWrap: true,
itemCount: numbers.length,
itemBuilder: (context, index) => Column(
children: <Widget>[
Text(
numbers[index].toString(),
),
RaisedButton(
child: Text('$index number'),
onPressed: itemfunctions[index],
)
],
),
)
列表项函数=[
(){打印('按钮按下!');},
(){Navigator.of(context.pop()},
(){Navigator.popandpushname(context,NewScreen.routeName)},
];
//初始化的位置取决于是否需要上下文
ListView.builder(
收缩膜:对,
itemCount:number.length,
itemBuilder:(上下文,索引)=>列(
儿童:[
正文(
数字[索引].toString(),
),
升起的按钮(
子项:文本(“$index number”),
onPressed:itemfunctions[索引],
)
],
),
)
Listview中最多有多少个项目?@user我认为20-30个项目不适用。我点击这些项目,但什么也没发生。我会升级我的代码,如果您愿意帮助我,请这样做,因为您试图从函数内部调用函数,我仍然需要上下文。。。我应该将函数放在哪里?我更改了代码以反映您需要进行的更改,如果您想将打印语句保留在onTap
中,您需要删除列表中的(){}
,您不能(){}
在onTap和列表中
如果需要,其上下文位于构建函数顶部的右侧位置
List<Function> itemfunctions = [
(){print('Button Pressed!');},
(){Navigator.of(context).pop()},
(){Navigator.popAndPushNamed(context, NewScreen.routeName)},
];
//Where you initialize this depends on whether you need context or not
ListView.builder(
shrinkWrap: true,
itemCount: numbers.length,
itemBuilder: (context, index) => Column(
children: <Widget>[
Text(
numbers[index].toString(),
),
RaisedButton(
child: Text('$index number'),
onPressed: itemfunctions[index],
)
],
),
)