StatelessWidget Flutter

18-Sep-2024

Learn the concept and usage of StatelessWidget in Flutter for static content

StatelessWidget is the base class for widgets that do not change their mutable state.
StatelessWidget is created, but its properties cannot be modified.
All values ​​are final.
Stateless widgets are typically used for UI components that don't need to maintain  internal state.



import 'package:flutter/material.dart';


void main() {
runApp(
MaterialApp(
home: Scaffold(
body: MyApp(text: "Hello World"),
),
)
);
}

class MyApp extends StatelessWidget {

String text;
MyApp({required this.text});


@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Text(text),
),
);
}
}




In this example, MyStatelessWidget is a stateless widget that accepts a text parameter in its constructor.
In the build method we define the widget's UI structure.
Once a widget is created with a specific set of properties, those properties cannot be changed during the widget's lifetime.


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