flutter父子传值,StatefulWidget和StatelessWidget
flutter 父子组件传值
·
父组件:
ElevatedButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => FormPage(title: 'form page!')));
},
style: ButtonStyle(
padding: MaterialStateProperty.all(
const EdgeInsets.fromLTRB(20, 10, 20, 10))),
child: const Text('传值按钮'),
)
子组件的StatelessWidget模式:
class FormPage extends StatelessWidget {
late String title;
FormPage({Key? key, this.title = "form"}) : super(key: key);
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: ListView(
children: const [
ListTile(
title: Text('表单内容'),
)
],
),
);
}
}
子组件的StatefulWidget模式:
class FormPage extends StatefulWidget {
String title;
FormPage({Key? key, required this.title}) : super(key: key);
// ignore: no_logic_in_create_state
State<FormPage> createState() => _FormPageState(title: title);
}
class _FormPageState extends State<FormPage> {
String title;
_FormPageState({required this.title});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(
children: const [
ListTile(
title: Text('表单内容'),
)
],
),
);
;
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)