- Open VS Code and navigate to the "Preferences" menu (on Mac, this is under "Code"; on Windows, it's under "File").
- Select "User Snippets" from the dropdown menu.
- Select "dart.json" to open the Dart snippets file.
- Add the following code to the "stful" and "stless" snippets:
"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"
}
- Save the file.
Now, every time you use the "stful" or "stless" snippets to create a new widget, the import 'package:flutter/material.dart'; statement will be included by default.