網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
概述:
Flutter 標(biāo)簽類控件大全ChipFlutter內(nèi)置了多個(gè)標(biāo)簽類控件,但本質(zhì)上它們都是同一個(gè)控件,只不過(guò)是屬性參數(shù)不同而已,在學(xué)習(xí)的過(guò)程中可以將其放在放在一起學(xué)習(xí),方便記憶。
RawChip
Material風(fēng)格標(biāo)簽控件,此控件是其他標(biāo)簽控件的基類,通常情況下,不會(huì)直接創(chuàng)建此控件,而是使用如下控件:
- Chip
- InputChip
- ChoiceChip
- FilterChip
- ActionChip
如果你想自定義標(biāo)簽類控件,通常使用此控件。
RawChip可以通過(guò)設(shè)置onSelected
被選中,設(shè)置onDeleted
被刪除,也可以通過(guò)設(shè)置onPressed
而像一個(gè)按鈕,它有一個(gè)label
屬性,有一個(gè)前置(avatar
)和后置圖標(biāo)(deleteIcon
)。
RawChip(label: Text('RawChip')),
效果:
設(shè)置左側(cè)控件,一般是圖標(biāo):
RawChip( avatar: CircleAvatar(child: Text('R'),), label: Text('RawChip'), isEnabled: false,//禁止點(diǎn)選狀態(tài) ),
設(shè)置label的樣式和內(nèi)邊距:
RawChip( avatar: CircleAvatar(child: Text('R'),), label: Text('RawChip'), // isEnabled: false,//禁止點(diǎn)選狀態(tài) labelPadding: EdgeInsets.symmetric(horizontal: 20), padding: EdgeInsets.only(left: 10,right: 10,top: 5), ),
設(shè)置刪除相關(guān)屬性:
RawChip( label: Text('RawChip'), onDeleted: (){ print('onDeleted'); }, deleteIcon: Icon(Icons.delete), deleteIconColor: Colors.red, deleteButtonTooltipMessage: "刪除", // isEnabled: false,//禁止點(diǎn)選狀態(tài) labelPadding: EdgeInsets.symmetric(horizontal: 10), padding: EdgeInsets.only(left: 10,right: 10,top: 5,bottom: 5), ),
設(shè)置形狀、背景顏色及內(nèi)邊距,陰影:
RawChip( label: Text('RawChip'), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)), backgroundColor: Colors.blue, padding: EdgeInsets.symmetric(vertical: 10), elevation: 8, shadowColor: Colors.grey, )
materialTapTargetSize是配置組件點(diǎn)擊區(qū)域大小的屬性,很多組件都有此屬性,比如:
[FloatingActionButton], only the mini tap target size is increased. * [MaterialButton] * [OutlineButton] * [FlatButton] * [RaisedButton] * [TimePicker] * [SnackBar] * [Chip] * [RawChip] * [InputChip] * [ChoiceChip] * [FilterChip] * [ActionChip] * [Radio] * [Switch] * [Checkbox]
MaterialTapTargetSize有2個(gè)值,分別為:
- padded:最小點(diǎn)擊區(qū)域?yàn)?8*48。
- shrinkWrap:子組件的實(shí)際大小。
設(shè)置選中狀態(tài)、顏色:
RawChip( label: Text('RawChip'), selected: _selected, onSelected: (v){ setState(() { _selected =v; }); }, selectedColor: Colors.blue, selectedShadowColor: Colors.red, )
Chip
Chip是一個(gè)簡(jiǎn)單的標(biāo)簽控件,僅顯示信息和刪除
相關(guān)屬性,是一個(gè)簡(jiǎn)化版的RawChip,用法和RawChip一樣。源代碼如下:
@override Widget build(BuildContext context) { assert(debugCheckHasMaterial(context)); return RawChip( avatar: avatar, label: label, labelStyle: labelStyle, labelPadding: labelPadding, deleteIcon: deleteIcon, onDeleted: onDeleted, deleteIconColor: deleteIconColor, deleteButtonTooltipMessage: deleteButtonTooltipMessage, tapEnabled: false, shape: shape, clipBehavior: clipBehavior, focusNode: focusNode, autofocus: autofocus, backgroundColor: backgroundColor, padding: padding, materialTapTargetSize: materialTapTargetSize, elevation: elevation, shadowColor: shadowColor, isEnabled: true, ); }
InputChip
以緊湊的形式表示一條復(fù)雜的信息,例如實(shí)體(人,地方或事物)或?qū)υ捨谋尽?/p>
InputChip 本質(zhì)上也是RawChip,用法和RawChip一樣。源代碼如下:
override Widget build(BuildContext context) { assert(debugCheckHasMaterial(context)); return RawChip( avatar: avatar, label: label, labelStyle: labelStyle, labelPadding: labelPadding, deleteIcon: deleteIcon, onDeleted: onDeleted, deleteIconColor: deleteIconColor, deleteButtonTooltipMessage: deleteButtonTooltipMessage, onSelected: onSelected, onPressed: onPressed, pressElevation: pressElevation, selected: selected, tapEnabled: true, disabledColor: disabledColor, selectedColor: selectedColor, tooltip: tooltip, shape: shape, clipBehavior: clipBehavior, focusNode: focusNode, autofocus: autofocus, backgroundColor: backgroundColor, padding: padding, materialTapTargetSize: materialTapTargetSize, elevation: elevation, shadowColor: shadowColor, selectedShadowColor: selectedShadowColor, showCheckmark: showCheckmark, checkmarkColor: checkmarkColor, isEnabled: isEnabled && (onSelected != null || onDeleted != null || onPressed != null), avatarBorder: avatarBorder, ); }
基本用法:
InputChip( avatar: CircleAvatar( radius: 12.0, ), label: Text( 'InputChip', style: TextStyle(fontSize: 12.0), ), shadowColor: Colors.grey, deleteIcon: Icon( Icons.close, color: Colors.black54, size: 14.0, ), onDeleted: () { print('onDeleted'); }, onSelected: (bool selected) { setState(() { _selected = selected; }); }, selectedColor: Colors.orange, disabledColor: Colors.grey, selected: _selected, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, labelStyle: TextStyle(color: Colors.black54), ),
ChoiceChip
允許從一組選項(xiàng)中進(jìn)行單個(gè)選擇,創(chuàng)建一個(gè)類似于單選按鈕的標(biāo)簽,本質(zhì)上ChoiceChip也是一個(gè)RawChip,ChoiceChip本身不具備單選屬性。
int _selectedIndex = 0; Wrap( spacing: 5, children: List.generate(20, (index){ return ChoiceChip( label: Text('測(cè)試 $index'), selected: _selectedIndex==index, onSelected: (v){ setState(() { _selectedIndex =index; }); }, ); }).toList(), )
FilterChip
FilterChip可以作為過(guò)濾標(biāo)簽,本質(zhì)上也是一個(gè)RawChip,用法如下:
List<String> _filters = []; _buildFilterChip(){ return Column( children: [ Wrap( spacing: 15, children: List.generate(10, (index) { return FilterChip( label: Text('測(cè)試 $index'), selected: _filters.contains('$index'), onSelected: (v) { setState(() { if(v){ _filters.add('$index'); }else{ _filters.removeWhere((f){ return f == '$index'; }); } }); }, ); }).toList(), ), Text('選中:${_filters.join(',')}'), ], ); }
運(yùn)行效果:
總結(jié):
本篇主要講了以下幾種chip組件的用法案例:
- RawChip:是Material風(fēng)格標(biāo)簽控件,此控件是其他標(biāo)簽控件的基類,通常情況下,不會(huì)直接創(chuàng)建此控件,而是使用其他的標(biāo)簽控件。
- InputChip:以緊湊的形式表示一條復(fù)雜的信息,例如實(shí)體(人,地方或事物)或?qū)υ捨谋尽nputChip 本質(zhì)上也是RawChip,用法和RawChip一樣
- ChoiceChip:允許從一組選項(xiàng)中進(jìn)行單個(gè)選擇,創(chuàng)建一個(gè)類似于單選按鈕的標(biāo)簽,本質(zhì)上ChoiceChip也是一個(gè)RawChip,ChoiceChip本身不具備單選屬性。
- FilterChip:可以作為過(guò)濾標(biāo)簽,本質(zhì)上也是一個(gè)RawChip
- ActionChip:顯示與主要內(nèi)容有關(guān)的一組動(dòng)作,本質(zhì)上也是一個(gè)RawChip
- Chip:一個(gè)簡(jiǎn)單的標(biāo)簽控件,僅顯示信息和刪除相關(guān)屬性,是一個(gè)簡(jiǎn)化版的RawChip,用法和RawChip一樣
原文鏈接:https://juejin.cn/post/7155685641341108231
相關(guān)推薦
- 2022-11-23 Python面向?qū)ο蟮膬?nèi)置方法梳理講解_python
- 2022-05-06 Python如何判斷字符串是否僅包含數(shù)字_python
- 2022-08-21 C#?Any()和AII()方法的區(qū)別_C#教程
- 2022-11-14 flutter中使用流式布局示例詳解_Android
- 2023-04-12 在redis中防止消息丟失的機(jī)制_Redis
- 2022-09-18 ASP.NET實(shí)現(xiàn)文件上傳功能_實(shí)用技巧
- 2022-08-27 一文讀懂Android?Kotlin的數(shù)據(jù)流_Android
- 2022-05-21 python實(shí)現(xiàn)會(huì)員信息管理系統(tǒng)(List)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支