Flutter實現(xiàn)單選,復(fù)選和開關(guān)組件的示例代碼
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)文章
Android省市區(qū)三級聯(lián)動控件使用方法實例講解
最近有需求需要實現(xiàn)省市區(qū)三級聯(lián)動,但是發(fā)現(xiàn)之前的實現(xiàn)不夠靈活,自己做了一些優(yōu)化。下面通過實例代碼給大家介紹下Android省市區(qū)三級聯(lián)動控件使用方法2017-01-01
Android自定義Gallery控件實現(xiàn)3D圖片瀏覽器
這篇文章主要介紹了Android自定義Gallery控件實現(xiàn)3D圖片瀏覽器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
修改Android簽名證書keystore的密碼、別名alias以及別名密碼
這篇文章主要介紹了修改Android簽名證書keystore的密碼、別名alias以及別名密碼的相關(guān)資料,需要的朋友可以參考下2015-12-12
Android自定義View實現(xiàn)仿網(wǎng)易音樂唱片播放效果
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)仿網(wǎng)易音樂唱片播放效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Android調(diào)用系統(tǒng)圖片裁剪限定尺寸及7.0照相問題的解決方法
這篇文章主要介紹了Android調(diào)用系統(tǒng)圖片裁剪限定尺寸,及7.0照相問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07

