最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Flutter實現(xiàn)單選,復(fù)選和開關(guān)組件的示例代碼

 更新時間:2022年04月22日 17:02:06   作者:老李code  
在App開發(fā)過程中,選擇交互是非常常見的,今天主要介紹下關(guān)于選擇的三個組件的使用:開關(guān)、單選和復(fù)選,感興趣的小伙伴可以了解一下

1、開關(guān) Switch

構(gòu)造方法:

const Switch({
  Key? key,
  required this.value,//當(dāng)前開關(guān)狀態(tài)
  required this.onChanged,// 改變狀態(tài)回調(diào)
  this.activeColor,// 開啟全部顏色
  this.activeTrackColor,// 開啟軌道的顏色
  this.inactiveThumbColor,//關(guān)閉滑塊顏色
  this.inactiveTrackColor,// 關(guān)閉軌道顏色
  this.activeThumbImage,// 開啟滑塊圖片
  this.onActiveThumbImageError,// 開啟滑塊圖片加載失敗觸發(fā)
  this.inactiveThumbImage,// 關(guān)閉滑塊圖片
  this.onInactiveThumbImageError,// 關(guān)閉滑塊圖片加載失敗觸發(fā)
  this.thumbColor,// 可以通過不同狀態(tài)設(shè)置滑塊顏色
  this.trackColor,// 可以通過不同狀態(tài)設(shè)置軌道顏色
  this.materialTapTargetSize,//設(shè)置組件的最小大小
  this.dragStartBehavior = DragStartBehavior.start,// 處理手勢拖拽行為
  this.mouseCursor,//設(shè)置鼠標(biāo)停留狀態(tài) app用不到
  this.focusColor,// 獲取焦點顏色
  this.hoverColor,//指針懸停顏色 
  this.overlayColor,// 設(shè)置按壓滑動覆蓋上面的顏色
  this.splashRadius,// 設(shè)置點擊滑動覆蓋圓環(huán)的半徑
  this.focusNode,//焦點控制
  this.autofocus = false,// 是否自動獲取焦點

通過Switch構(gòu)造方法我們可以實現(xiàn)簡單的開關(guān)組件,并且除了改變顏色之外我們還可以自定義滑塊,如果對這個開關(guān)組件進行說明除了自定義布局,還可以使用SwitchListTile組件,一個列表和Swith的組合,官方幫我們實現(xiàn)了很多常見的功能,可以直接拿來使用。如果使用蘋果風(fēng)格開關(guān)可以使用封裝好的CupertinoSwitch。

示例代碼:

Switch(
    // activeColor: Colors.blue,
    activeTrackColor: Colors.red,
    inactiveTrackColor: Colors.green,
    // inactiveThumbColor: Colors.yellow,
    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
    dragStartBehavior: DragStartBehavior.start,
    activeThumbImage: AssetImage("images/lbxx.png"),
    inactiveThumbImage: AssetImage("images/lbxx.png"),
    value: _switchSelected,
    onChanged: (value) {
      setState(() {
        _switchSelected = value;
      });
    }),

2、單選 Radio

構(gòu)造方法:

const Radio<T>({
  Key? key,
  required this.value,//單選按鈕的值
  required this.groupValue,//當(dāng)前選中的值
  required this.onChanged,//選中這個按鈕的回調(diào)
  this.mouseCursor,// 鼠標(biāo)懸停狀態(tài)
  this.toggleable = false,//點擊已選中按鈕是否調(diào)用onChanged回調(diào)
  this.activeColor,// 選項按鈕顏色
  this.fillColor,//設(shè)置單選框不同狀態(tài)的的顏色
  this.focusColor,// 獲取焦點顏色
  this.hoverColor,//指針懸停顏色
  this.overlayColor,//按壓覆蓋顏色
  this.splashRadius,//按壓覆蓋顏色的半徑
  this.materialTapTargetSize,//組件最小大小
  this.visualDensity,//組件的緊湊程度
  this.focusNode,//焦點
  this.autofocus = false,//是否自動獲取焦點
})

單選組件使用了泛型,我們在使用的時候可以自定義選項的數(shù)據(jù)類型,一般都是在列表中使用,通過單選組件可以幫我們實現(xiàn)一個單選列表選項,當(dāng)然Radio也有對應(yīng)的RadioListTile,用來對單選框進行說明。

示例代碼:

Column(
  children: [
_radioCheckBox(_dataList[0]),
_radioCheckBox(_dataList[1]),
_radioCheckBox(_dataList[2]),
_radioCheckBox(_dataList[3])
  ],
),
Row _radioCheckBox(FMRadioBean fmRadioBean) {
  return Row(
    children: [
      Radio<FMRadioBean>(
          visualDensity: VisualDensity(
              horizontal: VisualDensity.minimumDensity,
              vertical: VisualDensity.minimumDensity),
          value: fmRadioBean,
          // activeColor: Colors.red,
          fillColor: MaterialStateProperty.resolveWith((state) {
            if (state.contains(MaterialState.selected)) {
              return Colors.red;
            } else {
              return Colors.blue;
            }
          }),
          focusColor: Colors.orange,
          groupValue: groupValue,
          toggleable: false,
          onChanged: (value) {
            setState(() {
              groupValue = fmRadioBean;
              radioText = fmRadioBean.text;
            });
          }),
      Text(fmRadioBean.text)
    ],
  );
}
class FMRadioBean {
  int index;
  String text;
  bool isSelect;
  FMRadioBean(this.index, this.text, this.isSelect);
}

3、復(fù)選多選 Checkbox

構(gòu)造方法:

const Checkbox({
  Key? key,
  required this.value,// 是否被選中
  this.tristate = false,//復(fù)選框value是否可以為null
  required this.onChanged,// 選中回調(diào)
  this.mouseCursor,// 鼠標(biāo)指針狀態(tài)
  this.activeColor,// 選中顏色
  this.fillColor,// 不同狀態(tài)顏色設(shè)置
  this.checkColor,// 對勾顏色
  this.focusColor,// 獲取焦點顏色
  this.hoverColor,// 指針懸停顏色
  this.overlayColor,// 按壓覆蓋顏色
  this.splashRadius,// 按壓覆蓋半徑
  this.materialTapTargetSize,//最小大小
  this.visualDensity,// 組件緊湊程度
  this.focusNode,
  this.autofocus = false,
  this.shape,// 自定義選項框樣式
  this.side,// 自定義選項框邊框樣式
}) 

多選組件可以使用shape和side字段自定義選擇框樣式,不過一般交互都是單選用圓形,多選用方形。既然前面?zhèn)z兄弟都有現(xiàn)成的輔助說明組件,多選自然也有CheckboxListTile,仨兄弟用法基本一樣。

示例代碼:

Column(
  children: [
    _checkCheckBox(_dataList[0]),
    _checkCheckBox(_dataList[1]),
    _checkCheckBox(_dataList[2]),
    _checkCheckBox(_dataList[3])
  ],
),
Text(_checkText.toString())

Row _checkCheckBox(FMRadioBean fmRadioBean) {
  return Row(
    children: [
      Checkbox(
          visualDensity: VisualDensity(
              horizontal: VisualDensity.minimumDensity,
              vertical: VisualDensity.minimumDensity),
          value: fmRadioBean.isSelect,
          activeColor: Colors.blue,
          checkColor: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(6))
          ),
          side: BorderSide(color: Colors.black,width: 2,style: BorderStyle.solid),
          onChanged: (value) {
            setState(() {
              if (value == true) {
                fmRadioBean.isSelect = true;
                _checkText.add(fmRadioBean.text);
              } else {
                fmRadioBean.isSelect = false;
                _checkText.remove(fmRadioBean.text);
              }
            });
          }),
      Text(fmRadioBean.text)
    ],
  );
}

小結(jié)

可以看到這仨兄弟的構(gòu)造有很多一樣的屬性,同時也有在移動端也用不著的屬性,比如鼠標(biāo)焦點相關(guān)的屬性,我目前使用的版本是2.8.1,在2.10之后版本Flutter正式支持了Windows桌面應(yīng)用開發(fā),也可見Flutter組件跨平臺的特點,以后有時間再研究下Windwos應(yīng)用開發(fā)。

到此這篇關(guān)于Flutter實現(xiàn)單選,復(fù)選和開關(guān)組件的示例代碼的文章就介紹到這了,更多相關(guān)Flutter單選 復(fù)選 開關(guān)組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

教育| 武陟县| 广西| 香格里拉县| 贵港市| 噶尔县| 德钦县| 汝城县| 蕲春县| 宁德市| 黄陵县| 罗山县| 海伦市| 得荣县| 青阳县| 合江县| 延吉市| 长顺县| 兴和县| 南宁市| 张北县| 黄大仙区| 文化| 兖州市| 丘北县| 察哈| 海阳市| 昭平县| 永宁县| 广州市| 桃源县| 娄底市| 渭南市| 永平县| 泰和县| 鸡东县| 武鸣县| 黄骅市| 霍州市| 昌吉市| 迭部县|