Flutter组件之ChoiceChip教程

简介

ChoiceChip 代表一个单一的选择,一般用于从一组选项中进行单选。

最简单演示

未选中

  • 未选中 - code
1
2
3
4
5
6
Center(
child: ChoiceChip(
label: Text("This is ChoiceChip"),
selected: false,
),
),
  • 未选中 - 效果

    image-20200426175043067

选中

  • 选中 - code
1
2
3
4
5
6
Center(
child: ChoiceChip(
label: Text("This is ChoiceChip"),
selected: true,
),
),
  • 选中 - 效果

image-20200426174748562

常用属性

属性名 是否必须 描述
label Y 标签的文字描述
selected Y 是否选中
avatar N 左侧小图标
labelStyle N 标签文字的样式
selectedColor N 选中时的颜色
selectedShadowColor N 选中时的阴影颜色
elevation N 阴影大小
  • code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Center(
child: ChoiceChip(
label: Text("This is ChoiceChip"),
labelStyle: TextStyle(
color: Colors.black,
fontSize: 16
),
selected: true,
avatar: Image.asset("assets/images/flutter.png"),
selectedColor: Colors.orangeAccent.withAlpha(39),
selectedShadowColor: Colors.orangeAccent,
elevation: 30,
),
)
  • 效果

    image-20200426181643219

列表中单选使用举例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class MyThreeOptions extends StatefulWidget {
@override
_MyThreeOptionsState createState() => _MyThreeOptionsState();
}

class _MyThreeOptionsState extends State<MyThreeOptions> {
int _value = 1;

@override
Widget build(BuildContext context) {
return Wrap(
children: List<Widget>.generate(
3,
(int index) {
return ChoiceChip(
label: Text('Item $index'),
avatar: Image.asset("assets/images/flutter.png"),
selectedColor: Colors.orangeAccent.withAlpha(39),
selectedShadowColor: Colors.orangeAccent,
elevation: 3,
selected: _value == index,
onSelected: (bool selected) {
setState(() {
_value = selected ? index : null;
});
},
);
},
).toList(),
);
}
}
  • 效果

    choice-chip

文章作者: 普通程序员
文章链接: https://programmerauthor.github.io/2020/04/26/choice-chip/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 普通程序员