本篇內容主要講解“Flutter實現自定義篩選框的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Flutter實現自定義篩選框的方法”吧!
一、首先自定義篩選框的按鈕視圖,布局很簡單,一個listView就可以搞定。
二、定義篩選數據展示列表視圖。
1、在數據model中添加了一個selectedModel屬性,用來記錄當前已選擇的篩選項(目前僅支持單選)。
2、當按鈕數量小于3個時,按鈕最大寬度為屏幕寬度的1/3;小于屏幕寬度時,則為屏幕寬度/按鈕數量。
具體代碼如下:
var text = model.selectedFilterModel != null
? model.selectedFilterModel.dictValue
: model.name ?? "";
return Container(
padding: EdgeInsets.symmetric(horizontal: 20),
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width /
(widget.dataList.length > 3 ? 3 : widget.dataList.length),
maxHeight: widget.viewHeight),
color: Colors.white,
child: InkWell(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
text,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: widget.textSize,
color: model.isSelected
? widget.textSelectColor
: widget.textColor),
),
SizedBox(
width: 4,
),
model.isSelected == true
? Icon(Icons.keyboard_arrow_down,
color: widget.textSelectColor)
: Icon(Icons.keyboard_arrow_up, color: widget.textColor),
],
),
onTap: () {
setState(() {
if (_selectModel != null && _selectModel != model) {
_selectModel.isSelected = false;
}
model.isSelected = !model.isSelected;
_selectModel = model;
});
}));首先在剩余視圖上定義一個背景黑色透明的遮罩層,然后在這層Container上展示listView,listView展示的數據為篩選的具體數據內容。Visibility控制整體視圖是否可見,具體代碼如下:
visible:
Provider.of<FilterModelProvider>(context).isShowFilterOptionsView ??
false,
child: GestureDetector(
onTap: () {
Provider.of<FilterModelProvider>(context, listen: false)
.hideFilterOptionsView();
},
child: Container(
color: Colors.black54,
child: Container(
margin: EdgeInsets.only(
bottom: SizeFit.screenHeight -
widget.filterButtonViewHeight -
SizeFit.appBarHeight -
listViewHeight +
animation.value),
color: Colors.white,
child: ListView.builder(
padding: EdgeInsets.zero,
itemCount: _dataList.length,
itemBuilder: (BuildContext context, int index) {
return InkWell(
child: Container(
height: widget.listHeight,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
// crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
_dataList[index].dictValue,
overflow: TextOverflow.ellipsis,
maxLines: 1,
style: TextStyle(
fontSize: 16,
color: Colors.black87,
fontWeight: FontWeight.normal),
),
Divider(
height: 1,
indent: 1,
)
],
),
),
onTap: () {
Provider.of<FilterModelProvider>(context, listen: false)
.selectFilterOption(_dataList[index]);
},
);
}),
),
),
),
);到此,相信大家對“Flutter實現自定義篩選框的方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。