free geoip
31

StatefulWidget vs StatelessWidget in Dart Guide

When building mobile applications with Flutter using Android Studio, choosing between StatefulWidget and StatelessWidget is a crucial decision. These two…

When building mobile applications with Flutter using Android Studio, choosing between StatefulWidget and StatelessWidget is a crucial decision. These two types of widgets determine how your app behaves when state (data) changes.

A StatelessWidget is best suited for UI components that remain constant during runtime. On the other hand, a StatefulWidget is ideal for widgets that need to update dynamically based on user interaction or data change.

StatefulWidget vs StatelessWidget in Dart

Let’s break them down with full class examples:

Example of StatelessWidget

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import 'package:flutter/material.dart';
class MyStatelessWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Text(
'This is a StatelessWidget',
style: TextStyle(fontSize: 20),
),
);
}
}
import 'package:flutter/material.dart'; class MyStatelessWidget extends StatelessWidget { @override Widget build(BuildContext context) { return Center( child: Text( 'This is a StatelessWidget', style: TextStyle(fontSize: 20), ), ); } }
import 'package:flutter/material.dart';

class MyStatelessWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(
        'This is a StatelessWidget',
        style: TextStyle(fontSize: 20),
      ),
    );
  }
}

Key Characteristics:

  • Immutable once built.
  • Renders UI only once unless the parent rebuilds it.
  • Ideal for static layouts like titles, labels, or icons.

Example of StatefulWidget

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import 'package:flutter/material.dart';
class MyStatefulWidget extends StatefulWidget {
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _counter = 0;
void _increment() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Button pressed $_counter times'),
SizedBox(height: 10),
ElevatedButton(
onPressed: _increment,
child: Text('Increment'),
),
],
);
}
}
import 'package:flutter/material.dart'; class MyStatefulWidget extends StatefulWidget { @override _MyStatefulWidgetState createState() => _MyStatefulWidgetState(); } class _MyStatefulWidgetState extends State<MyStatefulWidget> { int _counter = 0; void _increment() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Button pressed $_counter times'), SizedBox(height: 10), ElevatedButton( onPressed: _increment, child: Text('Increment'), ), ], ); } }
import 'package:flutter/material.dart';

class MyStatefulWidget extends StatefulWidget {
  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _counter = 0;

  void _increment() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text('Button pressed $_counter times'),
        SizedBox(height: 10),
        ElevatedButton(
          onPressed: _increment,
          child: Text('Increment'),
        ),
      ],
    );
  }
}

Key Characteristics:

  • Has a mutable state.
  • Uses setState() to trigger UI updates.
  • Useful for interactive widgets like forms, animations, and toggles.

These examples show that understanding the role of each widget type is essential for clean and efficient Flutter app development.

For further learning, check out the official Flutter widget guide:
https://docs.flutter.dev/ui/widgets-intro

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *