简体   繁体   中英

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. 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. Can I tailor VS Code to include my desired package when I use the templates?

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

You need an extension like 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.

If you use mateapp , an stateless class with a MaterialApp widget and the Material import is generated. 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'),
        ),
      ),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM