dart flutter:VS代码中的包自动导入问题

dgtucam1  于 2023-04-27  发布在  Flutter
关注(0)|答案(1)|浏览(248)

当我在任何flutter应用中创建一个新的Stateless或statefull类时,VScode会自动导入以下三个包:

import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/container.dart'; 
import 'package:flutter/src/widgets/framework.dart';

那么如何配置VScode只导入材质包呢:

import 'package:flutter/material.dart';

我尝试在IDE中更改Flutter和 dart 设置,但没有任何变化。

x6h2sr28

x6h2sr281#

1.打开VS Code,导航到“首选项”菜单(在Mac上,这是在“代码”下;在Windows上,它在“文件”下)。
1.从下拉菜单中选择“用户代码段”。
1.选择“dart.json”打开Dart片段文件。
1.将以下代码添加到“stful”和“stless”代码段:

"stful": {
        "prefix": "stful",
        "body": [
            "import 'package:flutter/material.dart';",
            "",
            "class ${1:MyWidget} extends StatefulWidget {",
            "const ${1:MyWidget}({super.key});",
            "",
            "  @override",
            "   State<${1:MyWidget}> createState() => _${1:MyWidget}State();",
            "}",
            "",
            "class _${1:MyWidget}State extends State<${1:MyWidget}> {",
            "  @override",
            "  Widget build(BuildContext context) {",
            "    return Scaffold(",
            "    );",
            "  }",
            "}"
        ],
        "description": "Stateful widget template"
    },
    "stless": {
        "prefix": "stless",
        "body": [
            "import 'package:flutter/material.dart';",
            "",
            "class ${1:MyWidget} extends StatelessWidget {",
            " const ${1:MyWidget}({super.key});",
            "",
            "  @override",
            "  Widget build(BuildContext context) {",
            "    return Scaffold(",
            "    );",
            "  }",
            "}"
        ],
        "description": "Stateless widget template"
    }

1.保存文件。现在,每次使用“stful”或“stless”片段创建新的小部件时,导入'package:flutter/material. dart';默认情况下将包括语句。

相关问题