gpt4 book ai didi

flutter - 在页面开始运行方法

转载 作者:IT王子 更新时间:2023-10-29 07:23:23 24 4
gpt4 key购买 nike

我有一个 Statless 小部件如何在加载或启动小部件时运行函数

如何在小部件打开时运行_myFunction

class Register extends StatelessWidget{

final GlobalKey<ScaffoldState> _registerKey = GlobalKey<ScaffoldState>();

@override
Widget build(BuildContext context) {
return Scaffold(
key: _registerKey,
appBar: new AppBar(
title: new Text('Register'),
centerTitle: true,
),
body: new Center(
child: new Column(
children: <Widget>[
new Container(
margin: const EdgeInsets.only(left: 50, right: 50, top: 20),
child: new TextField(
decoration: InputDecoration(
icon: Icon(Icons.store), hintText: "Store name"),
style: new TextStyle(
fontSize: 20,
color: const Color(0xFF2980b9),
)),
),
new Container(
margin: const EdgeInsets.only(left: 50, right: 50, top: 20),
child: new TextField(
decoration: InputDecoration(
icon: Icon(Icons.person), hintText: "Employee name"),
style: new TextStyle(
fontSize: 20,
color: const Color(0xFF2980b9),
)),
),
new Container(
margin: const EdgeInsets.only(left: 50, right: 50, top: 20),
child: new TextField(
decoration: InputDecoration(
icon: Icon(Icons.email), hintText: "e-mail"),
style: new TextStyle(
fontSize: 20,
color: const Color(0xFF2980b9),
)),
),
new Container(
margin: const EdgeInsets.only(left: 50, right: 50, top: 20),
child: new TextField(
decoration:
InputDecoration(icon: Icon(Icons.phone), hintText: "Phone"),
style: new TextStyle(
fontSize: 20,
color: const Color(0xFF2980b9),
)),
),
new Container(
margin: const EdgeInsets.only(left: 50, right: 50, top: 20),
child: new TextField(
obscureText: true,
decoration: InputDecoration(
icon: Icon(Icons.lock), hintText: "Password"),
style: new TextStyle(
fontSize: 20,
color: const Color(0xFF2980b9),
)),
),
new Container(
margin: const EdgeInsets.only(left: 50, right: 50, top: 20),
child: new TextField(
decoration: InputDecoration(
icon: Icon(Icons.location_on), hintText: "Address"),
style: new TextStyle(
fontSize: 20,
color: const Color(0xFF2980b9),
)),
),
new Container(
margin: const EdgeInsets.only(left: 50, right: 50, top: 20),
child: new Row(
children: <Widget>[
new DropdownButton<String>(
hint: new Text("Choose city"),
items: <String>['A', 'B', 'C', 'D'].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (_) {},
),
new DropdownButton<String>(
hint: new Text("Choose area"),
items: <String>['A', 'B', 'C', 'D'].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (_) {},
)
],
),
),
new Container(
margin: const EdgeInsets.only(left: 50, right: 50, top: 20),
child: new Container(
child: new RaisedButton(
onPressed: null,
color: const Color(0xFF2980b9),
child: new Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Text("Register",
style: new TextStyle(
fontSize: 20,
color: const Color(0xFFffffff),
fontFamily: "Roboto")),
new Icon(
Icons.arrow_forward,
color: const Color(0xFFffffff),
),
],
)),
)),
],
)),
);
}


void _myFunction() {
snackBar("Success", _registerKey);
}

}

最佳答案

使用 StatefulWidget 并在 initState 中执行:

class Foo extends StatefulWidget {
@override
_FooState createState() => _FooState();
}

class _FooState extends State<Foo> {
@override
void initState() {
// TODO: call your method
super.initState();
}

@override
Widget build(BuildContext context) {
return Container();
}
}

另一种方法是使用 hooks

class Foo extends HookWidget {
@override
Widget build(BuildContext context) {
useEffect(() {
// TODO: call your method here
}, const []);

return Container();
}
}

关于flutter - 在页面开始运行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54404009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com