Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
GanZhiXiong committed Apr 6, 2021
1 parent 084e980 commit 1933f2a
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 118 deletions.
244 changes: 139 additions & 105 deletions example/lib/gzx_dropdown_menu_test_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class SortCondition {
String name;
bool isSelected;

SortCondition({required this.name, required this.isSelected});
SortCondition({
required this.name,
required this.isSelected,
});
}

class GZXDropDownMenuTestPage extends StatefulWidget {
Expand Down Expand Up @@ -65,12 +68,13 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
return Scaffold(
key: _scaffoldKey,
appBar: PreferredSize(
child: AppBar(
brightness: Brightness.dark,
backgroundColor: Theme.of(context).primaryColor,
elevation: 0,
),
preferredSize: Size.fromHeight(0)),
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),
Expand All @@ -80,7 +84,9 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
// child: TextField(),
// ),),
child: ListView(
children: <Widget>[TextField()],
children: <Widget>[
TextField(),
],
),
),
// GZXDropDownMenu只能在Stack内,后续有时间会改进,以及支持CustomScrollView和NestedScrollView
Expand All @@ -96,7 +102,11 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
alignment: Alignment.center,
child: Text(
'仿美团电影下拉筛选菜单$_dropdownMenuChange',
style: TextStyle(fontSize: 16, color: Colors.white, fontWeight: FontWeight.bold),
style: TextStyle(
fontSize: 16,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
// SizedBox(height: 20,),
Expand All @@ -110,7 +120,11 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
_dropDownHeaderItemStrings[2],
style: TextStyle(color: Colors.green),
),
GZXDropDownHeaderItem(_dropDownHeaderItemStrings[3], iconData: Icons.filter_frames, iconSize: 18),
GZXDropDownHeaderItem(
_dropDownHeaderItemStrings[3],
iconData: Icons.filter_frames,
iconSize: 18,
),
],
// GZXDropDownHeader对应第一父级Stack的key
stackKey: _stackKey,
Expand Down Expand Up @@ -243,32 +257,12 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
Container(
width: 100,
child: ListView(
children: firstLevels.map((item) {
int index = firstLevels.indexOf(item);
return GestureDetector(
onTap: () {
_selectTempFirstLevelIndex = index;

if (_selectTempFirstLevelIndex == 0) {
itemOnTap('全城');
return;
}
setState(() {});
},
child: Container(
height: 40,
color: _selectTempFirstLevelIndex == index ? Colors.grey[200] : Colors.white,
alignment: Alignment.center,
child: _selectTempFirstLevelIndex == index
? Text(
'$item',
style: TextStyle(
color: Theme.of(context).primaryColor,
),
)
: Text('$item')),
);
}).toList(),
children: firstLevels.map(
(item) {
int index = firstLevels.indexOf(item);
return gestureDetector0(index, itemOnTap, item);
},
).toList(),
),
),
Expanded(
Expand All @@ -277,43 +271,79 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
child: _selectTempFirstLevelIndex == 0
? Container()
: ListView(
children: secondLevels.map((item) {
int index = secondLevels.indexOf(item);
return GestureDetector(
onTap: () {
_selectSecondLevelIndex = index;
_selectFirstLevelIndex = _selectTempFirstLevelIndex;
if (_selectSecondLevelIndex == 0) {
itemOnTap(firstLevels[_selectFirstLevelIndex]);
} else {
itemOnTap(item);
}
},
child: Container(
height: 40,
alignment: Alignment.centerLeft,
child: Row(children: <Widget>[
SizedBox(
width: 20,
),
_selectFirstLevelIndex == _selectTempFirstLevelIndex && _selectSecondLevelIndex == index
? Text(
'$item',
style: TextStyle(
color: Theme.of(context).primaryColor,
),
)
: Text('$item'),
]),
));
}).toList(),
children: secondLevels.map(
(item) {
int index = secondLevels.indexOf(item);
return gestureDetector1(index, itemOnTap, firstLevels, item);
},
).toList(),
),
),
)
),
],
);
}

GestureDetector gestureDetector0(int index, void itemOnTap(String selectValue), item) {
return GestureDetector(
onTap: () {
_selectTempFirstLevelIndex = index;

if (_selectTempFirstLevelIndex == 0) {
itemOnTap('全城');
return;
}
setState(() {});
},
child: Container(
height: 40,
color: _selectTempFirstLevelIndex == index ? Colors.grey[200] : Colors.white,
alignment: Alignment.center,
child: _selectTempFirstLevelIndex == index
? Text(
'$item',
style: TextStyle(
color: Theme.of(context).primaryColor,
),
)
: Text('$item'),
),
);
}

GestureDetector gestureDetector1(int index, void itemOnTap(String selectValue), List firstLevels, item) {
return GestureDetector(
onTap: () {
_selectSecondLevelIndex = index;
_selectFirstLevelIndex = _selectTempFirstLevelIndex;
if (_selectSecondLevelIndex == 0) {
itemOnTap(firstLevels[_selectFirstLevelIndex]);
} else {
itemOnTap(item);
}
},
child: Container(
height: 40,
alignment: Alignment.centerLeft,
child: Row(
children: <Widget>[
SizedBox(
width: 20,
),
_selectFirstLevelIndex == _selectTempFirstLevelIndex && _selectSecondLevelIndex == index
? Text(
'$item',
style: TextStyle(
color: Theme.of(context).primaryColor,
),
)
: Text('$item'),
],
),
),
);
}

_buildConditionListWidget(items, void itemOnTap(SortCondition sortCondition)) {
return ListView.separated(
shrinkWrap: true,
Expand All @@ -323,47 +353,51 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
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;
return gestureDetector(items, index, itemOnTap, context);
},
);
}

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,
GestureDetector gestureDetector(items, int index, void itemOnTap(SortCondition sortCondition), BuildContext context) {
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,
),
],
),
),
);
}
}
13 changes: 11 additions & 2 deletions lib/src/gzx_dropdown_header.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';

import 'gzx_dropdown_menu_controller.dart';

/// Signature for when a tap has occurred.
Expand Down Expand Up @@ -95,7 +96,10 @@ class _GZXDropDownHeaderState extends State<GZXDropDownHeader> with SingleTicker
height: widget.height,
// padding: EdgeInsets.only(top: 1, bottom: 1),
decoration: BoxDecoration(
border: Border.all(color: widget.borderColor, width: widget.borderWidth),
border: Border.all(
color: widget.borderColor,
width: widget.borderWidth,
),
),
child: gridView,
);
Expand Down Expand Up @@ -192,5 +196,10 @@ class GZXDropDownHeaderItem {
final double? iconSize;
final TextStyle? style;

GZXDropDownHeaderItem(this.title, {this.iconData, this.iconSize, this.style});
GZXDropDownHeaderItem(
this.title, {
this.iconData,
this.iconSize,
this.style,
});
}
25 changes: 14 additions & 11 deletions lib/src/gzx_dropdown_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class GZXDropdownMenuBuilder {
/// Dropdown menu height.
final double dropDownHeight;

GZXDropdownMenuBuilder({required this.dropDownWidget, required this.dropDownHeight});
GZXDropdownMenuBuilder({
required this.dropDownWidget,
required this.dropDownHeight,
});
}

typedef DropdownMenuChange = void Function(bool isShow, int? index);
Expand All @@ -30,15 +33,15 @@ class GZXDropDownMenu extends StatefulWidget {

/// Creates a dropdown menu widget.
/// The widget must be inside the Stack because the widget is a Positioned.
const GZXDropDownMenu(
{Key? key,
required this.controller,
required this.menus,
this.animationMilliseconds = 500,
this.maskColor = const Color.fromRGBO(0, 0, 0, 0.5),
this.dropdownMenuChanging,
this.dropdownMenuChanged})
: super(key: key);
const GZXDropDownMenu({
Key? key,
required this.controller,
required this.menus,
this.animationMilliseconds = 500,
this.maskColor = const Color.fromRGBO(0, 0, 0, 0.5),
this.dropdownMenuChanging,
this.dropdownMenuChanged,
}) : super(key: key);

@override
_GZXDropDownMenuState createState() => _GZXDropDownMenuState();
Expand Down Expand Up @@ -194,7 +197,7 @@ class _GZXDropDownMenuState extends State<GZXDropDownMenu> with SingleTickerProv
height: _animation == null ? 0 : _animation!.value,
child: widget.menus[menuIndex].dropDownWidget,
),
_mask()
_mask(),
],
));
}
Expand Down

0 comments on commit 1933f2a

Please sign in to comment.