📱 What is a Widget?
In Flutter, everything is a widget. A widget is the basic building block of a Flutter app's user interface.
Think of widgets like LEGO blocks 🧱 — you combine small, simple pieces to build complex UIs.
Types of Widgets
1. StatelessWidget
A widget that doesn't change once it's built.
class MyGreeting extends StatelessWidget {
Widget build(BuildContext context) {
return Text('Hello, Vineel!');
}
}
2. StatefulWidget
A widget that can change over time (like a counter, toggle, or form).
class MyCounter extends StatefulWidget {
_MyCounterState createState() => _MyCounterState();
}
class _MyCounterState extends State<MyCounter> {
int count = 0;
Widget build(BuildContext context) {
return Column(
children: [
Text('Count: $count'),
ElevatedButton(
onPressed: () => setState(() => count++),
child: Text('Increment'),
),
],
);
}
}
Key Takeaway
In Flutter, UI = f(state). Your UI is a function of your app's state. When state changes, the widget rebuilds. Simple and powerful! 🚀
More Flutter articles coming soon...