In Flutter, widgets are the fundamental building blocks of the user interface. Everything you see on the screen is a widget, from buttons and text to layout structures and images. Widgets are immutable, meaning that once they are created, they cannot be changed. Instead, when the state of a widget changes, Flutter creates a new widget to reflect that change.
1. Types of Widgets
Widgets in Flutter can be categorized into two main types:
- Stateless Widgets: These widgets do not maintain any state. They are immutable and are rebuilt only when their parent widget changes. Stateless widgets are ideal for static content.
- Stateful Widgets: These widgets maintain state that can change during the lifetime of the widget. They can be rebuilt when their state changes, allowing for dynamic content.
2. Creating a Stateless Widget
Here’s an example of a simple stateless widget that displays a greeting message:
import 'package:flutter/material.dart';
class GreetingWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Text(
'Hello, Flutter!',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
);
}
}
In this example:
- We import the
material.dartpackage, which provides material design components. - The
GreetingWidgetclass extendsStatelessWidget. - The
buildmethod returns aCenterwidget containing aTextwidget that displays the greeting message.
3. Creating a Stateful Widget
Now, let’s create a stateful widget that maintains a counter:
class CounterWidget extends StatefulWidget {
@override
_CounterWidgetState createState() => _CounterWidgetState();
}
class _CounterWidgetState extends State<counterwidget> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Counter: $_counter',
style: TextStyle(fontSize: 24),
),
ElevatedButton(
onPressed: _incrementCounter,
child: Text('Increment'),
),
],
);
}
}
</counterwidget>In this example:
- The
CounterWidgetclass extendsStatefulWidget. - The
_CounterWidgetStateclass manages the state of the counter. - The
_incrementCountermethod updates the counter and callssetStateto rebuild the widget. - The
buildmethod returns aColumnwidget containing aTextwidget and anElevatedButton.
4. Using Widgets in the Main Application
To use these widgets in your main application, you can set up the main function as follows:
void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Widgets in Flutter')),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GreetingWidget(),
CounterWidget(),
],
),
),
));
}
In this example:
- The
mainfunction initializes the Flutter application. - A
MaterialAppis created with aScaffoldthat contains anAppBarand aColumnwidget. - Both the
GreetingWidgetandCounterWidgetare included in the column, displaying the greeting message and the counter.
5. Conclusion
Widgets are the core components of Flutter applications, allowing developers to create complex user interfaces with ease. By understanding the difference between stat eless and stateful widgets, you can effectively manage the UI and its interactions. This guide provided examples of both types of widgets, demonstrating how to create and use them in a Flutter application.
