Skip to content

Commit

Permalink
GZXDropDownMenu add dropdownMenuChanging and dropdownMenuChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
GanZhiXiong committed Jun 2, 2020
1 parent 301ccc8 commit bace915
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
31 changes: 21 additions & 10 deletions example/lib/gzx_dropdown_menu_test_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
var _scaffoldKey = new GlobalKey<ScaffoldState>();
GlobalKey _stackKey = GlobalKey();

String _dropdownMenuChange;

@override
void initState() {
// TODO: implement initState
Expand Down Expand Up @@ -92,11 +94,8 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
color: Theme.of(context).primaryColor,
alignment: Alignment.center,
child: Text(
'仿美团电影下拉筛选菜单',
style: TextStyle(
fontSize: 16,
color: Colors.white,
),
'仿美团电影下拉筛选菜单$_dropdownMenuChange',
style: TextStyle(fontSize: 16, color: Colors.white, fontWeight: FontWeight.bold),
),
),
// SizedBox(height: 20,),
Expand Down Expand Up @@ -133,12 +132,12 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
// // 分割线颜色
// dividerColor: Color(0xFFeeede6),
// // 文字样式
// style: TextStyle(color: Color(0xFF666666), fontSize: 13),
style: TextStyle(color: Color(0xFF666666), fontSize: 14),
// // 下拉时文字样式
// dropDownStyle: TextStyle(
// fontSize: 13,
// color: Theme.of(context).primaryColor,
// ),
dropDownStyle: TextStyle(
fontSize: 14,
color: Theme.of(context).primaryColor,
),
// // 图标大小
// iconSize: 20,
// // 图标颜色
Expand Down Expand Up @@ -170,6 +169,18 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
// 下拉后遮罩颜色
// maskColor: Theme.of(context).primaryColor.withOpacity(0.5),
// maskColor: Colors.red.withOpacity(0.5),
dropdownMenuChanging: (isShow, index) {
setState(() {
_dropdownMenuChange = '(正在${isShow ? '显示' : '隐藏'}$index)';
print(_dropdownMenuChange);
});
},
dropdownMenuChanged: (isShow, index) {
setState(() {
_dropdownMenuChange = '(已经${isShow ? '显示' : '隐藏'}$index)';
print(_dropdownMenuChange);
});
},
// 下拉菜单,高度自定义,你想显示什么就显示什么,完全由你决定,你只需要在选择后调用_dropdownMenuController.hide();即可
menus: [
GZXDropdownMenuBuilder(
Expand Down
25 changes: 21 additions & 4 deletions lib/src/gzx_dropdown_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ class GZXDropdownMenuBuilder {
GZXDropdownMenuBuilder({@required this.dropDownWidget, @required this.dropDownHeight});
}

typedef DropdownMenuChange = void Function(bool isShow, int index);

class GZXDropDownMenu extends StatefulWidget {
final GZXDropdownMenuController controller;
final List<GZXDropdownMenuBuilder> menus;
final int animationMilliseconds;
final Color maskColor;
final DropdownMenuChange dropdownMenuChanging;
final DropdownMenuChange dropdownMenuChanged;

const GZXDropDownMenu(
{Key key,
@required this.controller,
@required this.menus,
this.animationMilliseconds = 500,
this.maskColor = const Color.fromRGBO(0, 0, 0, 0.5)})
this.maskColor = const Color.fromRGBO(0, 0, 0, 0.5),
this.dropdownMenuChanging,
this.dropdownMenuChanged})
: super(key: key);

@override
Expand All @@ -38,6 +44,8 @@ class _GZXDropDownMenuState extends State<GZXDropDownMenu> with SingleTickerProv

double _dropDownHeight;

int _currentMenuIndex;

@override
void initState() {
// TODO: implement initState
Expand Down Expand Up @@ -70,17 +78,20 @@ class _GZXDropDownMenuState extends State<GZXDropDownMenu> with SingleTickerProv
}

_showDropDownItemWidget() {
int menuIndex = widget.controller.menuIndex;
if (menuIndex >= widget.menus.length || widget.menus[menuIndex] == null) {
_currentMenuIndex = widget.controller.menuIndex;
if (_currentMenuIndex >= widget.menus.length || widget.menus[_currentMenuIndex] == null) {
return;
}

_isShowDropDownItemWidget = !_isShowDropDownItemWidget;
if (widget.dropdownMenuChanging != null) {
widget.dropdownMenuChanging(_isShowDropDownItemWidget, _currentMenuIndex);
}
if (!_isShowMask) {
_isShowMask = true;
}

_dropDownHeight = widget.menus[menuIndex].dropDownHeight;
_dropDownHeight = widget.menus[_currentMenuIndex].dropDownHeight;

_animation?.removeListener(_animationListener);
_animation?.removeStatusListener(_animationStatusListener);
Expand All @@ -106,6 +117,9 @@ class _GZXDropDownMenuState extends State<GZXDropDownMenu> with SingleTickerProv
case AnimationStatus.dismissed:
// print('dismissed');
_isShowMask = false;
if (widget.dropdownMenuChanged != null) {
widget.dropdownMenuChanged(false, _currentMenuIndex);
}
break;
case AnimationStatus.forward:
// TODO: Handle this case.
Expand All @@ -115,6 +129,9 @@ class _GZXDropDownMenuState extends State<GZXDropDownMenu> with SingleTickerProv
break;
case AnimationStatus.completed:
// print('completed');
if (widget.dropdownMenuChanged != null) {
widget.dropdownMenuChanged(true, _currentMenuIndex);
}
break;
}
}
Expand Down

0 comments on commit bace915

Please sign in to comment.