Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!
We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!
Learn from Guru Rajesh Kumar and double your salary in just one year.
Stateful Widget :- When user interacts with an App and the widget change then it is called Stateful Widget. A Stateful widget is Dynamic.
For Example :- When user click on a button on App and text changes then it is called Stateful Widget.
When the widget’s state changes, the state objects calls setState(), telling the framework to redraw the widget.
Write down the below code in main.dart file in your project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: "My App", | |
home: new HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
String myText = "Hello World"; | |
void _changeText() { | |
setState(() { | |
if(myText.startsWith("H")){ | |
myText = "welcome To My App"; | |
} else{ | |
myText = "Hello World"; | |
} | |
}); | |
} | |
Widget _bodywidget() { | |
return new Container( | |
padding: const EdgeInsets.all(8.0), | |
child: new Center( | |
child: new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
new Text(myText), | |
new RaisedButton( | |
child: new Text("click"), | |
onPressed: _changeText, | |
), | |
], | |
), | |
), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text("Home Page"), | |
), | |
body: _bodywidget()); | |
} | |
} |