constructor in stateful widget flutter code example

Example 1: make stateful widget flutter

class MyClass extends StatefulWidget {
	@override
    MyClassState createState() => new MyClassState();
}

class MyClassState extends State<MyClass> {
	//Widgets and other code here
}

Example 2: flutter constructor in statefull widget

STATEFUL WIDGET CONSTRUCTOR

class ServerIpText extends StatefulWidget {
  final String serverIP;

  const ServerIpText ({ Key key, this.serverIP }): super(key: key);

  @override
  _ServerIpTextState createState() => _ServerIpTextState();
}

class _ServerIpTextState extends State<ServerIpText> {
  @override
  Widget build(BuildContext context) {
    return Text(widget.serverIP);
  }
}

class AnotherClass extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: ServerIpText(serverIP: "127.0.0.1")
    );
  }
}

Tags:

Misc Example