【发布时间】:2021-12-04 20:49:34
【问题描述】:
我是个颤抖的新手 我想创建简单的输入数据并将数据传递到其他页面并显示文本。
这是我的代码
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
const appTitle = 'Form Validation Demo';
return MaterialApp(
title: appTitle,
home: Scaffold(
appBar: AppBar(
title: const Text(appTitle),
),
body: const MyForm(),
),
);
}
}
class MyForm extends StatefulWidget {
const MyForm({Key? key}) : super(key: key);
@override
MyFormState createState() {
return MyFormState();
}
}
class MyFormState extends State<MyForm> {
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Page2(_formKey)),
);
}
},
child: const Text('Submit'),
),
],
),
);
}
}
class Page2 extends StatelessWidget {
Page2(final data){
this.data = data;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Page 2'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(this.data),
RaisedButton(
child: Text('BACK'),
onPressed: () {
Navigator.pop(context);
},
),
],
),
),
);
}
}
但最后,它得到了
没有为“Page2”类型定义设置器“数据”。
错误
知道怎么做吗?
我的代码出了什么问题
非常感谢。
【问题讨论】:
标签: flutter