Flutter 標籤類控件大全Chip

老孟導讀:Flutter內置了多個標籤類控件,但本質上它們都是同一個控件,只不過是屬性參數不一樣而已,在學習的過程當中能夠將其放在放在一塊兒學習,方便記憶。html

RawChip

Material風格標籤控件,此控件是其餘標籤控件的基類,一般狀況下,不會直接建立此控件,而是使用以下控件:git

  • Chip
  • InputChip
  • ChoiceChip
  • FilterChip
  • ActionChip

若是你想自定義標籤類控件時一般使用此控件。程序員

RawChip能夠經過設置onSelected被選中,設置onDeleted被刪除,也能夠經過設置onPressed而像一個按鈕,它有一個label屬性,有一個前置(avatar)和後置圖標(deleteIcon)。微信

基本用法以下:ide

RawChip(
  label: Text('老孟'),
)複製代碼

效果以下:學習

禁用狀態設置:ui

RawChip(
  label: Text('老孟'),
  isEnabled: false,
)複製代碼

效果以下:spa

設置左側控件,通常是圖標:debug

RawChip(
  avatar: CircleAvatar(
    child: Text('孟'),
  ),
  label: Text('老孟'),
)複製代碼

效果以下:3d

設置label的樣式和內邊距:

RawChip(
  label: Text('老孟'),
  labelStyle: TextStyle(color: Colors.blue),
  labelPadding: EdgeInsets.symmetric(horizontal: 10),
)複製代碼

效果以下:

設置刪除相關屬性:

RawChip(
  label: Text('老孟'),
  onDeleted: (){
    print('onDeleted');
  },
  deleteIcon: Icon(Icons.delete),
  deleteIconColor: Colors.red,
  deleteButtonTooltipMessage: '刪除',
)複製代碼

效果以下:

點擊刪除圖標,回調onDeleted

設置形狀、背景顏色及內邊距:

RawChip(
  label: Text('老孟'),
  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
  backgroundColor: Colors.blue,
  padding: EdgeInsets.symmetric(vertical: 10),
)複製代碼

效果以下:

設置陰影:

RawChip(
  label: Text('老孟'),
  elevation: 8,
  shadowColor: Colors.blue,
)複製代碼

效果以下:

materialTapTargetSize屬性控制最小點擊區域,詳情查看:MaterialTapTargetSize

設置選中狀態、顏色:

bool _selected = false;
RawChip(
  label: Text('老孟'),
  selected: _selected,
  onSelected: (v){
    setState(() {
      _selected = v;
    });
  },
  selectedColor: Colors.blue,
  selectedShadowColor: Colors.red,
)複製代碼

效果以下:

設置選中狀態下「前置對勾」圖標:

RawChip(
  label: Text('老孟'),
  selected: true,
  showCheckmark: true,
  checkmarkColor: Colors.red,
)複製代碼

效果以下:

showCheckmark爲false時,無「前置對勾」圖標。

設置點擊屬性:

RawChip(
  label: Text('老孟'),
  onPressed: (){
    print('onPressed');
  },
  pressElevation: 12,
)複製代碼

效果以下:

點擊時有水波紋效果。

Chip

Chip是一個簡單的標籤控件,僅顯示信息和刪除相關屬性,是一個簡化版的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

以緊湊的形式表示一條複雜的信息,例如實體(人,地方或事物)或對話文本。

InputChip 本質上也是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,
  );
}複製代碼

ChoiceChip

容許從一組選項中進行單個選擇,建立一個相似於單選按鈕的標籤,本質上ChoiceChip也是一個RawChip,ChoiceChip自己不具有單選屬性。

單選demo以下:

int _selectIndex = 0;
Wrap(
  spacing: 15,
  children: List.generate(10, (index) {
    return ChoiceChip(
      label: Text('老孟 $index'),
      selected: _selectIndex == index,
      onSelected: (v) {
        setState(() {
          _selectIndex = index;
        });
      },
    );
  }).toList(),
)複製代碼

效果以下:

本控件由共建者普通程序員提供。

FilterChip

FilterChip能夠做爲過濾標籤,本質上也是一個RawChip,用法以下:

List<String> _filters = [];

Column(
  children: <Widget>[
    Wrap(
      spacing: 15,
      children: List.generate(10, (index) {
        return FilterChip(
          label: Text('老孟 $index'),
          selected: _filters.contains('$index'),
          onSelected: (v) {
            setState(() {
              if(v){
                _filters.add('$index');
              }else{
                _filters.removeWhere((f){
                  return f == '$index';
                });
              }
            });
          },
        );
      }).toList(),
    ),
    Text('選中:${_filters.join(',')}'),
  ],
)複製代碼

效果以下:

ActionChip

顯示與主要內容有關的一組動做,本質上也是一個RawChip,用法以下:

ActionChip(
    avatar: CircleAvatar(
      backgroundColor: Colors.grey.shade800,
      child: Text('孟'),
    ),
    label: Text('老孟'),
    onPressed: () {
      print("onPressed");
    })複製代碼

效果以下:

效果很像按鈕類控件。

交流

老孟Flutter博客地址(近200個控件用法):laomengit.com

歡迎加入Flutter交流羣(微信:laomengit)、關注公衆號【老孟Flutter】:

相關文章
相關標籤/搜索