在主屏幕上,我在输入字段中输入文本,单击按钮,发生到第二屏幕的转换,数据被传输并显示在第二屏幕上。
当我在第二个屏幕上执行相同操作时,当我转到第一个屏幕时,数据不会传输。
第一种方式
Navigator.pop(context, _MyHomePageState(data: _controller.text));
第二种方式
Navigator.push(
context,
//MaterialPageRoute(builder: (context) => Two())
MaterialPageRoute(builder: (_) => _MyHomePageState(data: _controller.text,))
).then((value) {
_controller.text = ''; //очистка поля ввода
});
误入
_MyHomePageState(data: _controller.text,)
接下来是main.dart的完整代码和截图
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Name App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState(data: null);
}
class _MyHomePageState extends State<MyHomePage> {
//это будет передаваться в другой экран
TextEditingController _controller;
//это будет передано с другого экрана
String data;
_MyHomePageState ({@required this.data});
@override
void initState() {
super.initState();
_controller = TextEditingController();
data = '';
}
@override
void dispose() {
super.dispose();
}
void _f1() {
setState(() {
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Main screen'),
),
body: Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: _controller,
cursorColor: Colors.pink,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0),
borderSide: BorderSide()
),
),
),
),
),
Align(
alignment: Alignment.center,
child: Text(
(data == null) ? '' : data,
style: TextStyle(
fontSize: 46.0,
color: Colors.pink
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
width: double.infinity,
margin: const EdgeInsets.all(16.0),
child: RaisedButton(
onPressed: () {
Navigator.push(
context,
//MaterialPageRoute(builder: (context) => Two())
MaterialPageRoute(builder: (_) => Two(data: _controller.text,))
).then((value) {
_controller.text = ''; //очистка поля ввода
});
},
splashColor: Colors.blue.withOpacity(0.5),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)
),
child: Text(
'Go to Two',
style: TextStyle(
color: Colors.deepPurple,
fontSize: 20.0,
),
),
),
),
),
],
),
);
}
}
class Two extends StatelessWidget {
//это будет передаваться в другой экран
TextEditingController _controller = TextEditingController();
//это будет передаваться в другой экран
String data;
Two({Key key, @required this.data}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Two screen'),
),
body: Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: _controller,
cursorColor: Colors.pink,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0),
borderSide: BorderSide()
),
),
),
),
),
Align(
alignment: Alignment.center,
child: Text(
(data == null) ? '' : data,
style: TextStyle(
fontSize: 46.0,
color: Colors.blue
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
width: double.infinity,
margin: const EdgeInsets.all(16.0),
child: RaisedButton(
onPressed: () {
//1 способ ошибки нет, но данные не выводятся
Navigator.pop(context, _MyHomePageState(data: _controller.text));
//2 способ ошибка в _MyHomePageState(data: _controller.text,)
/*Navigator.push(
context,
//MaterialPageRoute(builder: (context) => Two())
MaterialPageRoute(builder: (_) => _MyHomePageState(data: _controller.text,))
).then((value) {
_controller.text = ''; //очистка поля ввода
});*/
},
splashColor: Colors.blue.withOpacity(0.5),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)
),
child: Text(
'Go to Main',
style: TextStyle(
color: Colors.deepPurple,
fontSize: 20.0,
),
),
),
),
),
],
),
);
}
}
_MyHomePageState构造函数调用应该只执行一次,当_MyHomePage小部件创建时,所有其他调用都没有意义,因为 新副本只是悬而未决。从构造函数中删除了data参数- 它对根小部件无用。
从第二页返回到第一页时,只传递数据本身,在这种情况下,是_controller.text的内容。如果您需要一次返回多个值,那么您可以创建自己的类并返回它的一个实例,但在或多或少严肃的项目中,您应该使用全局状态存储,例如BLOC。
从第二个页面接收到的值的处理必须伴随一个 setState调用,否则主页面将显示旧数据。
以下是 diff 格式代码的主要更改: