Run method at page start

0

I have a Statless widget how to run a function when the widget is loaded or started

How to run _myFunction when the widget is opened?

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);
  }

}
flutter
asked on Stack Overflow Jan 28, 2019 by Mohamed Adel

1 Answer

1

Use a StatefulWidget and do that inside 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();
  }
}

An alternative is to use hooks

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

    return Container();
  }
}
answered on Stack Overflow Jan 28, 2019 by RĂ©mi Rousselet

User contributions licensed under CC BY-SA 3.0