constructor for stateful widget flutter code example
Example 1: send params to stateful widget
class Screen1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Screen2(userId: "89d6db7e-7469-4888-9fac-38c300628ff1") // sends the value
);
}
}
class Screen2 extends StatefulWidget {
final String userId; // receives the value
Screen2({ Key key, this.userId }): super(key: key);
@override
_Screen2State createState() => _Screen2State();
}
class _Screen2State extends State {
@override
Widget build(BuildContext context) {
return Text(widget.userId); // uses the value
}
}
Example 2: flutter stateful widget
class YellowBird extends StatefulWidget {
const YellowBird({ Key key }) : super(key: key);
@override
_YellowBirdState createState() => _YellowBirdState();
}
class _YellowBirdState extends State {
@override
Widget build(BuildContext context) {
return Container(color: const Color(0xFFFFE306));
}
}
Example 3: make stateful widget flutter
class MyClass extends StatefulWidget {
@override
MyClassState createState() => new MyClassState();
}
class MyClassState extends State {
//Widgets and other code here
}
Example 4: 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 {
@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")
);
}
}