我RaisedButton
在 Wrap 中有一大堆s,因此它们可以根据需要占用尽可能多的行。但我确实想将按钮扩展为方形按钮。我可以通过用Wrap
a替换GridView.count
并使用来做到这一点crossAxisCount
,但是当有更多可用空间时,按钮会变得不必要地大。基本上,我希望它们都是相同大小的正方形,大小都与它们所容纳的内容一样大。它们不是动态加载的或任何东西,所以不用担心性能。但是我们正在研究相当小的屏幕的可能性,因此滚动也需要是可能的。有没有办法让所有这些事情都正确?
这是当前代码及其生成的内容:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Wrap(
direction: Axis.horizontal,
children: <Widget>[
RaisedButton.icon(
onPressed: () {}
label: Text('Park In', style: TextStyle(fontSize: 15.0)),
icon: Icon(Icons.add),
),
RaisedButton.icon(
onPressed: () {}
label: Text('Park Out', style: TextStyle(fontSize: 15.0)),
icon: Icon(Icons.eject),
),
RaisedButton.icon(
onPressed: () {}
label: Text('Maintainence In', style: TextStyle(fontSize: 15.0)),
icon: Icon(Icons.vertical_align_bottom),
),
RaisedButton.icon(
onPressed: () {}
label: Text('Maintainence Out', style: TextStyle(fontSize: 15.0)),
icon: Icon(Icons.vertical_align_top),
),
RaisedButton.icon(
onPressed: null,
label: Text('Move', style: TextStyle(fontSize: 15.0)),
icon: Icon(Icons.open_with),
),
],
),
);
}
用 2 替换 GridView 作为crossAxisCount
给出这个,这非常接近我需要的(理想情况下,文本会更大并换行 - 如果没有给定正确的空间,它似乎会溢出,但我想我可以处理它时到此为止):
但是,例如,当我进入横向模式时,很容易在一行中放置 3 个按钮,GridView 只是“嗯,随便”,并使按钮变得巨大:
你可以使用一个OrientationBuilder
。它将为您处理方向变化:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("word"),
),
body: OrientationBuilder(
builder: (context, orientation) {
int count = 2;
if(orientation == Orientation.landscape){
count = 3;
}
return GridView.count(
crossAxisCount: count,
children: <Widget>[
RaisedButton.icon(
onPressed: () {},
label: Text('Park In', style: TextStyle(fontSize: 15.0)),
icon: Icon(Icons.add),
),
RaisedButton.icon(
onPressed: () {},
label: Text('Park Out', style: TextStyle(fontSize: 15.0)),
icon: Icon(Icons.eject),
),
RaisedButton.icon(
onPressed: () {},
label:
Text('Maintainence In', style: TextStyle(fontSize: 15.0)),
icon: Icon(Icons.vertical_align_bottom),
),
RaisedButton.icon(
onPressed: () {},
label:
Text('Maintainence Out', style: TextStyle(fontSize: 15.0)),
icon: Icon(Icons.vertical_align_top),
),
RaisedButton.icon(
onPressed: null,
label: Text('Move', style: TextStyle(fontSize: 15.0)),
icon: Icon(Icons.open_with),
),
],
);
},
),
);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句