最近开始用flutter开发公司里的项目,所以打算写一系列的博客,分享工作到遇到的一些难题和解决方法
今天遇到一个需求----------下拉筛选,跟下面的图片需求差不多
首先我先找一下flutter有没有相关的组件,找了一下没有找到,或者找到了都不友好。最后查到一个插件gzx_dropdown_menu
gzx_dropdown_menu : ^1.0.1
_brandSortConditions.add(SortCondition(name: '-游玩地方-', isSelected: true));
_brandSortConditions.add(SortCondition(name: '银泰', isSelected: false));
_brandSortConditions.add(SortCondition(name: '万达', isSelected: false));
_brandSortConditions.add(SortCondition(name: '八佰伴', isSelected: false));
_brandSortConditions.add(SortCondition(name: '游乐园', isSelected: false));
_brandSortConditions.add(SortCondition(name: '电影院', isSelected: false));
_selectBrandSortCondition = _brandSortConditions[0];
// 下拉菜单头部
GZXDropDownHeader(
// 下拉的头部项,目前每一项,只能自定义显示的文字、图标、图标大小修改
items: [
GZXDropDownHeaderItem(_dropDownHeaderItemStrings[0]),
],
// GZXDropDownHeader对应第一父级Stack的key
stackKey: _stackKey,
// controller用于控制menu的显示或隐藏
controller: _dropdownMenuController,
// 当点击头部项的事件,在这里可以进行页面跳转或openEndDrawer
onItemTap: (index) {
if (index == 3) {
_scaffoldKey.currentState.openEndDrawer();
_dropdownMenuController.hide();
}
},
),
// 下拉菜单
GZXDropDownMenu(
// controller用于控制menu的显示或隐藏
controller: _dropdownMenuController,
// 下拉菜单显示或隐藏动画时长
animationMilliseconds: 500,
// 下拉菜单,高度自定义,你想显示什么就显示什么,完全由你决定,你只需要在选择后调用_dropdownMenuController.hide();即可
menus: [
GZXDropdownMenuBuilder(
dropDownHeight: 40 * 8.0,
dropDownWidget: _buildConditionListWidget(_brandSortConditions, (value) {
_selectBrandSortCondition = value;
_dropDownHeaderItemStrings[0] =
_selectBrandSortCondition.name == '全部' ? '品牌' : _selectBrandSortCondition.name;
_dropdownMenuController.hide();
setState(() {});
})),
],
),
import 'package:flutter/material.dart';
import 'package:gzx_dropdown_menu/gzx_dropdown_menu.dart';
class SortCondition {
String name;
bool isSelected;
SortCondition({this.name, this.isSelected});
}
class GZXDropDownMenuTestPage extends StatefulWidget {
@override
_GZXDropDownMenuTestPageState createState() => _GZXDropDownMenuTestPageState();
}
class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
List<String> _dropDownHeaderItemStrings = ['品牌'];
List<SortCondition> _brandSortConditions = [];
SortCondition _selectBrandSortCondition;
GZXDropdownMenuController _dropdownMenuController = GZXDropdownMenuController();
var _scaffoldKey = new GlobalKey<ScaffoldState>();
GlobalKey _stackKey = GlobalKey();
@override
void initState() {
// TODO: implement initState
super.initState();
_brandSortConditions.add(SortCondition(name: '-游玩地方-', isSelected: true));
_brandSortConditions.add(SortCondition(name: '银泰', isSelected: false));
_brandSortConditions.add(SortCondition(name: '万达', isSelected: false));
_brandSortConditions.add(SortCondition(name: '八佰伴', isSelected: false));
_brandSortConditions.add(SortCondition(name: '游乐园', isSelected: false));
_brandSortConditions.add(SortCondition(name: '电影院', isSelected: false));
_selectBrandSortCondition = _brandSortConditions[0];
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: PreferredSize(
child: AppBar(
brightness: Brightness.dark,
backgroundColor: Theme.of(context).primaryColor,
elevation: 0,
),
preferredSize: Size.fromHeight(0)),
backgroundColor: Colors.white,
endDrawer: Container(
margin: EdgeInsets.only(left: MediaQuery.of(context).size.width / 4, top: 0),
color: Colors.white,
),
body: Stack(
key: _stackKey,
children: <Widget>[
Column(
children: <Widget>[
// 下拉菜单头部
GZXDropDownHeader(
// 下拉的头部项,目前每一项,只能自定义显示的文字、图标、图标大小修改
items: [
GZXDropDownHeaderItem(_dropDownHeaderItemStrings[0]),
],
// GZXDropDownHeader对应第一父级Stack的key
stackKey: _stackKey,
// controller用于控制menu的显示或隐藏
controller: _dropdownMenuController,
// 当点击头部项的事件,在这里可以进行页面跳转或openEndDrawer
onItemTap: (index) {
if (index == 3) {
_scaffoldKey.currentState.openEndDrawer();
_dropdownMenuController.hide();
}
},
),
],
),
// 下拉菜单
GZXDropDownMenu(
// controller用于控制menu的显示或隐藏
controller: _dropdownMenuController,
// 下拉菜单显示或隐藏动画时长
animationMilliseconds: 500,
// 下拉菜单,高度自定义,你想显示什么就显示什么,完全由你决定,你只需要在选择后调用_dropdownMenuController.hide();即可
menus: [
GZXDropdownMenuBuilder(
dropDownHeight: 40 * 8.0,
dropDownWidget: _buildConditionListWidget(_brandSortConditions, (value) {
_selectBrandSortCondition = value;
_dropDownHeaderItemStrings[0] =
_selectBrandSortCondition.name == '全部' ? '品牌' : _selectBrandSortCondition.name;
_dropdownMenuController.hide();
setState(() {});
})),
],
),
],
),
);
}
_buildConditionListWidget(items, void itemOnTap(sortCondition)) {
return ListView.separated(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: items.length,
// item 的个数
separatorBuilder: (BuildContext context, int index) => Divider(height: 1.0),
// 添加分割线
itemBuilder: (BuildContext context, int index) {
SortCondition goodsSortCondition = items[index];
return GestureDetector(
onTap: () {
for (var value in items) {
value.isSelected = false;
}
goodsSortCondition.isSelected = true;
itemOnTap(goodsSortCondition);
},
child: Container(
// color: Colors.blue,
height: 40,
child: Row(
children: <Widget>[
SizedBox(
width: 16,
),
Expanded(
child: Text(
goodsSortCondition.name,
style: TextStyle(
color: goodsSortCondition.isSelected ? Theme.of(context).primaryColor : Colors.black,
),
),
),
goodsSortCondition.isSelected
? Icon(
Icons.check,
color: Theme.of(context).primaryColor,
size: 16,
)
: SizedBox(),
SizedBox(
width: 16,
),
],
),
),
);
},
);
}
}
这个坑就先填到这里,希望对你可以有帮助