Build custom Flutter widgets with state management
✓Works with OpenClaudeYou are a Flutter developer. The user wants to build custom Flutter widgets with proper state management using StatefulWidget or StatelessWidget patterns.
What to check first
- Run
flutter doctorto verify Flutter SDK is installed and configured - Check that you have a Flutter project initialized with
flutter create project_name - Verify your IDE has the Dart and Flutter extensions installed
Steps
- Decide between StatelessWidget (immutable, no state) or StatefulWidget (mutable state with setState)
- Create a new Dart file in
lib/directory with your widget class name - Extend either
StatelessWidgetorStatefulWidgetdepending on your needs - Implement the required
build()method that returns a Widget - If using StatefulWidget, create a State class that extends
State<YourWidget> - Use
setState(() { })to trigger rebuilds when state changes in StatefulWidget - Pass parameters through the widget's constructor and store as final fields
- Build the UI tree by composing Flutter's built-in widgets (Container, Row, Column, Text, etc.)
- Test the widget in a MaterialApp by calling it in main.dart or a test file
Code
import 'package:flutter/material.dart';
// Stateless widget - immutable, no internal state
class GreetingCard extends StatelessWidget {
final String name;
final Color backgroundColor;
const GreetingCard({
required this.name,
this.backgroundColor = Colors.blue,
});
@override
Widget build(BuildContext context) {
return Card(
color: backgroundColor,
child: Padding(
padding: EdgeInsets.all(16),
child: Text(
'Hello, $name!',
style: TextStyle(fontSize: 20, color: Colors.white),
),
),
);
}
}
// Stateful widget - mutable state with setState
class CounterButton extends StatefulWidget {
final String label;
const CounterButton({this.label = 'Increment'});
@override
State<CounterButton> createState() => _CounterButtonState();
}
class _CounterButtonState extends State<CounterButton> {
int _count = 0;
void _incrementCounter() {
setState(() {
_count++;
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Count: $_count', style: TextStyle(fontSize: 24)),
SizedBox(height: 16),
ElevatedButton(
onPressed: _incrementCounter,
child: Text(widget.label),
),
],
);
}
}
// Usage in main.dart
void main() {
runApp(MaterialApp(
home: Scaffold(
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Flutter Skills
Other Claude Code skills in the same category — free to download.
Flutter Riverpod
Set up Riverpod for state management in Flutter
Flutter Navigation
Configure GoRouter for declarative navigation
Flutter HTTP
Build HTTP client with Dio and interceptors
Flutter Firebase
Integrate Firebase (Auth, Firestore, Storage) in Flutter
Flutter Testing
Write widget tests and integration tests for Flutter
Flutter State Management with Riverpod
Manage app state in Flutter using Riverpod 2.x for type-safe reactive state
Flutter Platform Channels
Call native iOS and Android code from Flutter via platform channels
Want a Flutter skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.