Flutter 项目实战 07 自定义单选按钮 封装

老规矩,直接上代码

List list2 = [
{
“title”: “新天鹅堡”,
“type”: 0,
},
{“title”: “高天鹅堡”, “type”: 1},
{“title”: “新天鹅堡+高天鹅堡”, “type”: 2},
{“title”: “巴伐利亚国家博物馆”, “type”: 3},
];

new GridView.count(
padding: EdgeInsets.all(5.0),
//一行多少个
crossAxisCount: 2,
//滚动方向
scrollDirection: Axis.vertical,
// 左右间隔
crossAxisSpacing: 10.0,
// 上下间隔
mainAxisSpacing: 15.0,
//宽高比
childAspectRatio: 1 / 0.22,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
children: this.list2.map((value) {
return getTickListitem(this.groupValue, context, value,
// ignore: missing_return
updateGroupValue: (v) {
setState(() {
this.groupValue = v;
});
});
}).toList(),
),

Widget getTickListitem(int groupValue, BuildContext context, value,
{Function updateGroupValue(int v)}) {

print(value[‘type’]);
return groupValue == value[‘type’]
? FlatButton(
color: Color(0x0D00A0E8),
shape: new RoundedRectangleBorder(
side: new BorderSide(
color: FHColors.btnColorBlue,
),
borderRadius: BorderRadius.circular(FHAdapt.px(6)),
),
onPressed: () {
print(‘切换KaTeX parse error: Expected 'EOF', got '}' at position 67: …']); }̲, chi…{value}’);
updateGroupValue(value[‘type’]);
},
shape: new RoundedRectangleBorder(
borderRadius: BorderRadius.circular(FHAdapt.px(6)),
),
child: FHText(
value[‘title’],
style: TextStyle(
color: FHColors.textColorTheme,
fontSize: fontSizeT(value[‘title’]),
fontWeight: FontWeight.w400),
),
),
);
}

这里做了一个文字的简单适配,还需要修改
double fontSizeT (String vv){
double size = 0.0;
if(vv.length > 8){
size = FHAdapt.px(27);
}else{
size = FHAdapt.px(28);
}

return size;
}

你可能感兴趣的:(Flutter)