从列表生成自动换行内的按钮 flutter

问题描述 投票:0回答:3

我有一个List<String>,我想在一个Wrap小部件内生成N Buttons(长度为N的字符串)。

我读到一个List.generate,我正在尝试实现它,你知道我该怎么做吗?

flutter dart flutter-layout
3个回答
1
投票

0
投票
List<Widget> _ButtonsList(){ List<String> list = _getLanguages(); List listings = new List<Widget>(); int lenght = list.length; for (int i=0;i<=lenght; i++){ listings.add(_getrandomButton(list[i])); } return listings; } Widget _myWrap(){ Wrap( spacing: 12, children: _ButtonsList(), ); }

但是它说我在list.split上叫了null

Future<String> _read(String filename) async {
  try {
  final file = await _localFile(filename);

// Read the file.
return await file.readAsString();
} catch (e) {
// If encountering an error, return 0.
return "Can't read";
 }
}

String _myRead( String filename){
var _bio;

_read(filename).then((String) {
  _bio = String;
 });
 return _bio;
}

List<String> _getLanguages() {
 String _bio= _myRead("bio.txt");
 List<String> list =_bio.split(",");
 return list;
}

这是"bio.txt"内部的内容:英文,意大利文,中文


0
投票
final strings = ['A', 'B']; final buttons = List<Widget>.generate( strings.length, (i) => RaisedButton( onPressed: null, child: Text(strings[i]), ), ).toList();

然后将它们直接用作Wrap的子代:

Wrap(children: buttons)
© www.soinside.com 2019 - 2024. All rights reserved.