以以下函数为例:
List<Widget> getListFiles() {
List<Widget> list = [
Container(),
Container(),
Container(),
];
return list;
}
Run Code Online (Sandbox Code Playgroud)
如何插入子参数?
Column(
children: <Widget>
[
Text(),
Text(),
getListFiles(), <---
Text(),
]
)
Run Code Online (Sandbox Code Playgroud)
mir*_*cal 14
更新
现在 Dart 已经在 2.3 版中使用了扩展运算符。
[
...anotherList
item1
]
Run Code Online (Sandbox Code Playgroud)
没有扩展运算符的答案
我认为您需要传播您的列表,因为您无法将元素类型“列表”分配给列表类型“小部件”。Spread operator是一个想要的功能。您可以在此处关注有关它的问题。
同时您可以使用generators和yield操作符。
Column(
children: List.unmodifiable(() sync* {
yield Text();
yield Text();
yield* getListFiles();
yield Text();
}()),
);
Run Code Online (Sandbox Code Playgroud)
小部件中使用的完整代码。
import 'package:flutter/material.dart';
class App extends StatelessWidget {
List<Widget> getListFiles() {
List<Widget> list = [Text('hello'), Text('hello1'), Text('hello2')];
return list;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stackoverflow'),
),
body: Column(
children: List.unmodifiable(() sync* {
yield Text('a');
yield Text('b');
yield* getListFiles();
yield Text('c');
}()),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
现在你可以使用展开运算符 - 从 dart 2.3.0 开始
Column(
children: <Widget>
[
Text(),
Text(),
...getListFiles(), <---
Text(),
]
)
Run Code Online (Sandbox Code Playgroud)
此外,您可能需要将 pubspec.yaml 中的最低 SDK 级别更改为 2.3.0
| 归档时间: |
|
| 查看次数: |
4726 次 |
| 最近记录: |