網站首頁 編程語言 正文
前言
我們在首次使用內容類 App 的時候,不少都會讓我們選擇個人偏好。這種通常是通過標簽來實現,比如列舉出一系列的技術棧,然后讓我們選擇。通過這些標簽選擇可以預先知道用戶的偏好信息,從而可以選擇感興趣的內容進行推送,這樣會讓用戶快速看到想看的內容。我們本篇就來看看 Flutter 如何實現興趣標簽的選擇,這可以通過 InputChip
來輕松實現。
InputChip
InputChip
類是一個簡單的小組件,它的內容區左側有一個 avatar
子組件,右側是一個 label
組件。然后支持刪除和標記選中,因此非常適合做興趣標簽的選擇。下面是未選中和選中的兩個狀態,選中的時候會在左側的 avatar
區域打個勾表示選中。這個組件相比我們自己去寫一個類似的組件來說會簡化很多。而且,多個InputChip
組件可以作為Wrap
組件的子組件,實現多個 InputChip
時自動等間距排布和超出寬度自動換行,這也恰恰是做興趣標簽所需要的。
我們來看一下 InputChip
構造方法和主要屬性。
const InputChip({ Key? key, this.avatar, required this.label, this.labelStyle, this.labelPadding, this.selected = false, this.isEnabled = true, this.onSelected, this.deleteIcon, this.onDeleted, this.deleteIconColor, this.deleteButtonTooltipMessage, this.onPressed, this.pressElevation, this.disabledColor, this.selectedColor, this.tooltip, this.side, this.shape, this.clipBehavior = Clip.none, this.focusNode, this.autofocus = false, this.backgroundColor, this.padding, this.visualDensity, this.materialTapTargetSize, this.elevation, this.shadowColor, this.selectedShadowColor, this.showCheckmark, this.checkmarkColor, this.avatarBorder = const CircleBorder(), @Deprecated( 'Migrate to deleteButtonTooltipMessage. ' 'This feature was deprecated after v2.10.0-0.3.pre.' ) this.useDeleteButtonTooltip = true, })
屬性很多,但是實際用的是下面這幾個:
-
avatar
:左側的子組件,通常可以用使用圓形(如CircularAvatar
)組件,注意高度是不可改的,隨整個InputChip
的高度決定; -
label
:右側的標簽組件,通常是一個文本組件,支持單行或多行文本,該組件決定了InputChip
的高度; -
labelPadding
:標簽的內邊距; -
selected
:選中狀態,如果是選中狀態則會在左側有個打勾的標記; -
isEnabled
:是否啟用,默認是啟用狀態,如果禁用則選中事件的回調(onSelected
)和點擊事件回調(onPressed
)都無法使用,但是刪除是可以用的。 -
onSelected
:選中狀態改變時的回調函數。 -
deleteIcon
:刪除圖標,默認是Icons.cancel
圖標。 -
onDeleted
:刪除事件回調。 -
onPressed
:點擊事件回調; -
backgroundColor
和selectedColor
:默認背景色和選中后背景色。
通過這些屬性我們就可以構建基礎的興趣標簽,比如下面的代碼,這里的 item 是標簽的數據實體對象,有三個屬性,分別是標簽名稱 name
,標簽默認背景色color
和選中狀態 selected
。 當標簽選中后我們將 InputChip
的avatar
設置為 null
,從而不顯示 avatar
。
InputChip( labelPadding: const EdgeInsets.fromLTRB(10, 0, 10, 0), backgroundColor: item.color, selectedColor: Colors.red[400], selected: item.selected, onSelected: (isSelected) { setState(() { item.selected = isSelected; }); }, avatar: item.selected ? null : CircleAvatar( backgroundColor: Colors.lightBlue, child: Text( item.name.substring(0, 1), style: const TextStyle(color: Colors.white), ), ), label: Text( item.name, ), )
興趣標簽選擇實現
興趣標簽通常會有多個,這時候需要逐個等間距排開,超出寬度后換行。這個可以通過 Wrap
組件和 InputChip
組件實現。代碼非常簡單,就是將一組 InputChip
組件作為 Wrap
組件的 children
參數,然后設置 Wrap
中子組件的間距即可。
Wrap( spacing: 10.0, children: _techList .map( (item) => InputChip( labelPadding: const EdgeInsets.fromLTRB(10, 0, 10, 0), backgroundColor: item.color, selectedColor: Colors.red[400], selected: item.selected, onSelected: (isSelected) { setState(() { item.selected = isSelected; }); }, avatar: item.selected ? null : CircleAvatar( backgroundColor: Colors.lightBlue, child: Text( item.name.substring(0, 1), style: const TextStyle(color: Colors.white), ), ), label: Text( item.name, ), ), ) .toList(), ),
最終我們實現的效果如下圖所示。
總結
本篇介紹了 Flutter 中的 InputChip
組件的使用,同時給出了如何用 InputChip
實現興趣標簽的選擇。在實際應用中,InputChip
還可以用于多選,在聊天會評論列表展示用戶信息(頭像加昵稱)。可以看到,InputChip 是個非常小巧但很實用的組件。
原文鏈接:https://juejin.cn/post/7163059109665701902
相關推薦
- 2022-09-25 Linux基礎組件之基于循環數組的無鎖隊列
- 2022-04-24 淺析GBase8s?唯一索引與非唯一索引問題_數據庫其它
- 2022-12-09 C++?opencv學習之圖像像素的邏輯操作_C 語言
- 2023-01-17 Golang?map實現原理淺析_Golang
- 2022-03-22 C語言寫一個散列表_C 語言
- 2022-03-29 基于Python+Tkinter實現一個簡易計算器_python
- 2022-11-28 協程作用域概念迭代RxTask?實現自主控制_Android
- 2022-07-21 CSS復合型寫法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支