我的代码混乱,无法找到解决方案。我写了一个简单的应用程序向您展示我的问题。
有两个屏幕:home_screen和dataEntry_screen。在主屏幕上,有两个字段:“年龄”和“高度”。如果我触摸“年龄”字段,则会打开dataEntry_screen,在其中可以输入字符串,当我按“将数据发送到home_screen”时,它将在“年龄”字段上显示字符串。 在这两个页面之间传递数据并不是真正的问题。但是,当我触摸“高度”字段时,它将打开相同的dataEntry_screen,我可以在其中进行输入,然后将其发送到“高度”字段。
我尝试了不同的方法,但是没有任何效果。如果我在dataEntry_page上输入条目,然后按“将数据发送到home_screen”,则数据将同时显示在“年龄”和“身高”屏幕上。我知道代码为什么会这样工作,但是我不知道如何使代码像这样工作:选中每个字段,打开dataEntry_screen并将不同的数据从同一'dataEntry_screen'发送到每个字段。
附带的代码只是描述问题的示例代码。我的实际应用程序具有几个不同的字段,并且由于我不喜欢标准键盘输入数据,因此我设计了自己的键盘,希望用来在不同的字段中进行输入。我希望您能理解我的问题,因为我是编程新手。
import 'package:flutter/material.dart';
import 'home_screen.dart';
import 'dataEntry_screen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(initialRoute: HomeScreen.id, routes: {
HomeScreen.id: (context) => HomeScreen(),
DataEntryScreen.id: (context) => DataEntryScreen(),
});
}
}
import 'package:flutter/material.dart';
import 'package:senddatastackoverflow/dataEntry_screen.dart';
class HomeScreen extends StatelessWidget {
HomeScreen({this.firstDataToReceive, this.secondDataToReceive});
final String firstDataToReceive;
final String secondDataToReceive;
static String id = 'home_screen';
String firstData = '';
String secondData = '';
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Age:'),
Center(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => DataEntryScreen(),
),
);
},
child: Container(
width: 200,
height: 50,
color: Colors.grey,
alignment: Alignment.center,
child:
Text(firstDataToReceive == null ? '' : firstDataToReceive),
),
),
),
SizedBox(
height: 50,
),
Text('Height:'),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => DataEntryScreen(),
),
);
},
child: Container(
width: 200,
height: 50,
color: Colors.grey,
alignment: Alignment.center,
child:
Text(secondDataToReceive == null ? '' : secondDataToReceive),
),
),
],
),
);
}
}
import 'package:flutter/material.dart';
import 'package:senddatastackoverflow/home_screen.dart';
class DataEntryScreen extends StatefulWidget {
static String id = 'dataEntry_screen';
@override
_DataEntryScreenState createState() => _DataEntryScreenState();
}
class _DataEntryScreenState extends State<DataEntryScreen> {
String data1;
String data2;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
child: TextField(
textAlign: TextAlign.center,
onChanged: (value) {
setState(() {
data1 = value;
});
},
),
),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => HomeScreen(
firstDataToReceive: data1,
secondDataToReceive: data2,
),
),
);
},
child: Container(
alignment: Alignment.center,
height: 50,
width: 200,
color: Colors.green,
child: Text('send data to home_screen'),
),
)
],
),
),
);
}
}
答案 0 :(得分:0)
如果您要问的是如何将数据从dataEntry_screen
发送回home_screen
,则可以将该数据传递到pop
中的dataEntry_screen
,然后在then
中push
语句的home_screen
块。
在home_screen
中:
Navigator.of(context).pushNamed(
DataEntryScreen.id,
).then((value) {
if(value != null) {
setState(() {
// Type cast to the data sent back via pop.
var args = value as Map<String,String>;
firstData = args['age'];
secondData = args['height'];
});
}
});
在dataEntry_screen
中,而不是push
中,home_screen
使用pop
发送回数据:
Navigator.of(context).pop({
'age' : data1,
'height' : data2,
});
要将数据从home_screen
发送到dataEntry_screen
,如果需要这些值,可以在arguments
中发送push
并从ModalRoute
中提取。
在home_screen
中:
Navigator.of(context).pushNamed(
DataEntryScreen.id,
arguments: {
'age': firstData,
'height': secondData,
}, // arguments to be sent to DataEntryScreen
).then((value) {
if(value != null) {
setState(() {
// Type cast to the data sent back via pop.
var args = value as Map<String,String>;
firstData = args['age'];
secondData = args['height'];
});
}
});
和dataEntry_screen
中的
@override
Widget build(BuildContext context) {
if(data1 == null && data2 == null) {
// Type cast to data sent via push
var args = ModalRoute.of(context).settings.arguments as Map<String, String>;
data1 = args['age'];
data2 = args['height'];
}
....
}
答案 1 :(得分:0)
不确定我是否正确理解。 您希望年龄或身高,而不是同时还是全部?
遵循Navaneeth P的答案可能会回答大多数人的导航并传递数据答案。
要么使用状态管理来决定要构建什么,要么何时进行操作,然后基于args [“ what you need”]返回Navigator.push()等待它。 进行检查并确定要建造什么。
_onTap() async {
var args = await Navigator.push()
if(args["your variable"]!=null){ // if you pop without entering it will be null.
setState(() {
//something that decides what to build or clear the other variables.
tobuild = "something"; //or even a boolean.
age = args["age"];
})
}
}
构建功能
@override
Widget build(BuildContext context) {
return ...
(tobuild =="Age"|| age!=null)? // set your condition
Text('Age:'),
Center(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => DataEntryScreen(),
),
);
},
child: Container(
width: 200,
height: 50,
color: Colors.grey,
alignment: Alignment.center,
child:
Text(firstDataToReceive == null ? '' : firstDataToReceive),
),
),
):Container(//or whatever other conditions to check);
}