sort_child_properties_last
在 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 map 語法來設定 linter 規則,請在 linter > rules 下方新增 sort_child_properties_last: true
analysis_options.yaml
yaml
linter:
rules:
sort_child_properties_last: true
除非另有說明,否則本網站上的文件反映的是 Dart 3.7.1 版本。頁面最後更新於 2025-03-07。 檢視原始碼 或回報問題。