Visual Studio Code Flutter 格式不适用于我的缩进空间值

Bur*_*nal 9 indentation dart visual-studio-code flutter

我浪费了我的一整天,我不明白发生了什么。我正在使用 Visual Studio Code 1.40.2 并且正在学习 Flutter 3.60。有时 Flutter 代码会因为缩进空间而变得不可读。我只想创建更多空间(缩进空间),但是当我使用格式选项时,标签大小又变成了 2。我看了太多的网站,包括 Stackoverflow,不幸的是我没有找到解决方案。它变得很烦人。这是我的配置文件:(感谢帮助)

{
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
"editor.fontSize": 18,
"editor.fontFamily": "Consolas, 'Courier New', monospace, ",
"dart.openDevTools": "flutter",
"workbench.colorTheme": "Night Owl (No Italics)",
"workbench.iconTheme": "material-icon-theme",
"editor.fastScrollSensitivity": 8,
"editor.tabSize": 8,
"editor.insertSpaces": true,
"editor.wordWrap": "on",
"editor.smoothScrolling": true,
"editor.cursorBlinking": "expand",
"editor.cursorSmoothCaretAnimation": true,
"editor.fontWeight": "400",
"outline.showFields": false,

"[dart]": {
    "editor.tabSize": 6,
    "editor.insertSpaces": true,
    "editor.detectIndentation": false,
},
Run Code Online (Sandbox Code Playgroud)

}

  • 我改变了 editor.insertSpaces false 和 true 并没有任何改变。
  • editor.detectIndentation true 或 false 不起作用。
  • 我添加了这个块但没有用。

    "[flutter]": { "editor.tabSize": 6, "editor.insertSpaces": true, "editor.detectIndentation": false, },

这是我的简单代码:

import 'package:flutter/material.dart';

class GridListe extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GridView.count(
        crossAxisCount: 3,
        primary: false,
        padding: EdgeInsets.all(10),
        crossAxisSpacing: 20,
        mainAxisSpacing: 40,
        children: <Widget>[
            Container(
                alignment: Alignment.center,
                color: Colors.teal,
                child: Text(
                "Salam",
                textAlign: TextAlign.center,
                ),
            ),
        ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

当我使用格式代码 (Shift + alt + p) 代码缩进空格或制表符大小变为 2 时,它会让我发疯。

import 'package:flutter/material.dart';

class GridListe extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GridView.count(
      crossAxisCount: 3,
      primary: false,
      padding: EdgeInsets.all(10),
      crossAxisSpacing: 20,
      mainAxisSpacing: 40,
      children: <Widget>[
        Container(
          alignment: Alignment.center,
          color: Colors.teal,
          child: Text(
            "Salam",
            textAlign: TextAlign.center,
          ),
        ),
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Hal*_*ynh 56

在 settings.json 文件中添加这个

"[dart]": {
   "editor.defaultFormatter": "Dart-Code.dart-code",
   "editor.formatOnSave": true
},
Run Code Online (Sandbox Code Playgroud)


For*_*kie 6

只需将以下代码添加到您的 ProjectDir/ProjectName/.vscode/settings.json

   {
        "editor.defaultFormatter": "Dart-Code.dart-code",
        "editor.formatOnSave": true
 }
Run Code Online (Sandbox Code Playgroud)


Dan*_*eny 1

VS Code 的 Dart 扩展使用 Dart SDK ( dart_style ) 中的格式化程序,该格式化程序不支持自定义缩进选项(按设计),因此它将始终使用 2 个空格。

如果您希望手动格式化,可以禁用内置格式化程序,并且其他 VS Code 扩展也可以为 Dart 提供格式化程序 - 尽管据我所知,还没有创建任何格式化程序(尽管我已经如果有人有兴趣创建 VS Code 扩展来执行此操作,则会在此问题上提供帮助)。