如果我有一个想要简化的复杂布局,Android Studio(或IntelliJ)中将小部件提取到方法中的快捷方式是什么?

例子:

我想提取堆栈中的三个主要小部件.

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,
            ),
          ),
        ),
      ],
    );
  }
}

我可以用手做,但我在找一条近路.

推荐答案

我正在为Android Studio和VS代码更新这个答案

Android Studio

方法1

快捷键:

  • Windows/Linux:将光标放在小部件名称上,然后按Ctrl+<Alt+M将其提取为方法,或按Ctrl+Alt+W.
  • 将光标放在小部件名称上,然后按Option+Command+M将其提取为方法,或按Option+Command+W将其提取为小部件.

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

方法2

也可以从Flutter 轮廓菜单将小工具提取到方法或新小工具中.

  1. 单击左上角的Flutter Outline
  2. 在大纲中 Select 小部件
  3. 右键单击并 Select Extract method...Extract widget...
  4. 给它起个名字

enter image description here

Visual Studio Code

将光标放在小部件名称上,然后按命令+或按住Ctrl键+在PC上.然后从关联菜单中 Select MethodWidget.

Flutter相关问答推荐

想更新ListView上的索引点击块Flutter

如何将Xpriter代码页更改为代码页27?

Flutter类调用另一个类中的另一个方法失败

Flutter记录返回类型问题

Flutter Riverpod 对提供者状态的变化做出react

「Flutter」错误:没有名为'kind'的命名参数

在flutter中从图库或相机中 Select 图像的方法

Android 模拟器在 Apple M2 芯片 EXC_BAD_ACCESS / KERN_INVALID_ADDRESS 上随机崩溃

如何设置行中项目之间的空格相等?

如何在 GridView.builder 中设置 textform 字段并在 flutter 中将 12 个 textformfield 作为单个字符串获取?

应用程序和应用程序内的 webview 之间的单点登录

如何建立最近的文件列表?

我想在一个屏幕上有两个相同区域的列表视图,我该如何设置?

Flutter:如何判断嵌套列表是否包含值

如何在 flutter 中制作这种类型的扩展瓦片

构建失败 - 无法解析 io.grpc:grpc-core:[1.28.0]. (是的,我已经升级到 mavenCentral())

使用 Chaquopy 插件后 Gradle Sync 无法正常工作

TextSpan 避免符号字符串组上的换行符

更新未知文档 ID firebase\flutter 中的字段

如何在Flutter 中将列表的模型类型转换为数组?