What is the shortcut to create flutter stateless and stateful widgets ?
#flutter#widget#state
Answer
Shortcuts to Create Flutter Widgets
Flutter provides convenient shortcuts and snippets to quickly create StatelessWidget and StatefulWidget classes, significantly speeding up development workflow.
IDE Shortcuts
VS Code (with Flutter Extension)
dart// Type and press Tab or Enter: stless → Creates StatelessWidget stful → Creates StatefulWidget
Android Studio / IntelliJ IDEA
dart// Type and press Tab or Enter: stless → Creates StatelessWidget stful → Creates StatefulWidget
Generated StatelessWidget
dart// Type: stless + Tab class MyWidget extends StatelessWidget { const MyWidget({Key? key}) : super(key: key); Widget build(BuildContext context) { return Container(); } }
Generated StatefulWidget
dart// Type: stful + Tab class MyWidget extends StatefulWidget { const MyWidget({Key? key}) : super(key: key); State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { Widget build(BuildContext context) { return Container(); } }
Additional Useful Shortcuts
| Shortcut | Creates | Description |
|---|---|---|
text | StatelessWidget | Immutable widget |
text | StatefulWidget | Mutable widget |
text | StatefulWidget with Animation | Widget with AnimationController |
text | build method | Override build method |
text | initState method | Initialize state |
text | dispose method | Cleanup resources |
Custom Snippets
VS Code Custom Snippet (
text
.vscode/flutter.code-snippetsjson{ "StatelessWidget": { "prefix": "stless", "body": [ "class ${1:WidgetName} extends StatelessWidget {", " const ${1:WidgetName}({super.key});", "", " @override", " Widget build(BuildContext context) {", " return ${2:Container}();", " }", "}" ] } }
Quick Actions
Wrap with Widget (Keyboard shortcuts):
- Windows/Linux: text
Ctrl + . - Mac: text
Cmd + .
dart// Place cursor on widget and use quick action: Text('Hello') // Convert to: Container( child: Text('Hello'), )
Refactoring Shortcuts
| Action | Windows/Linux | Mac |
|---|---|---|
| Extract Widget | text | text |
| Wrap with Widget | text | text |
| Remove Widget | text | text |
| Extract Method | text | text |
Live Templates (Android Studio)
dart// Create custom live template: // Settings → Editor → Live Templates → Flutter // Example template: class $NAME$ extends StatelessWidget { const $NAME$({super.key}); Widget build(BuildContext context) { return $END$; } }
Command Palette
VS Code:
- Press (Windows/Linux)text
Ctrl + Shift + P - Press (Mac)text
Cmd + Shift + P - Type "Flutter: New"
Available Commands:
- Flutter: New Project
- Flutter: New Stateless Widget
- Flutter: New Stateful Widget
- Flutter: New Bloc Class
Pro Tip: Install Flutter extensions in your IDE to unlock all these shortcuts and significantly boost your development productivity.
Learn more at Flutter Development Tools.