Skip to content

Instantly share code, notes, and snippets.

@cpboyd
Created October 6, 2020 15:39
Show Gist options
  • Save cpboyd/864524e80c80aec9aa400ec09652ca24 to your computer and use it in GitHub Desktop.
Save cpboyd/864524e80c80aec9aa400ec09652ca24 to your computer and use it in GitHub Desktop.
Cupertino Bottom Sheet Route for Router/Navigation 2.0
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
const double _behind_widget_visible_height = 10;
const Radius _default_top_radius = Radius.circular(12);
/// Cupertino Bottom Sheet Container
///
/// Clip the child widget to rectangle with top rounded corners and adds
/// top padding(+safe area padding). This padding [_behind_widget_visible_height]
/// is the height that will be displayed from previous route.
class _CupertinoBottomSheetContainer extends StatelessWidget {
final Widget child;
final Color backgroundColor;
final Radius topRadius;
const _CupertinoBottomSheetContainer(
{Key key, this.child, this.backgroundColor, @required this.topRadius})
: super(key: key);
@override
Widget build(BuildContext context) {
final topSafeAreaPadding = MediaQuery.of(context).padding.top;
final topPadding = _behind_widget_visible_height + topSafeAreaPadding;
final shadow =
BoxShadow(blurRadius: 10, color: Colors.black12, spreadRadius: 5);
final _backgroundColor =
backgroundColor ?? CupertinoTheme.of(context).scaffoldBackgroundColor;
return Padding(
padding: EdgeInsets.only(top: topPadding),
child: ClipRRect(
borderRadius: BorderRadius.vertical(top: topRadius),
child: Container(
decoration:
BoxDecoration(color: _backgroundColor, boxShadow: [shadow]),
width: double.infinity,
child: MediaQuery.removePadding(
context: context,
removeTop: true, //Remove top Safe Area
child: child,
),
),
),
);
}
}
Widget _defaultContainerBuilder(
BuildContext context, Animation<double> animation, Widget child) {
return _CupertinoBottomSheetContainer(
child: child,
backgroundColor: Colors.transparent,
topRadius: _default_top_radius,
);
}
class SimpleCupertinoModalRoute extends CupertinoModalBottomSheetRoute {
SimpleCupertinoModalRoute({
ScrollWidgetBuilder builder,
WidgetWithChildBuilder containerBuilder = _defaultContainerBuilder,
double closeProgressThreshold,
String barrierLabel,
double elevation,
ShapeBorder shape,
Clip clipBehavior,
AnimationController secondAnimationController,
Curve animationCurve,
Color modalBarrierColor,
bool bounce = true,
bool isDismissible = true,
bool enableDrag = true,
bool expanded = false,
Duration duration,
RouteSettings settings,
ScrollController scrollController,
Color transitionBackgroundColor,
Radius topRadius = _default_top_radius,
Curve previousRouteAnimationCurve,
}) : assert(expanded != null),
assert(isDismissible != null),
assert(enableDrag != null),
super(
builder: builder,
containerBuilder: containerBuilder,
closeProgressThreshold: closeProgressThreshold,
barrierLabel: barrierLabel,
elevation: elevation,
shape: shape,
clipBehavior: clipBehavior,
secondAnimationController: secondAnimationController,
animationCurve: animationCurve,
modalBarrierColor: modalBarrierColor,
bounce: bounce,
isDismissible: isDismissible,
enableDrag: enableDrag,
expanded: expanded,
duration: duration,
settings: settings,
scrollController: scrollController,
transitionBackgroundColor: transitionBackgroundColor,
topRadius: topRadius,
previousRouteAnimationCurve: previousRouteAnimationCurve,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment