跳到主要内容

sort_child_properties_last

稳定
Flutter
可修复

在 Widget 实例创建中将 child 属性排序在最后。

详情

#

在 Widget 实例创建中将 child 属性排序在最后。这提高了可读性,并且在使用 IDE 中的 UI as Code 可视化以及编辑器(例如 IntelliJ)中的 UI as Code 指南时,效果最佳,在这些环境中,正确顺序的属性清晰地与构造函数调用关联,并与 children 分隔开。

错误示例

dart
return Scaffold(
  appBar: AppBar(
    title: Text(widget.title),
  ),
  body: Center(
    child: Column(
      children: <Widget>[
        Text(
          'You have pushed the button this many times:',
         ),
        Text(
          '$_counter',
          style: Theme.of(context).textTheme.display1,
         ),
      ],
      mainAxisAlignment: MainAxisAlignment.center,
    ),
    widthFactor: 0.5,
  ),
  floatingActionButton: FloatingActionButton(
    child: Icon(Icons.add),
    onPressed: _incrementCounter,
    tooltip: 'Increment',
  ),
);

正确示例

dart
return Scaffold(
  appBar: AppBar(
    title: Text(widget.title),
  ),
  body: Center(
    widthFactor: 0.5,
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text(
          'You have pushed the button this many times:',
         ),
        Text(
          '$_counter',
          style: Theme.of(context).textTheme.display1,
         ),
      ],
    ),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: Icon(Icons.add),
  ),
);

例外:允许在 child 属性之后使用带有函数表达式的参数。

启用

#

要启用 sort_child_properties_last 规则,请在你的 analysis_options.yaml 文件中的 linter > rules 下添加 sort_child_properties_last

analysis_options.yaml
yaml
linter:
  rules:
    - sort_child_properties_last

如果你改为使用 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 sort_child_properties_last: true

analysis_options.yaml
yaml
linter:
  rules:
    sort_child_properties_last: true