State Less Wideget
State Less Wideget
▲▼▲▼▲▼
tags : #coding #flutter #widget
references : Widget
▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼
▲▼▲▼▲▼
A StatelessWidget is one of the fundamental building blocks in Flutter for creating user
interfaces. Here’s a comprehensive overview of StatelessWidget :
Overview
Key Characteristics
1. Immutability: The configuration of a StatelessWidget does not change over time. This
means that all properties of a StatelessWidget are final.
2. Single Build Method: A StatelessWidget only has one method to override:
build(BuildContext context) . This method describes the part of the user interface
represented by this widget.
Lifecycle
Example
import 'package:flutter/material.dart';
return MaterialApp(
home: Scaffold(
appBar: AppBar(
),
body: Center(
child: MyStatelessWidget(),
),
),
);
@override
return Text(
'Hello, Flutter!',
);
Usage :
Static Content: StatelessWidget is ideal for static content that does not change after the
widget is built, such as text labels, icons, static lists, etc.
Performance: Since StatelessWidget does not change, it can be more efficient in terms
of performance compared to StatefulWidget .
Best Practices :
1. Keep it Simple: Since StatelessWidget does not have internal state, keep the logic
simple and focus on UI representation.
2. Pure Functions: The build method should be a pure function, meaning it should not have
any side effects and should return the same output given the same input.
3. Composability: Break down complex UIs into smaller, reusable StatelessWidget s.
State Management: Use StatefulWidget if you need to manage state, handle user
interactions, or work with animations. StatefulWidget has an associated State object
that can be changed over time.
Rebuilds: StatefulWidget can trigger rebuilds based on state changes, while
StatelessWidget rebuilds only when its parent widget rebuilds.