Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut to extract Flutter widget from UI layout

If I have a complex layout that I want to simplify, what is the shortcut in Android Studio (or IntelliJ) to extract the widget out into a method?

Example:

I want to extract the three main widgets in the Stack.

class BodyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/image.jpg'),
              fit: BoxFit.cover,
            ),
          ),
        ),
        Align(
          alignment: Alignment(-0.7, -0.7),
          child: Container(
            height: 300,
            child: RichText(
              text: TextSpan(
                text: 'My text',
                style: TextStyle(
                  color: Colors.white70,
                  fontSize: 30,
                ),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.bottomCenter,
          child: Text(
            'Some other text',
            style: TextStyle(
              color: Colors.white70,
              fontSize: 20.0,
              fontWeight: FontWeight.w900,
              letterSpacing: 5.0,
            ),
          ),
        ),
      ],
    );
  }
}

I could do it by hand but I'm looking for a shortcut.

like image 980
Suragch Avatar asked Jul 19 '19 03:07

Suragch


People also ask

How do you extract a flutter?

Select opened file - Option + F1 + Fn (in most cases), press 1 after. Show Context Actions - Option + Enter. Extract to separate widget - Select a widget, Refactor -> Extract -> Flutter Widget. You can right click /ios or /android module and open it in XCode or Studio to work on some native part.

How do you select a whole widget in flutter?

#3 Select the entire widget code snippet To simplify this tedious process, click a widget and then press Alt+Up arrow key on Mac or Ctrl+W on Windows to easily select all of the widget's code.


4 Answers

I'm updating this answer for both Android Studio and VS Code

Android Studio

Method 1

Shortcut keys:

  • Windows/Linux: Put your cursor on the widget name and press Ctrl+Alt+M to extract it as a method or Ctrl+Alt+W to extract it as a widget.
  • MacOS: Put your cursor on the widget name and press Option+Command+M to extract it as a method or Option+Command+W to extract it as a widget.

You can also accomplish the same thing by right clicking the widget name and choosing Refactor > Extract from the context menu.

Method 2

You can also extract a widget into a method or new widget from the Flutter Outline menu.

  1. Click Flutter Outline on the top left side
  2. Select the widget in the outline
  3. Right click and choose Extract method... or Extract widget...
  4. Give it a name

enter image description here

Visual Studio Code

Put your cursor on the widget name and press Command+. on a Mac or Ctrl+. on a PC. Then choose Method or Widget from the context menu.

like image 136
Suragch Avatar answered Sep 20 '22 04:09

Suragch


To Extract particular code of Flutter in Android studio to varialbe, constant, method, or as a Widget.

1. Select a code block you want to extract.

enter image description here

2. Right click -> Refactor -> Extract -> Select type of refactor you want.

enter image description here

Shortcuts vary as per your Android studio configuration but shortcuts are also written over the there too ease your task.

like image 34
ibhavikmakwana Avatar answered Sep 18 '22 04:09

ibhavikmakwana


In my case Ctrl+Alt+W hotkey in Android Studio didn't work. To fix it just open Keymap Settings and re-set this hotkey - Android Studio will remove conflicting shortcuts and it became work.

like image 34
Rodion Mostovoy Avatar answered Sep 21 '22 04:09

Rodion Mostovoy


You can do this easily with command Alt+Enter.

In this post it is very well explained.

like image 38
Jacin Montava Avatar answered Sep 19 '22 04:09

Jacin Montava