Skip to content

Commit

Permalink
Migrate to null safety.
Browse files Browse the repository at this point in the history
  • Loading branch information
GanZhiXiong committed Apr 2, 2021
1 parent bacf448 commit dfe943e
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 132 deletions.
8 changes: 4 additions & 4 deletions example/lib/gzx_dropdown_menu_test_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SortCondition {
String name;
bool isSelected;

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

class GZXDropDownMenuTestPage extends StatefulWidget {
Expand All @@ -17,8 +17,8 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
List<String> _dropDownHeaderItemStrings = ['全城', '品牌', '距离近', '筛选'];
List<SortCondition> _brandSortConditions = [];
List<SortCondition> _distanceSortConditions = [];
SortCondition _selectBrandSortCondition;
SortCondition _selectDistanceSortCondition;
late SortCondition _selectBrandSortCondition;
late SortCondition _selectDistanceSortCondition;
GZXDropdownMenuController _dropdownMenuController = GZXDropdownMenuController();

var _scaffoldKey = new GlobalKey<ScaffoldState>();
Expand Down Expand Up @@ -119,7 +119,7 @@ class _GZXDropDownMenuTestPageState extends State<GZXDropDownMenuTestPage> {
onItemTap: (index) {
if (index == 3) {
_dropdownMenuController.hide();
_scaffoldKey.currentState.openEndDrawer();
_scaffoldKey.currentState!.openEndDrawer();
}
},
// // 头部的高度
Expand Down
2 changes: 1 addition & 1 deletion example/lib/test_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class _TestPageState extends State<TestPage> {
IconButton(
icon: Icon(Icons.menu),
onPressed: () {
_scaffoldKey.currentState.openEndDrawer();
_scaffoldKey.currentState!.openEndDrawer();
})
],
),
Expand Down
30 changes: 22 additions & 8 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,40 @@ description: A new Flutter application.
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

#Dart 运行环境区间
environment:
sdk: ">=2.1.0 <3.0.0"
# sdk: ">=2.11.0 <3.0.0" # Dart SDK版本号,多人开发建议写死
# 健全的空安全已在 Dart 2.12 和 Flutter 2 中可用。
# 所以迁移到空安全的
# 第一步:升级Flutter到2.0
# 第二步:修改Dart SDK范围为2.12.x以上
sdk: '>=2.12.0 <3.0.0'


dependencies:
flutter:
sdk: flutter

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
cupertino_icons: ^1.0.2

# 你可以通过以下三种方式引入gzx_dropdown_menu

# ^1.2.3 等于 '>=1.2.3 <2.0.0'
# ^0.1.2 等于 '>=0.1.2 <0.2.0'
# see detail https://stackoverflow.com/questions/53563079/what-is-the-caret-sign-before-the-dependency-version-number-in-flutters-pub
# 方式1:使用语义版本控制(推荐使用)
# ^3.1.5 等于 '>=3.1.5 <4.0.0'
# ^1.2.3 等于 '>=1.2.3 <2.0.0'
# ^0.1.2 等于 '>=0.1.2 <0.2.0'
# see detail https://stackoverflow.com/questions/53563079/what-is-the-caret-sign-before-the-dependency-version-number-in-flutters-pub
# gzx_dropdown_menu : ^2.1.0

# gzx_dropdown_menu :
# git:
# url: https://github.com/GanZhiXiong/gzx_dropdown_menu.git

# 方式2:通过远程Git仓库
# gzx_dropdown_menu :
# git:
# url: https://github.com/GanZhiXiong/gzx_dropdown_menu.git

# 方式3:通过本地路径
gzx_dropdown_menu :
path : ../

Expand Down
36 changes: 18 additions & 18 deletions lib/src/gzx_dropdown_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ class GZXDropDownHeader extends StatefulWidget {
final double borderWidth;
final Color borderColor;
final TextStyle style;
final TextStyle dropDownStyle;
final TextStyle? dropDownStyle;
final double iconSize;
final Color iconColor;
final Color iconDropDownColor;
final Color? iconDropDownColor;

// final List<String> menuStrings;
final double height;
final double dividerHeight;
final Color dividerColor;
final GZXDropdownMenuController controller;
final OnItemTap onItemTap;
final OnItemTap? onItemTap;
final List<GZXDropDownHeaderItem> items;
final GlobalKey stackKey;

GZXDropDownHeader({
Key key,
@required this.items,
@required this.controller,
@required this.stackKey,
Key? key,
required this.items,
required this.controller,
required this.stackKey,
this.style = const TextStyle(color: Color(0xFF666666), fontSize: 13),
this.dropDownStyle,
this.height = 40,
Expand All @@ -47,11 +47,11 @@ class GZXDropDownHeader extends StatefulWidget {

class _GZXDropDownHeaderState extends State<GZXDropDownHeader> with SingleTickerProviderStateMixin {
bool _isShowDropDownItemWidget = false;
double _screenWidth;
int _menuCount;
late double _screenWidth;
late int _menuCount;
GlobalKey _keyDropDownHeader = GlobalKey();
TextStyle _dropDownStyle;
Color _iconDropDownColor;
TextStyle? _dropDownStyle;
Color? _iconDropDownColor;

@override
void initState() {
Expand Down Expand Up @@ -109,9 +109,9 @@ class _GZXDropDownHeaderState extends State<GZXDropDownHeader> with SingleTicker

return GestureDetector(
onTap: () {
final RenderBox overlay = widget.stackKey.currentContext.findRenderObject();
final RenderBox? overlay = widget.stackKey.currentContext!.findRenderObject() as RenderBox?;

final RenderBox dropDownItemRenderBox = _keyDropDownHeader.currentContext.findRenderObject();
final RenderBox dropDownItemRenderBox = _keyDropDownHeader.currentContext!.findRenderObject() as RenderBox;

var position = dropDownItemRenderBox.localToGlobal(Offset.zero, ancestor: overlay);
// print("POSITION : $position ");
Expand All @@ -134,7 +134,7 @@ class _GZXDropDownHeaderState extends State<GZXDropDownHeader> with SingleTicker
}

if (widget.onItemTap != null) {
widget.onItemTap(index);
widget.onItemTap!(index);
}

setState(() {});
Expand All @@ -158,7 +158,7 @@ class _GZXDropDownHeaderState extends State<GZXDropDownHeader> with SingleTicker
),
Icon(
!_isShowDropDownItemWidget ? item.iconData ?? Icons.arrow_drop_down : item.iconData ?? Icons.arrow_drop_up,
color: _isShowDropDownItemWidget ? _iconDropDownColor : item?.style?.color ?? widget.iconColor,
color: _isShowDropDownItemWidget ? _iconDropDownColor : item.style?.color ?? widget.iconColor,
size: item.iconSize ?? widget.iconSize,
),
],
Expand All @@ -183,8 +183,8 @@ class _GZXDropDownHeaderState extends State<GZXDropDownHeader> with SingleTicker

class GZXDropDownHeaderItem {
final String title;
final IconData iconData;
final double iconSize;
final TextStyle style;
final IconData? iconData;
final double? iconSize;
final TextStyle? style;
GZXDropDownHeaderItem(this.title, {this.iconData, this.iconSize, this.style});
}
50 changes: 25 additions & 25 deletions lib/src/gzx_dropdown_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ class GZXDropdownMenuBuilder {
final Widget dropDownWidget;
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);
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;
final DropdownMenuChange? dropdownMenuChanging;
final DropdownMenuChange? dropdownMenuChanged;

const GZXDropDownMenu(
{Key key,
@required this.controller,
@required this.menus,
{Key? key,
required this.controller,
required this.menus,
this.animationMilliseconds = 500,
this.maskColor = const Color.fromRGBO(0, 0, 0, 0.5),
this.dropdownMenuChanging,
Expand All @@ -37,14 +37,14 @@ class _GZXDropDownMenuState extends State<GZXDropDownMenu> with SingleTickerProv
bool _isShowDropDownItemWidget = false;
bool _isShowMask = false;
bool _isControllerDisposed = false;
Animation<double> _animation;
AnimationController _controller;
Animation<double>? _animation;
AnimationController? _controller;

double _maskColorOpacity;
late double _maskColorOpacity;

double _dropDownHeight;
double? _dropDownHeight;

int _currentMenuIndex;
int? _currentMenuIndex;

@override
void initState() {
Expand All @@ -64,38 +64,38 @@ class _GZXDropDownMenuState extends State<GZXDropDownMenu> with SingleTickerProv
@override
Widget build(BuildContext context) {
// print('_GZXDropDownMenuState.build');
_controller.duration = Duration(milliseconds: widget.animationMilliseconds);
_controller!.duration = Duration(milliseconds: widget.animationMilliseconds);
return _buildDropDownWidget();
}

dispose() {
_animation?.removeListener(_animationListener);
_animation?.removeStatusListener(_animationStatusListener);
widget.controller?.removeListener(_onController);
widget.controller.removeListener(_onController);
_controller?.dispose();
_isControllerDisposed = true;
super.dispose();
}

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

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

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

_animation?.removeListener(_animationListener);
_animation?.removeStatusListener(_animationStatusListener);
_animation = new Tween(begin: 0.0, end: _dropDownHeight).animate(_controller)
_animation = new Tween(begin: 0.0, end: _dropDownHeight).animate(_controller!)
..addListener(_animationListener)
..addStatusListener(_animationStatusListener);

Expand All @@ -104,11 +104,11 @@ class _GZXDropDownMenuState extends State<GZXDropDownMenu> with SingleTickerProv
// print('${widget.controller.isShow}');

if (widget.controller.isShow) {
_controller.forward();
_controller!.forward();
} else if (widget.controller.isShowHideAnimation) {
_controller.reverse();
_controller!.reverse();
} else {
_controller.value = 0;
_controller!.value = 0;
}
}

Expand All @@ -118,7 +118,7 @@ class _GZXDropDownMenuState extends State<GZXDropDownMenu> with SingleTickerProv
// print('dismissed');
_isShowMask = false;
if (widget.dropdownMenuChanged != null) {
widget.dropdownMenuChanged(false, _currentMenuIndex);
widget.dropdownMenuChanged!(false, _currentMenuIndex);
}
break;
case AnimationStatus.forward:
Expand All @@ -130,14 +130,14 @@ class _GZXDropDownMenuState extends State<GZXDropDownMenu> with SingleTickerProv
case AnimationStatus.completed:
// print('completed');
if (widget.dropdownMenuChanged != null) {
widget.dropdownMenuChanged(true, _currentMenuIndex);
widget.dropdownMenuChanged!(true, _currentMenuIndex);
}
break;
}
}

void _animationListener() {
var heightScale = _animation.value / _dropDownHeight;
var heightScale = _animation!.value / _dropDownHeight!;
_maskColorOpacity = widget.maskColor.opacity * heightScale;
// print('$_maskColorOpacity');
//这行如果不写,没有动画效果
Expand Down Expand Up @@ -180,7 +180,7 @@ class _GZXDropDownMenuState extends State<GZXDropDownMenu> with SingleTickerProv
Container(
color: Colors.white,
width: MediaQuery.of(context).size.width,
height: _animation == null ? 0 : _animation.value,
height: _animation == null ? 0 : _animation!.value,
child: widget.menus[menuIndex].dropDownWidget,
),
_mask()
Expand Down
4 changes: 2 additions & 2 deletions lib/src/gzx_dropdown_menu_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import 'package:flutter/foundation.dart';
enum DropDownType { current, another }

class GZXDropdownMenuController extends ChangeNotifier {
double dropDownHeaderHeight;
double? dropDownHeaderHeight;

int menuIndex = 0;

bool isShow = false;

bool isShowHideAnimation = false;

DropDownType dropDownType;
DropDownType? dropDownType;

void show(int index) {
isShow = true;
Expand Down
Loading

0 comments on commit dfe943e

Please sign in to comment.