我有一个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);
}
}
答案 0 :(得分:1)
使用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();
}
}