Question #193EasyFlutter Basics

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

ShortcutCreatesDescription
text
stless
StatelessWidgetImmutable widget
text
stful
StatefulWidgetMutable widget
text
stanim
StatefulWidget with AnimationWidget with AnimationController
text
build
build methodOverride build method
text
initS
initState methodInitialize state
text
dis
dispose methodCleanup resources

Custom Snippets

VS Code Custom Snippet (

text
.vscode/flutter.code-snippets
):

json
{
  "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

ActionWindows/LinuxMac
Extract Widget
text
Ctrl + .
text
Cmd + .
Wrap with Widget
text
Ctrl + .
text
Cmd + .
Remove Widget
text
Ctrl + .
text
Cmd + .
Extract Method
text
Ctrl + Alt + M
text
Cmd + Opt + M

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
    text
    Ctrl + Shift + P
    (Windows/Linux)
  • Press
    text
    Cmd + Shift + P
    (Mac)
  • 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.