StatefulWidget Flutter

18-Sep-2024

Learn how to use StatefulWidget in Flutter to create interactive and dynamic UI elements

A StatefulWidget is a class that contains mutable state. Used when some part of the UI you are creating may change dynamically and you want the UI to reflect those changes.


Here's a simple example of using a StatefulWidget Flutter:

File open lib->main.dart file −



import 'package:flutter/material.dart';

void main() {
runApp(
MyApp()
);
}
class MyApp extends StatefulWidget{

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

class _MyAppState extends State<MyApp> {

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text('StatefulWidget Example',
style:TextStyle(fontSize: 20,color: Colors.white)),
),
body:Container(
alignment: Alignment.center,
child: Text("Stateful Widget",style: TextStyle(fontSize: 20),))
),
);

}
}






Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.


Output:


Comments