当我想使用 GridView
制作类似 Tags Widget
时遇到问题,
查看结果:
但是按钮的宽度是根据 GridView
包装自动设置的。
如何动态设置宽度
?
任何答案将不胜感激。
最佳答案
class MyHomePage extends StatelessWidget {
var tags = [
"love",
"instagood",
"photooftheday",
"beautiful",
"fashion",
"happy",
"tbt",
"cute",
"followme",
"like4like",
"follow",
"picoftheday",
"me",
"selfie",
"summer",
"instadaily"
];
var selected_tags = ["love", "me", "summer"];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Wrap(
spacing: 8.0, // gap between adjacent chips
runSpacing: 4.0, // gap between lines
children: <Widget>[...generate_tags()],
),
);
}
generate_tags() {
return tags.map((tag) => get_chip(tag)).toList();
}
get_chip(name) {
return FilterChip(
selected: selected_tags.contains(name),
selectedColor: Colors.blue.shade800,
disabledColor: Colors.blue.shade400,
labelStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
label: Text("#${name}"));
}
}
关于gridview - flutter - 在 gridview 上换行时如何设置宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56585958/