对于googler,我写了一个简单的Stateless Widget,其中包含本SO中提到的3种方法。希望这使它更容易理解。
import 'package:flutter/material.dart';
class ListAndFP extends StatelessWidget {
final List<String> items = ['apple', 'banana', 'orange', 'lemon'];
Widget method1() {
return Column(
children: <Widget>[
Text('You can put other Widgets here'),
for (var item in items) Text(item),
],
);
}
Widget method2() {
return Column(
children: <Widget>[
Text('You can put other Widgets here'),
...items.map((item) => Text(item)).toList(),
],
);
}
Widget method3() {
return Column(
children: items.map((item) => Text(item)).toList(),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: method1(),
);
}
}