flutter constructor stateful widget code example

Example 1: flutter stateful widget

class YellowBird extends StatefulWidget {
  const YellowBird({ Key key }) : super(key: key);

  
  _YellowBirdState createState() => _YellowBirdState();
}

class _YellowBirdState extends State<YellowBird> {
  
  Widget build(BuildContext context) {
    return Container(color: const Color(0xFFFFE306));
  }
}

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

  
  _ServerIpTextState createState() => _ServerIpTextState();
}

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

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

Tags:

Dart Example