CupertinoContextMenu (iOS 13) (#43918)

Adds the CupertinoContextMenu widget for iOS 13 support.
This commit is contained in:
Justin McCandless
2019-11-05 07:55:54 -08:00
committed by GitHub
parent 28b5cc3825
commit 9734754aad
7 changed files with 1896 additions and 8 deletions

View File

@@ -15,6 +15,8 @@ export 'src/cupertino/app.dart';
export 'src/cupertino/bottom_tab_bar.dart';
export 'src/cupertino/button.dart';
export 'src/cupertino/colors.dart';
export 'src/cupertino/context_menu.dart';
export 'src/cupertino/context_menu_action.dart';
export 'src/cupertino/date_picker.dart';
export 'src/cupertino/dialog.dart';
export 'src/cupertino/icon_theme_data.dart';

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,148 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'colors.dart';
/// A button in a _ContextMenuSheet.
///
/// A typical use case is to pass a [Text] as the [child] here, but be sure to
/// use [TextOverflow.ellipsis] for the [Text.overflow] field if the text may be
/// long, as without it the text will wrap to the next line.
class CupertinoContextMenuAction extends StatefulWidget {
/// Construct a CupertinoContextMenuAction.
const CupertinoContextMenuAction({
Key key,
@required this.child,
this.isDefaultAction = false,
this.isDestructiveAction = false,
this.onPressed,
this.trailingIcon,
}) : assert(child != null),
assert(isDefaultAction != null),
assert(isDestructiveAction != null),
super(key: key);
/// The widget that will be placed inside the action.
final Widget child;
/// Indicates whether this action should receive the style of an emphasized,
/// default action.
final bool isDefaultAction;
/// Indicates whether this action should receive the style of a destructive
/// action.
final bool isDestructiveAction;
/// Called when the action is pressed.
final VoidCallback onPressed;
/// An optional icon to display to the right of the child.
///
/// Will be colored in the same way as the [TextStyle] used for [child] (for
/// example, if using [isDestructiveAction]).
final IconData trailingIcon;
@override
_CupertinoContextMenuActionState createState() => _CupertinoContextMenuActionState();
}
class _CupertinoContextMenuActionState extends State<CupertinoContextMenuAction> {
static const Color _kBackgroundColor = Color(0xFFEEEEEE);
static const Color _kBackgroundColorPressed = Color(0xFFDDDDDD);
static const double _kButtonHeight = 56.0;
static const TextStyle _kActionSheetActionStyle = TextStyle(
fontFamily: '.SF UI Text',
inherit: false,
fontSize: 20.0,
fontWeight: FontWeight.w400,
color: CupertinoColors.black,
textBaseline: TextBaseline.alphabetic,
);
final GlobalKey _globalKey = GlobalKey();
bool _isPressed = false;
void onTapDown(TapDownDetails details) {
setState(() {
_isPressed = true;
});
}
void onTapUp(TapUpDetails details) {
setState(() {
_isPressed = false;
});
}
void onTapCancel() {
setState(() {
_isPressed = false;
});
}
TextStyle get _textStyle {
if (widget.isDefaultAction) {
return _kActionSheetActionStyle.copyWith(
fontWeight: FontWeight.w600,
);
}
if (widget.isDestructiveAction) {
return _kActionSheetActionStyle.copyWith(
color: CupertinoColors.destructiveRed,
);
}
return _kActionSheetActionStyle;
}
@override
Widget build(BuildContext context) {
return GestureDetector(
key: _globalKey,
onTapDown: onTapDown,
onTapUp: onTapUp,
onTapCancel: onTapCancel,
onTap: widget.onPressed,
behavior: HitTestBehavior.opaque,
child: ConstrainedBox(
constraints: const BoxConstraints(
minHeight: _kButtonHeight,
),
child: Semantics(
button: true,
child: Container(
decoration: BoxDecoration(
color: _isPressed ? _kBackgroundColorPressed : _kBackgroundColor,
border: const Border(
bottom: BorderSide(width: 1.0, color: _kBackgroundColorPressed),
),
),
padding: const EdgeInsets.symmetric(
vertical: 16.0,
horizontal: 10.0,
),
child: DefaultTextStyle(
style: _textStyle,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: widget.child,
),
if (widget.trailingIcon != null)
Icon(
widget.trailingIcon,
color: CupertinoColors.destructiveRed,
),
],
),
),
),
),
),
);
}
}

View File

@@ -4,7 +4,7 @@
import 'dart:async';
import 'dart:math';
import 'dart:ui' show lerpDouble;
import 'dart:ui' show lerpDouble, ImageFilter;
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
@@ -794,11 +794,15 @@ class _CupertinoEdgeShadowPainter extends BoxPainter {
class _CupertinoModalPopupRoute<T> extends PopupRoute<T> {
_CupertinoModalPopupRoute({
this.builder,
this.barrierLabel,
this.barrierColor,
this.barrierLabel,
this.builder,
ImageFilter filter,
RouteSettings settings,
}) : super(settings: settings);
}) : super(
filter: filter,
settings: settings,
);
final WidgetBuilder builder;
@@ -890,14 +894,16 @@ class _CupertinoModalPopupRoute<T> extends PopupRoute<T> {
Future<T> showCupertinoModalPopup<T>({
@required BuildContext context,
@required WidgetBuilder builder,
ImageFilter filter,
bool useRootNavigator = true,
}) {
assert(useRootNavigator != null);
return Navigator.of(context, rootNavigator: useRootNavigator).push(
_CupertinoModalPopupRoute<T>(
builder: builder,
barrierLabel: 'Dismiss',
barrierColor: CupertinoDynamicColor.resolve(_kModalBarrierColor, context),
barrierLabel: 'Dismiss',
builder: builder,
filter: filter,
),
);
}

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
@@ -735,7 +736,15 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
/// Creates a route that blocks interaction with previous routes.
ModalRoute({
RouteSettings settings,
}) : super(settings: settings);
ui.ImageFilter filter,
}) : _filter = filter,
super(settings: settings);
/// The filter to add to the barrier.
///
/// If given, this filter will be applied to the modal barrier using
/// [BackdropFilter]. This allows blur effects, for example.
final ui.ImageFilter _filter;
// The API for general users of this class
@@ -1286,6 +1295,12 @@ abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T
barrierSemanticsDismissible: semanticsDismissible,
);
}
if (_filter != null) {
barrier = BackdropFilter(
filter: _filter,
child: barrier,
);
}
return IgnorePointer(
ignoring: animation.status == AnimationStatus.reverse || // changedInternalState is called when this updates
animation.status == AnimationStatus.dismissed, // dismissed is possible when doing a manual pop gesture
@@ -1321,7 +1336,11 @@ abstract class PopupRoute<T> extends ModalRoute<T> {
/// Initializes the [PopupRoute].
PopupRoute({
RouteSettings settings,
}) : super(settings: settings);
ui.ImageFilter filter,
}) : super(
filter: filter,
settings: settings,
);
@override
bool get opaque => false;

View File

@@ -0,0 +1,64 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/cupertino.dart';
void main() {
// Constants taken from _ContextMenuActionState.
const Color _kBackgroundColor = Color(0xFFEEEEEE);
const Color _kBackgroundColorPressed = Color(0xFFDDDDDD);
Widget _getApp([VoidCallback onPressed]) {
final UniqueKey actionKey = UniqueKey();
final CupertinoContextMenuAction action = CupertinoContextMenuAction(
key: actionKey,
child: const Text('I am a CupertinoContextMenuAction'),
onPressed: onPressed,
);
return CupertinoApp(
home: CupertinoPageScaffold(
child: Center(
child: action,
),
),
);
}
BoxDecoration _getDecoration(WidgetTester tester) {
final Finder finder = find.descendant(
of: find.byType(CupertinoContextMenuAction),
matching: find.byType(Container),
);
expect(finder, findsOneWidget);
final Container container = tester.widget(finder);
return container.decoration;
}
testWidgets('responds to taps', (WidgetTester tester) async {
bool wasPressed = false;
await tester.pumpWidget(_getApp(() {
wasPressed = true;
}));
expect(wasPressed, false);
await tester.tap(find.byType(CupertinoContextMenuAction));
expect(wasPressed, true);
});
testWidgets('turns grey when pressed and held', (WidgetTester tester) async {
await tester.pumpWidget(_getApp());
expect(_getDecoration(tester).color, _kBackgroundColor);
final Offset actionCenter = tester.getCenter(find.byType(CupertinoContextMenuAction));
final TestGesture gesture = await tester.startGesture(actionCenter);
await tester.pump();
expect(_getDecoration(tester).color, _kBackgroundColorPressed);
await gesture.up();
await tester.pump();
expect(_getDecoration(tester).color, _kBackgroundColor);
});
}

View File

@@ -0,0 +1,366 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/cupertino.dart';
void main() {
final TestWidgetsFlutterBinding binding =
TestWidgetsFlutterBinding.ensureInitialized();
const double _kOpenScale = 1.1;
Widget _getChild() {
return Container(
width: 300.0,
height: 100.0,
color: CupertinoColors.activeOrange,
);
}
Widget _getContextMenu({
Alignment alignment = Alignment.center,
Size screenSize = const Size(800.0, 600.0),
Widget child,
}) {
return CupertinoApp(
home: CupertinoPageScaffold(
child: MediaQuery(
data: MediaQueryData(size: screenSize),
child: Align(
alignment: alignment,
child: CupertinoContextMenu(
actions: <CupertinoContextMenuAction>[
CupertinoContextMenuAction(
child: Text('CupertinoContextMenuAction $alignment'),
),
],
child: child ?? _getChild(),
),
),
),
),
);
}
// Finds the child widget that is rendered inside of _DecoyChild.
Finder _findDecoyChild(Widget child) {
return find.descendant(
of: find.byType(ShaderMask),
matching: find.byWidget(child),
);
}
// Finds the child widget rendered inside of _ContextMenuRouteStatic.
Finder _findStatic() {
return find.descendant(
of: find.byType(CupertinoApp),
matching: find.byWidgetPredicate((Widget w) => '${w.runtimeType}' == '_ContextMenuRouteStatic'),
);
}
Finder _findStaticChild(Widget child) {
return find.descendant(
of: _findStatic(),
matching: find.byWidget(child),
);
}
group('CupertinoContextMenu before and during opening', () {
testWidgets('An unopened CupertinoContextMenu renders child in the same place as without', (WidgetTester tester) async {
// Measure the child in the scene with no CupertinoContextMenu.
final Widget child = _getChild();
await tester.pumpWidget(
CupertinoApp(
home: CupertinoPageScaffold(
child: Center(
child: child,
),
),
),
);
final Rect childRect = tester.getRect(find.byWidget(child));
// When wrapped in a CupertinoContextMenu, the child is rendered in the same Rect.
await tester.pumpWidget(_getContextMenu(child: child));
expect(find.byWidget(child), findsOneWidget);
expect(tester.getRect(find.byWidget(child)), childRect);
});
testWidgets('Can open CupertinoContextMenu by tap and hold', (WidgetTester tester) async {
final Widget child = _getChild();
await tester.pumpWidget(_getContextMenu(child: child));
expect(find.byWidget(child), findsOneWidget);
final Rect childRect = tester.getRect(find.byWidget(child));
expect(find.byType(ShaderMask), findsNothing);
// Start a press on the child.
final TestGesture gesture = await tester.startGesture(childRect.center);
await tester.pump();
// The _DecoyChild is showing directly on top of the child.
expect(_findDecoyChild(child), findsOneWidget);
Rect decoyChildRect = tester.getRect(_findDecoyChild(child));
expect(childRect, equals(decoyChildRect));
// After a small delay, the _DecoyChild has begun to animate.
await tester.pump(const Duration(milliseconds: 100));
decoyChildRect = tester.getRect(_findDecoyChild(child));
expect(childRect, isNot(equals(decoyChildRect)));
// Eventually the decoy fully scales by _kOpenSize.
await tester.pump(const Duration(milliseconds: 500));
decoyChildRect = tester.getRect(_findDecoyChild(child));
expect(childRect, isNot(equals(decoyChildRect)));
expect(decoyChildRect.width, childRect.width * _kOpenScale);
// Then the CupertinoContextMenu opens.
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
expect(_findStatic(), findsOneWidget);
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/44152
});
group('CupertinoContextMenu when open', () {
testWidgets('Can close CupertinoContextMenu by background tap', (WidgetTester tester) async {
final Widget child = _getChild();
await tester.pumpWidget(_getContextMenu(child: child));
// Open the CupertinoContextMenu
final Rect childRect = tester.getRect(find.byWidget(child));
final TestGesture gesture = await tester.startGesture(childRect.center);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
expect(_findStatic(), findsOneWidget);
// Tap and ensure that the CupertinoContextMenu is closed.
await tester.tapAt(const Offset(1.0, 1.0));
await tester.pumpAndSettle();
expect(_findStatic(), findsNothing);
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/44152
testWidgets('Can close CupertinoContextMenu by dragging down', (WidgetTester tester) async {
final Widget child = _getChild();
await tester.pumpWidget(_getContextMenu(child: child));
// Open the CupertinoContextMenu
final Rect childRect = tester.getRect(find.byWidget(child));
final TestGesture gesture = await tester.startGesture(childRect.center);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
expect(_findStatic(), findsOneWidget);
// Drag down not far enough and it bounces back and doesn't close.
expect(_findStaticChild(child), findsOneWidget);
Offset staticChildCenter = tester.getCenter(_findStaticChild(child));
TestGesture swipeGesture = await tester.startGesture(staticChildCenter);
await swipeGesture.moveBy(
const Offset(0.0, 100.0),
timeStamp: const Duration(milliseconds: 100),
);
await tester.pump();
await swipeGesture.up();
await tester.pump();
expect(tester.getCenter(_findStaticChild(child)).dy, greaterThan(staticChildCenter.dy));
await tester.pumpAndSettle();
expect(tester.getCenter(_findStaticChild(child)), equals(staticChildCenter));
expect(_findStatic(), findsOneWidget);
// Drag down far enough and it does close.
expect(_findStaticChild(child), findsOneWidget);
staticChildCenter = tester.getCenter(_findStaticChild(child));
swipeGesture = await tester.startGesture(staticChildCenter);
await swipeGesture.moveBy(
const Offset(0.0, 200.0),
timeStamp: const Duration(milliseconds: 100),
);
await tester.pump();
await swipeGesture.up();
await tester.pumpAndSettle();
expect(_findStatic(), findsNothing);
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/44152
testWidgets('Can close CupertinoContextMenu by flinging down', (WidgetTester tester) async {
final Widget child = _getChild();
await tester.pumpWidget(_getContextMenu(child: child));
// Open the CupertinoContextMenu
final Rect childRect = tester.getRect(find.byWidget(child));
final TestGesture gesture = await tester.startGesture(childRect.center);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
expect(_findStatic(), findsOneWidget);
// Fling up and nothing happens.
expect(_findStaticChild(child), findsOneWidget);
await tester.fling(_findStaticChild(child), const Offset(0.0, -100.0), 1000.0);
await tester.pumpAndSettle();
expect(_findStaticChild(child), findsOneWidget);
// Fling down to close the menu.
expect(_findStaticChild(child), findsOneWidget);
await tester.fling(_findStaticChild(child), const Offset(0.0, 100.0), 1000.0);
await tester.pumpAndSettle();
expect(_findStatic(), findsNothing);
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/44152
testWidgets('Backdrop is added using ModalRoute\'s filter parameter', (WidgetTester tester) async {
final Widget child = _getChild();
await tester.pumpWidget(_getContextMenu(child: child));
expect(find.byType(BackdropFilter), findsNothing);
// Open the CupertinoContextMenu
final Rect childRect = tester.getRect(find.byWidget(child));
final TestGesture gesture = await tester.startGesture(childRect.center);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
expect(_findStatic(), findsOneWidget);
expect(find.byType(BackdropFilter), findsOneWidget);
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/44152
});
group('Open layout differs depending on child\'s position on screen', () {
testWidgets('Portrait', (WidgetTester tester) async {
const Size portraitScreenSize = Size(600.0, 800.0);
await binding.setSurfaceSize(portraitScreenSize);
// Pump a CupertinoContextMenu in the center of the screen and open it.
final Widget child = _getChild();
await tester.pumpWidget(_getContextMenu(
alignment: Alignment.center,
screenSize: portraitScreenSize,
child: child,
));
expect(find.byType(CupertinoContextMenuAction), findsNothing);
Rect childRect = tester.getRect(find.byWidget(child));
TestGesture gesture = await tester.startGesture(childRect.center);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
// The position of the action is in the center of the screen.
expect(find.byType(CupertinoContextMenuAction), findsOneWidget);
final Offset center = tester.getTopLeft(find.byType(CupertinoContextMenuAction));
// Close the CupertinoContextMenu.
await tester.tapAt(const Offset(1.0, 1.0));
await tester.pumpAndSettle();
expect(_findStatic(), findsNothing);
// Pump a CupertinoContextMenu on the left of the screen and open it.
await tester.pumpWidget(_getContextMenu(
alignment: Alignment.centerLeft,
screenSize: portraitScreenSize,
child: child,
));
expect(find.byType(CupertinoContextMenuAction), findsNothing);
await tester.pumpAndSettle();
childRect = tester.getRect(find.byWidget(child));
gesture = await tester.startGesture(childRect.center);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
// The position of the action is on the left of the screen.
expect(find.byType(CupertinoContextMenuAction), findsOneWidget);
final Offset left = tester.getTopLeft(find.byType(CupertinoContextMenuAction));
expect(left.dx, lessThan(center.dx));
// Close the CupertinoContextMenu.
await tester.tapAt(const Offset(1.0, 1.0));
await tester.pumpAndSettle();
expect(_findStatic(), findsNothing);
// Pump a CupertinoContextMenu on the right of the screen and open it.
await tester.pumpWidget(_getContextMenu(
alignment: Alignment.centerRight,
screenSize: portraitScreenSize,
child: child,
));
expect(find.byType(CupertinoContextMenuAction), findsNothing);
childRect = tester.getRect(find.byWidget(child));
gesture = await tester.startGesture(childRect.center);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
// The position of the action is on the right of the screen.
expect(find.byType(CupertinoContextMenuAction), findsOneWidget);
final Offset right = tester.getTopLeft(find.byType(CupertinoContextMenuAction));
expect(right.dx, greaterThan(center.dx));
// Set the screen back to its normal size.
await binding.setSurfaceSize(const Size(800.0, 600.0));
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/44152
testWidgets('Landscape', (WidgetTester tester) async {
// Pump a CupertinoContextMenu in the center of the screen and open it.
final Widget child = _getChild();
await tester.pumpWidget(_getContextMenu(
alignment: Alignment.center,
child: child,
));
expect(find.byType(CupertinoContextMenuAction), findsNothing);
Rect childRect = tester.getRect(find.byWidget(child));
TestGesture gesture = await tester.startGesture(childRect.center);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
// Landscape doesn't support a centered action list, so the action is on
// the left side of the screen.
expect(find.byType(CupertinoContextMenuAction), findsOneWidget);
final Offset center = tester.getTopLeft(find.byType(CupertinoContextMenuAction));
// Close the CupertinoContextMenu.
await tester.tapAt(const Offset(1.0, 1.0));
await tester.pumpAndSettle();
expect(_findStatic(), findsNothing);
// Pump a CupertinoContextMenu on the left of the screen and open it.
await tester.pumpWidget(_getContextMenu(
alignment: Alignment.centerLeft,
child: child,
));
expect(find.byType(CupertinoContextMenuAction), findsNothing);
childRect = tester.getRect(find.byWidget(child));
gesture = await tester.startGesture(childRect.center);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
// The position of the action is on the right of the screen, which is the
// same as for center aligned children in landscape.
expect(find.byType(CupertinoContextMenuAction), findsOneWidget);
final Offset left = tester.getTopLeft(find.byType(CupertinoContextMenuAction));
expect(left.dx, equals(center.dx));
// Close the CupertinoContextMenu.
await tester.tapAt(const Offset(1.0, 1.0));
await tester.pumpAndSettle();
expect(_findStatic(), findsNothing);
// Pump a CupertinoContextMenu on the right of the screen and open it.
await tester.pumpWidget(_getContextMenu(
alignment: Alignment.centerRight,
child: child,
));
expect(find.byType(CupertinoContextMenuAction), findsNothing);
childRect = tester.getRect(find.byWidget(child));
gesture = await tester.startGesture(childRect.center);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
// The position of the action is on the left of the screen.
expect(find.byType(CupertinoContextMenuAction), findsOneWidget);
final Offset right = tester.getTopLeft(find.byType(CupertinoContextMenuAction));
expect(right.dx, lessThan(left.dx));
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/44152
});
}