[英]How to send a data from listview screen to form screen using flutter
I am trying to send a data from ontap listview screen to form screen like image below.我正在尝试从 ONTAP 列表视图屏幕发送数据以形成如下图所示的屏幕。 I have searched many references on google but I can't find any references that can help me, if you can provide solutions or references, I will greatly appreciate it.
我在谷歌上搜索了很多参考资料,但我找不到任何可以帮助我的参考资料,如果你能提供解决方案或参考资料,我将不胜感激。 enter image description here
在此处输入图像描述
Pass the tapped item value to the next page via named parameter of other page class.通过其他页面 class 的命名参数将点击的项目值传递到下一页。
class ListPage extends StatelessWidget {
const ListPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return ListTile(
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) {
return NextPage(value: index);
},
));
},
title: Text(index.toString()),
);
},
),
);
}
}
class NextPage extends StatelessWidget {
final int value;
const NextPage({Key? key, required this.value}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(value.toString()),
),
);
}
}
Example in ListView screen, you have a variable called List<String> listLocations
.在 ListView 屏幕中的示例,您有一个名为
List<String> listLocations
的变量。 Your ListView widget be like:您的 ListView 小部件如下所示:
ListView.builder(
itemCount: listLocations.length,
itemBuilder: (context, index) {
return InkWell(
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) {
return SecondScreen(listLocations[index]);
},
)),
child: ...
);
}
}
And your SecondScreen is a StatefulWidget (well it is a Form screen, so it would be Stateful, not Stateless, use TextEditingController
in Form widget):而您的 SecondScreen 是一个 StatefulWidget(它是一个表单屏幕,所以它是有状态的,而不是无状态的,在表单小部件中使用
TextEditingController
):
import 'package:flutter/material.dart';
class SecondScreen extends StatefulWidget {
final String location;
SecondScreen(this.location, {Key? key}) : super(key: key);
@override
State<SecondScreen> createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
var _textEditingController = TextEditingController();
@override
void initState() {
_textEditingController.text = widget.location;
super.initState();
}
@override
void dispose() {
_textEditingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container();
}
}
You need to pass the location value in init state, and don't forget to dispose it.您需要在 init state 中传递位置值,并且不要忘记处理它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.