Flutter實(shí)現(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,// 可以通過(guò)不同狀態(tài)設(shè)置滑塊顏色
this.trackColor,// 可以通過(guò)不同狀態(tài)設(shè)置軌道顏色
this.materialTapTargetSize,//設(shè)置組件的最小大小
this.dragStartBehavior = DragStartBehavior.start,// 處理手勢(shì)拖拽行為
this.mouseCursor,//設(shè)置鼠標(biāo)停留狀態(tài) app用不到
this.focusColor,// 獲取焦點(diǎn)顏色
this.hoverColor,//指針懸停顏色
this.overlayColor,// 設(shè)置按壓滑動(dòng)覆蓋上面的顏色
this.splashRadius,// 設(shè)置點(diǎn)擊滑動(dòng)覆蓋圓環(huán)的半徑
this.focusNode,//焦點(diǎn)控制
this.autofocus = false,// 是否自動(dòng)獲取焦點(diǎn)通過(guò)Switch構(gòu)造方法我們可以實(shí)現(xiàn)簡(jiǎn)單的開關(guān)組件,并且除了改變顏色之外我們還可以自定義滑塊,如果對(duì)這個(gè)開關(guān)組件進(jìn)行說(shuō)明除了自定義布局,還可以使用SwitchListTile組件,一個(gè)列表和Swith的組合,官方幫我們實(shí)現(xiàn)了很多常見的功能,可以直接拿來(lái)使用。如果使用蘋果風(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,//選中這個(gè)按鈕的回調(diào)
this.mouseCursor,// 鼠標(biāo)懸停狀態(tài)
this.toggleable = false,//點(diǎn)擊已選中按鈕是否調(diào)用onChanged回調(diào)
this.activeColor,// 選項(xiàng)按鈕顏色
this.fillColor,//設(shè)置單選框不同狀態(tài)的的顏色
this.focusColor,// 獲取焦點(diǎn)顏色
this.hoverColor,//指針懸停顏色
this.overlayColor,//按壓覆蓋顏色
this.splashRadius,//按壓覆蓋顏色的半徑
this.materialTapTargetSize,//組件最小大小
this.visualDensity,//組件的緊湊程度
this.focusNode,//焦點(diǎn)
this.autofocus = false,//是否自動(dòng)獲取焦點(diǎn)
})單選組件使用了泛型,我們?cè)谑褂玫臅r(shí)候可以自定義選項(xiàng)的數(shù)據(jù)類型,一般都是在列表中使用,通過(guò)單選組件可以幫我們實(shí)現(xiàn)一個(gè)單選列表選項(xiàng),當(dāng)然Radio也有對(duì)應(yīng)的RadioListTile,用來(lái)對(duì)單選框進(jìn)行說(shuō)明。
示例代碼:
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,// 對(duì)勾顏色
this.focusColor,// 獲取焦點(diǎn)顏色
this.hoverColor,// 指針懸停顏色
this.overlayColor,// 按壓覆蓋顏色
this.splashRadius,// 按壓覆蓋半徑
this.materialTapTargetSize,//最小大小
this.visualDensity,// 組件緊湊程度
this.focusNode,
this.autofocus = false,
this.shape,// 自定義選項(xiàng)框樣式
this.side,// 自定義選項(xiàng)框邊框樣式
}) 多選組件可以使用shape和side字段自定義選擇框樣式,不過(guò)一般交互都是單選用圓形,多選用方形。既然前面?zhèn)z兄弟都有現(xiàn)成的輔助說(shuō)明組件,多選自然也有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)造有很多一樣的屬性,同時(shí)也有在移動(dòng)端也用不著的屬性,比如鼠標(biāo)焦點(diǎn)相關(guān)的屬性,我目前使用的版本是2.8.1,在2.10之后版本Flutter正式支持了Windows桌面應(yīng)用開發(fā),也可見Flutter組件跨平臺(tái)的特點(diǎn),以后有時(shí)間再研究下Windwos應(yīng)用開發(fā)。
到此這篇關(guān)于Flutter實(shí)現(xiàn)單選,復(fù)選和開關(guān)組件的示例代碼的文章就介紹到這了,更多相關(guān)Flutter單選 復(fù)選 開關(guān)組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android檢查手機(jī)網(wǎng)絡(luò)狀態(tài)及網(wǎng)絡(luò)類型的方法
這篇文章主要介紹了Android檢查手機(jī)網(wǎng)絡(luò)狀態(tài)及網(wǎng)絡(luò)類型的方法,涉及Android檢測(cè)手機(jī)網(wǎng)絡(luò)狀態(tài)的技巧,需要的朋友可以參考下2015-04-04
Android中Progress的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android中Progress的簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
搭建Android上的服務(wù)器 “實(shí)現(xiàn)隔空取物”的方法
本篇文章主要介紹了搭建Android上的服務(wù)器 “實(shí)現(xiàn)隔空取物”的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android實(shí)現(xiàn)原生側(cè)滑菜單的超簡(jiǎn)單方式
網(wǎng)上關(guān)于Android實(shí)現(xiàn)側(cè)滑菜單的文章有很多,可是我們這篇文章是給大家分享一種超簡(jiǎn)單的方式,對(duì)大家開發(fā)Android具有一定的參考借鑒價(jià)值,有需要的朋友們可以一起來(lái)看看。2016-09-09
Flutter最小刷新范圍探索ValueListenableBuilder使用詳解
這篇文章主要為大家介紹了Flutter最小刷新范圍探索ValueListenableBuilder使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

