日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Flutter?Widgets之標(biāo)簽類控件Chip詳解_IOS

作者:風(fēng)雨_83 ? 更新時(shí)間: 2022-11-28 編程語(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

欄目分類
最近更新