5. Flutter - Stateless Widget Class (&HotReloads)
Stateless vs Stateful Widget:
- Stateless: state of widget cannot change over time. Final Widget.
- Stateful: state of widget can change over time. Dynamic Widget.
- Stateful and Stateless widget are basically custom widgets. We can put any widget inside it by returning a widget.
Creating a stateless widget class:
Using snippet "stless"
class testclass extends StatelessWidget {
// const test({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container();
}
}
// Replace Container() with widget tree widget, for example, the Scaffold() Widget.
// The "@override" tells the function to override the extended "StatelessWidget's" own build function with the one in our custom stateless widget.
// Stateless widget is basically a widget. If we pop it as a widget in the "home: " property of the app, then it becomes the home widget.
// Any change within the build function will be updated on a save (hot reload). The function returns a widget that is newly changed and displays on the emulator.
Using the stateless widget:
void main() => runApp(MaterialApp(
home: Home(), // Home() is our custom stateless widget.
));
// Our Home Stateless Widget only returns a Scaffold() widget tree full of widgets.
// We can reuse the Home() widget in any other widget trees.
Comments
Post a Comment