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實現自定義篩選框的示例代碼的文章就介紹到這瞭,更多相關Flutter 自定義篩選框內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: