【发布时间】:2020-08-04 05:52:00
【问题描述】:
我连续有文本和一个开关,数据很好但布局不好。我尝试了 mainAxisAlignement.spaceAround 或 mainAxisAlignement.spaceBetween 或 mainAxisAlignement.spaceEvenly 但由于 text 的大小,项目列表未对齐锯齿形。我已经实现如下
标题小部件
return Container(
margin: EdgeInsets.only(top: 20),
width: MediaQuery.of(context).size.width,
height: 50,
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 5.0,
color: Color.fromRGBO(232, 232, 232, 1),
)),
color: Colors.grey),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Spacer(),
Text("S.N"),
Spacer(),
Text("Food Name"),
Spacer(),
Text("Price"),
Spacer(),
Text("Qty"),
Spacer(),
Text("Action"),
Spacer(),
]),
);
列表项
return Container(
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
border:
Border(bottom: BorderSide(width: 5.0, color: Colors.grey[300])),
color: Colors.white,
),
child: Wrap(
children: <Widget>[
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(child: Flexible(child: Text((i + 1).toString()))),
Container(
child: Flexible(
child: Text(
removeAllHtmlTags(menuList[i].name),
softWrap: true,
)),
),
Container(
child: Flexible(
child: Text(
removeAllHtmlTags(menuList[i].discountPrice.toString()),
),
),
),
Container(
child: Flexible(
child: Text(
menuList[i].maxQty.toString(),
),
),
),
Container(
width: 100,
child: menuList[i].status == 0
? Text(
menuList[i].foodStatus,
style: TextStyle(color: Colors.red),
)
: YourListViewItem(
id: menuList[i].id,
index: menuList[i].status,
),
),
],
)
],
));
YourListViewItem 小部件
return ListTile(
trailing: new Switch(
value: isSwitched,
activeColor: Colors.green,
activeTrackColor: Colors.green,
inactiveThumbColor: Colors.white,
inactiveTrackColor: Colors.grey,
onChanged: (value) {
setState(() {
if (isSwitched) {
isSwitched = false;
isSwitched = value;
changeFoodStatus(widget.id);
} else {
isSwitched = true;
isSwitched = value;
changeFoodStatus(widget.id);
}
});
},
),
);
【问题讨论】:
-
您是在为
header使用Table小部件还是Row小部件? -
我使用了行小部件,我已经编辑了我的问题
-
您可以尝试为 erow 中的每个元素赋予权重,并使用扩展的小部件作为食物名称
-
您可能需要考虑使用
Table小部件来轻松满足各种设备尺寸的需求。 api.flutter.dev/flutter/widgets/Table-class.html -
我也尝试过使用扩展 @AnirudhBagri
标签: flutter