Skip to content

Commit

Permalink
211108_[1.0.1]_fix issue 5 and 7
Browse files Browse the repository at this point in the history
  • Loading branch information
whatamelon committed Nov 8, 2021
1 parent 31e20e9 commit 90db6df
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 96 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
## [1.0.1] - 2021-11-08

* Fix issue https://github.com/whatamelon/flutter_dropdown_below/issues/5
* Pull request https://github.com/whatamelon/flutter_dropdown_below/pull/8 this commit solving https://github.com/whatamelon/flutter_dropdown_below/issues/7 this issue by @darker-dubai

## [1.0.0] - 2021-07-28

* Fix issue https://github.com/whatamelon/flutter_dropdown_below/issues/4#issue-953987467
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>8.0</string>
<string>9.0</string>
</dict>
</plist>
6 changes: 3 additions & 3 deletions example/ios/Runner.xcodeproj/project.pbxproj
Expand Up @@ -280,7 +280,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -362,7 +362,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -411,7 +411,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
137 changes: 76 additions & 61 deletions example/lib/dropdownBelowTest.dart
@@ -1,3 +1,4 @@

import 'dart:math' as math;

import 'package:flutter/material.dart';
Expand All @@ -19,7 +20,7 @@ const EdgeInsets _kAlignedMenuMargin = EdgeInsets.zero;
/// *[_kAlignedMenuMargin] which is dropdown item's default margin
const EdgeInsetsGeometry _kUnalignedMenuMargin =
EdgeInsetsDirectional.only(start: 16.0, end: 24.0);
EdgeInsetsDirectional.only(start: 16.0, end: 24.0);

/// *[_kAlignedMenuMargin] which is dropdown item's default margin for align rule.
Expand All @@ -30,10 +31,10 @@ class _DropdownMenuPainter extends CustomPainter {
this.selectedIndex,
this.resize,
}) : _painter = BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
boxShadow: kElevationToShadow[elevation!])
.createBoxPainter(),
color: color,
borderRadius: BorderRadius.circular(5),
boxShadow: kElevationToShadow[elevation!])
.createBoxPainter(),
super(repaint: resize);

final Color? color;
Expand Down Expand Up @@ -67,7 +68,7 @@ class _DropdownMenuPainter extends CustomPainter {

final Tween<double> bottom = Tween<double>(
begin:
(top.begin! + _kMenuItemHeight).clamp(_kMenuItemHeight, size.height),
(top.begin! + _kMenuItemHeight).clamp(_kMenuItemHeight, size.height),
end: size.height,
);

Expand Down Expand Up @@ -95,7 +96,7 @@ class _DropdownScrollBehavior extends ScrollBehavior {

@override
Widget buildViewportChrome(
BuildContext context, Widget child, AxisDirection axisDirection) =>
BuildContext context, Widget child, AxisDirection axisDirection) =>
child;

@override
Expand All @@ -107,12 +108,14 @@ class _DropdownMenu<T> extends StatefulWidget {
const _DropdownMenu({
Key? key,
this.padding,
this.dropdownColor,
this.route,
}) : super(key: key);

final _DropdownRoute<T>? route;

final EdgeInsets? padding;
final Color? dropdownColor;

@override
_DropdownMenuState<T> createState() => _DropdownMenuState<T>();
Expand Down Expand Up @@ -140,7 +143,7 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
@override
Widget build(BuildContext context) {
final MaterialLocalizations localizations =
MaterialLocalizations.of(context);
MaterialLocalizations.of(context);
final _DropdownRoute<T> route = widget.route!;
final double unit = 0.5 / (route.items!.length + 1.5);
final List<Widget> children = <Widget>[];
Expand Down Expand Up @@ -174,7 +177,7 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
opacity: _fadeOpacity,
child: CustomPaint(
painter: _DropdownMenuPainter(
color: Colors.white,
color: widget.dropdownColor ?? Colors.white,
elevation: 2,
selectedIndex: route.selectedIndex,
resize: _resize,
Expand Down Expand Up @@ -230,8 +233,8 @@ class _DropdownMenuRouteLayout<T> extends SingleChildLayoutDelegate {
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
final double maxHeight =
math.max(0.0, constraints.maxHeight - 2 * _kMenuItemHeight);
return new BoxConstraints(
math.max(0.0, constraints.maxHeight - 2 * _kMenuItemHeight);
return BoxConstraints(
minWidth: itemWidth!,
maxWidth: itemWidth!,
minHeight: 0.0,
Expand All @@ -258,7 +261,7 @@ class _DropdownMenuRouteLayout<T> extends SingleChildLayoutDelegate {
left = buttonRect!.left.clamp(0.0, size.width - childSize.width);
break;
}
return new Offset(left + 15, menuTop + 13);
return Offset(left + 15, menuTop + 13);
}

@override
Expand Down Expand Up @@ -293,6 +296,7 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
this.padding,
this.buttonRect,
this.selectedIndex,
this.dropdownColor,
this.elevation = 8,
required this.style,
this.barrierLabel,
Expand All @@ -315,6 +319,7 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
final int elevation;

final TextStyle style;
final Color? dropdownColor;

ScrollController? scrollController;

Expand Down Expand Up @@ -348,8 +353,9 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
double menuTop = (buttonTop - selectedItemOffset) -
(_kMenuItemHeight - buttonRect!.height) / 2.0;
const double topPreferredLimit = _kMenuItemHeight;
if (menuTop < topPreferredLimit)
if (menuTop < topPreferredLimit) {
menuTop = math.min(buttonTop, topPreferredLimit);
}
double bottom = menuTop + menuHeight;
final double bottomPreferredLimit = screenHeight - _kMenuItemHeight;
if (bottom > bottomPreferredLimit) {
Expand All @@ -359,19 +365,20 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {

if (scrollController == null) {
double scrollOffset = 0.0;
if (preferredMenuHeight > maxMenuHeight)
if (preferredMenuHeight > maxMenuHeight) {
scrollOffset = selectedItemOffset - (buttonTop - menuTop);
scrollController =
new ScrollController(initialScrollOffset: scrollOffset);
}
scrollController = ScrollController(initialScrollOffset: scrollOffset);
}

final TextDirection textDirection = Directionality.of(context);
Widget menu = _DropdownMenu<T>(
route: this,
padding: padding!.resolve(textDirection),
dropdownColor: dropdownColor,
);

return new MediaQuery.removePadding(
return MediaQuery.removePadding(
context: context,
removeTop: true,
removeBottom: true,
Expand Down Expand Up @@ -402,25 +409,26 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
class DropdownBelow<T> extends StatefulWidget {
DropdownBelow(
{Key? key,
required this.items,
this.value,
this.hint,
this.itemTextstyle,
this.itemWidth,
this.boxHeight,
this.boxWidth,
this.boxPadding,
this.boxTextstyle,
required this.onChanged,
this.boxDecoration,
this.elevation = 8,
this.isDense = false,
this.icon})
required this.items,
this.value,
this.hint,
this.itemTextstyle,
this.itemWidth,
this.boxHeight,
this.boxWidth,
this.boxPadding,
this.boxTextstyle,
required this.onChanged,
this.boxDecoration,
this.dropdownColor,
this.elevation = 8,
this.isDense = false,
this.icon})
: assert(value == null ||
items
.where((DropdownMenuItem<T> item) => item.value == value)
.length ==
1),
items
.where((DropdownMenuItem<T> item) => item.value == value)
.length ==
1),
super(key: key);

final List<DropdownMenuItem<T>> items;
Expand Down Expand Up @@ -473,8 +481,14 @@ class DropdownBelow<T> extends StatefulWidget {

final bool isDense;

/// The background color of the dropdown.
///
/// If it is not provided, the theme's [Colors.white] will be used
/// instead.
final Color? dropdownColor;

@override
_DropdownBelowState<T> createState() => new _DropdownBelowState<T>();
_DropdownBelowState<T> createState() => _DropdownBelowState<T>();
}

class _DropdownBelowState<T> extends State<DropdownBelow<T>>
Expand All @@ -497,7 +511,7 @@ class _DropdownBelowState<T> extends State<DropdownBelow<T>>

@override
void didChangeMetrics() {
_removeDropdownRoute();
// _removeDropdownRoute();
}

void _removeDropdownRoute() {
Expand All @@ -514,8 +528,8 @@ class _DropdownBelowState<T> extends State<DropdownBelow<T>>
void _updateSelectedIndex() {
assert(widget.value == null ||
widget.items
.where((DropdownMenuItem<T> item) => item.value == widget.value)
.length ==
.where((DropdownMenuItem<T> item) => item.value == widget.value)
.length ==
1);
_selectedIndex = null;
for (int itemIndex = 0; itemIndex < widget.items.length; itemIndex++) {
Expand All @@ -531,18 +545,19 @@ class _DropdownBelowState<T> extends State<DropdownBelow<T>>
final Rect itemRect = itemBox.localToGlobal(Offset.zero) & itemBox.size;
final TextDirection textDirection = Directionality.of(context);
final EdgeInsetsGeometry menuMargin =
ButtonTheme.of(context).alignedDropdown
? _kAlignedMenuMargin
: _kUnalignedMenuMargin;
ButtonTheme.of(context).alignedDropdown
? _kAlignedMenuMargin
: _kUnalignedMenuMargin;

assert(_dropdownRoute == null);
_dropdownRoute = new _DropdownRoute<T>(
_dropdownRoute = _DropdownRoute<T>(
itemWidth: widget.itemWidth,
items: widget.items,
buttonRect: menuMargin.resolve(textDirection).inflateRect(itemRect),
padding: _kMenuItemPadding.resolve(textDirection),
selectedIndex: -1,
elevation: widget.elevation,
dropdownColor: widget.dropdownColor,
style: widget.itemTextstyle ?? TextStyle(),
barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
);
Expand All @@ -551,7 +566,7 @@ class _DropdownBelowState<T> extends State<DropdownBelow<T>>
.then<void>((_DropdownRouteResult<T>? newValue) {
_dropdownRoute = null;
if (!mounted || newValue == null) return;
if (widget.onChanged != null) widget.onChanged(newValue.result);
widget.onChanged(newValue.result);
});
}

Expand All @@ -572,33 +587,33 @@ class _DropdownBelowState<T> extends State<DropdownBelow<T>>
));
}

Widget result = new DefaultTextStyle(
Widget result = DefaultTextStyle(
style: widget.boxTextstyle!,
child: new Container(
child: Container(
decoration: widget.boxDecoration,
width: widget.boxWidth,
padding: widget.boxPadding,
height: widget.boxHeight,
child: widget.icon == null
? IndexedStack(
index: _selectedIndex ?? hintIndex,
alignment: AlignmentDirectional.centerStart,
children: items,
)
: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expanded(
child: IndexedStack(
index: _selectedIndex ?? hintIndex,
alignment: AlignmentDirectional.centerStart,
children: items,
)
: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expanded(
child: IndexedStack(
index: _selectedIndex ?? hintIndex,
alignment: AlignmentDirectional.centerStart,
children: items,
),
),
widget.icon ?? Container()
],
),
),
widget.icon ?? Container()
],
),
),
);

Expand All @@ -618,7 +633,7 @@ class _DropdownBelowState<T> extends State<DropdownBelow<T>>
);
}

return new Semantics(
return Semantics(
button: true,
child: GestureDetector(
onTap: _handleTap, behavior: HitTestBehavior.opaque, child: result),
Expand Down

0 comments on commit 90db6df

Please sign in to comment.