[英]How to automatically add material.dart when creating new stateless or stateful class in flutter
When I open a new file and use a stateless or stateful widget template, I want to customise VS Code to add material.dart instead of the two imports it automatically adds which are framework.dart and container.dart.当我打开一个新文件并使用无状态或有状态小部件模板时,我想自定义 VS Code 以添加 material.dart 而不是它自动添加的两个导入,即 framework.dart 和 container.dart。 My current solution is either delete these imported libraries and use quick fix to get the option to add material.dart or add the package manually myself.我当前的解决方案是删除这些导入的库并使用快速修复来获取添加 material.dart 的选项,或者我自己手动添加 package。 Can I tailor VS Code to include my desired package when I use the templates?我可以在使用模板时定制 VS Code 以包含我想要的 package 吗?
You need an extension like Awesome Flutter Snippets .您需要像Awesome Flutter Snippets这样的扩展。 It provides many snippets to generate classes.它提供了许多片段来生成类。 You only have to write statefulW
or statelessW
and press Tab
to generate the class.你只需要写statefulW
或者statelessW
然后按Tab
就可以生成class。
If you use mateapp
, an stateless class with a MaterialApp
widget and the Material
import is generated.如果您使用mateapp
,则会生成带有MaterialApp
小部件和Material
导入的无状态 class。 Like this:像这样:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
appBar: AppBar(
title: const Text('Material App Bar'),
),
body: const Center(
child: Text('Hello World'),
),
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.