diff --git a/packages/flutter/lib/src/cupertino/dialog.dart b/packages/flutter/lib/src/cupertino/dialog.dart index 1806f54ec3..edd50f7809 100644 --- a/packages/flutter/lib/src/cupertino/dialog.dart +++ b/packages/flutter/lib/src/cupertino/dialog.dart @@ -9,7 +9,7 @@ library; import 'dart:math' as math; -import 'dart:ui' show ImageFilter; +import 'dart:ui' show ImageFilter, lerpDouble; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; @@ -59,7 +59,8 @@ const TextStyle _kCupertinoDialogActionStyle = TextStyle( // CupertinoActionSheet-specific text styles. const TextStyle _kActionSheetActionStyle = TextStyle( - fontFamily: 'CupertinoSystemText', + // The fontSize and fontWeight may be adjusted when the text is rendered. + fontFamily: 'CupertinoSystemDisplay', inherit: false, fontSize: 17.0, fontWeight: FontWeight.w400, @@ -96,8 +97,14 @@ const double _kActionSheetEdgePadding = 8.0; const double _kActionSheetCancelButtonPadding = 8.0; const double _kActionSheetContentHorizontalPadding = 16.0; const double _kActionSheetContentVerticalPadding = 13.5; -const double _kActionSheetButtonHeight = 57.0; const double _kActionSheetActionsSectionMinHeight = 84.0; +const double _kActionSheetButtonHorizontalPadding = 10.0; + +// According to experimenting on the simulator, the height of action sheet +// buttons is proportional to the font size down to a minimal height. +const double _kActionSheetButtonMinHeight = 57.17; +const double _kActionSheetButtonVerticalPaddingFactor = 0.4; +const double _kActionSheetButtonVerticalPaddingBase = 1.8; // A translucent color that is painted on top of the blurred backdrop as the // dialog's background color @@ -937,9 +944,7 @@ class _CupertinoActionSheetState extends State { } else if (x >= x2) { return y2; } else { - return Tween(begin: y1, end: y2).transform( - (x - x1) / (x2 - x1) - ); + return lerpDouble(y1, y2, (x - x1) / (x2 - x1))!; } } @@ -1128,9 +1133,44 @@ class _CupertinoActionSheetActionState extends State widget.onPressed(); } + // Calculates the font size for action sheet buttons. + // + // The `contextBodySize` is the body font size specified by context. The + // return value is the button font size, including the effect of context font + // scale factor. Divide by context font scale factor before using in a `Text`. + static double _buttonFontSize(double contextBodySize) { + // It is observed that the native action sheet buttons use font sizes that + // deviate from standard HIG specifications in a non-linear way. The following + // table shows the regular body font size vs the button font size: + // + // Text scale | xs | s | m | l | xl | xxl | xxxl | ax1 | ax2 | ax3 | ax4 | ax5 + // Body font | 14 | 15 | 16 | 17 | 19 | 21 | 23 | 28 | 33 | 40 | 47 | 53 + // Button font | 21 | 21 | 21 | 21 | 23 | 24 | 24 | 28 | 33 | 40 | 47 | 53 + + // For very small or very large text, simple rules can be observed. + // For mid-sized text, piecewise linear interpolation is used. + return switch (contextBodySize) { + <= 17 => 21.0, + <= 19 => lerpDouble(21.0, 23.0, (contextBodySize - 17.0)/(19.0 - 17.0))!, + <= 21 => lerpDouble(23.0, 24.0, (contextBodySize - 19.0)/(21.0 - 19.0))!, + <= 24 => 24.0, + _ => contextBodySize, + }; + } + @override Widget build(BuildContext context) { + // The context scale factor is derived from the current body size and the + // standard body size in "large". + const double higLargeBodySize = 17.0; + final double contextBodySize = MediaQuery.textScalerOf(context).scale(higLargeBodySize); + final double contextScaleFactor = contextBodySize / higLargeBodySize; + final double fontSize = _buttonFontSize(contextBodySize); + TextStyle style = _kActionSheetActionStyle.copyWith( + // `Text` will scale the provided font size inside, so its parameter is + // unscaled first. + fontSize: fontSize / contextScaleFactor, color: widget.isDestructiveAction ? CupertinoDynamicColor.resolve(CupertinoColors.systemRed, context) : CupertinoTheme.of(context).primaryColor, @@ -1140,6 +1180,9 @@ class _CupertinoActionSheetActionState extends State style = style.copyWith(fontWeight: FontWeight.w600); } + final double verticalPadding = _kActionSheetButtonVerticalPaddingBase + + fontSize * _kActionSheetButtonVerticalPaddingFactor; + return MouseRegion( cursor: kIsWeb ? SystemMouseCursors.click : MouseCursor.defer, child: MetaData( @@ -1147,15 +1190,17 @@ class _CupertinoActionSheetActionState extends State behavior: HitTestBehavior.opaque, child: ConstrainedBox( constraints: const BoxConstraints( - minHeight: _kActionSheetButtonHeight, + minHeight: _kActionSheetButtonMinHeight, ), child: Semantics( button: true, onTap: widget.onPressed, child: Padding( - padding: const EdgeInsets.symmetric( - vertical: 16.0, - horizontal: 10.0, + padding: EdgeInsets.fromLTRB( + _kActionSheetButtonHorizontalPadding, + verticalPadding, + _kActionSheetButtonHorizontalPadding, + verticalPadding, ), child: DefaultTextStyle( style: style, diff --git a/packages/flutter/test/cupertino/action_sheet_test.dart b/packages/flutter/test/cupertino/action_sheet_test.dart index bd957764ee..b60cf36007 100644 --- a/packages/flutter/test/cupertino/action_sheet_test.dart +++ b/packages/flutter/test/cupertino/action_sheet_test.dart @@ -83,6 +83,98 @@ void main() { await gesture.up(); }); + testWidgets('Button appearance is correct with text scaling', (WidgetTester tester) async { + // Verifies layout of action button in various text scaling by drawing + // buttons in all 12 iOS text scales in one golden image. + + // The following function returns a CupertinoActionSheetAction that: + // * Has a fixed width + // * Is unconstrained in height + // * Is aligned center in a grid of fixed height + // * Is surrounded by a black border + const double buttonWidth = 400; + const double rowHeight = 100; + Widget testButton(double contextBodySize) { + const double standardHigBody = 17.0; + final double contextScaleFactor = contextBodySize / standardHigBody; + return OverrideMediaQuery( + transformer: (MediaQueryData data) { + return data.copyWith( + textScaler: TextScaler.linear(contextScaleFactor), + ); + }, + child: SizedBox( + height: rowHeight, + child: Center( + child: UnconstrainedBox( + child: ConstrainedBox( + constraints: const BoxConstraints.tightFor(width: buttonWidth), + child: DecoratedBox( + decoration: BoxDecoration(border: Border.all()), + child: CupertinoActionSheetAction( + onPressed: () {}, + child: const Text('Button'), + ), + ), + ), + ), + ), + ), + ); + } + + await tester.pumpWidget(CupertinoApp( + home: CupertinoPageScaffold( + child: Center( + child: Column( + children: [ + Row(children: [ + /*xs*/ testButton(14), + /*s*/ testButton(15), + ]), + Row(children: [ + /*m*/ testButton(16), + /*l*/ testButton(17), + ]), + Row(children: [ + /*xl*/ testButton(19), + /*xxl*/testButton(21), + ]), + Row(children: [ + /*xxxl*/testButton(23), + /*ax1*/ testButton(28), + ]), + Row(children: [ + /*ax2*/ testButton(33), + /*ax3*/ testButton(40), + ]), + Row(children: [ + /*ax4*/ testButton(47), + /*ax5*/ testButton(53), + ]), + ], + ), + ), + ), + ), + ); + + final Iterable buttons = tester.widgetList( + find.text('Button', findRichText: true)); + final Iterable sizes = buttons.map((RichText text) { + return text.textScaler.scale(text.text.style!.fontSize!); + }); + expect(sizes, + [21, 21, 21, 21, 23, 24, 24, 28, 33, 40, 47, 53].map( + (double size) => moreOrLessEquals(size, epsilon: 0.001)), + ); + + await expectLater( + find.byType(Column), + matchesGoldenFile('cupertinoActionSheet.textScaling.png'), + ); + }); + testWidgets('Verify that a tap on modal barrier dismisses an action sheet', (WidgetTester tester) async { await tester.pumpWidget( createAppWithButtonThatLaunchesActionSheet( @@ -417,11 +509,11 @@ void main() { expect(tester.getCenter(find.widgetWithText(CupertinoActionSheetAction, 'Five')).dx, equals(400.0)); // Check that the action buttons are the correct heights. - expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'One')).height, equals(83.0)); - expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Two')).height, equals(83.0)); - expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Three')).height, equals(83.0)); - expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Four')).height, equals(83.0)); - expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Five')).height, equals(83.0)); + expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'One')).height, equals(95.4)); + expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Two')).height, equals(95.4)); + expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Three')).height, equals(95.4)); + expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Four')).height, equals(95.4)); + expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Five')).height, equals(95.4)); }); testWidgets('Content section is scrollable', (WidgetTester tester) async { @@ -1115,7 +1207,7 @@ void main() { await tester.tap(find.text('Go')); await tester.pump(); - expect(tester.getSize(find.byType(CupertinoActionSheet)).height, moreOrLessEquals(130.3)); + expect(tester.getSize(find.byType(CupertinoActionSheet)).height, moreOrLessEquals(130.64)); }); testWidgets('1 action button with cancel button', (WidgetTester tester) async { @@ -1142,7 +1234,7 @@ void main() { await tester.pump(); // Action section is size of one action button. - expect(findScrollableActionsSectionRenderBox(tester).size.height, 57.0); + expect(findScrollableActionsSectionRenderBox(tester).size.height, 57.17); }); testWidgets('2 action buttons with cancel button', (WidgetTester tester) async { @@ -1266,7 +1358,7 @@ void main() { await tester.tap(find.text('Go')); await tester.pump(); - expect(findScrollableActionsSectionRenderBox(tester).size.height, 57.0); + expect(findScrollableActionsSectionRenderBox(tester).size.height, 57.17); }); testWidgets('2+ action buttons without cancel button', (WidgetTester tester) async { @@ -1312,7 +1404,7 @@ void main() { // The action sheet consists of only a cancel button, so the height should // be cancel button height + padding. - const double expectedHeight = 57 // button height + const double expectedHeight = 57.17 // button height + 8 // bottom edge padding + 8; // top edge padding, since the screen has no top view padding expect(tester.getSize(find.byType(CupertinoActionSheet)).height, expectedHeight); @@ -1384,12 +1476,9 @@ void main() { await tester.pump(); await tester.pump(const Duration(seconds: 1)); - expect(tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'Cancel')).dy, 592.0); - expect( - tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'One')).dy, - moreOrLessEquals(469.7), - ); - expect(tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'Two')).dy, 527.0); + expect(tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'Cancel')).dy, moreOrLessEquals(592.0)); + expect(tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'One')).dy, moreOrLessEquals(469.36)); + expect(tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'Two')).dy, moreOrLessEquals(526.83)); }); // Verify that on a phone with the given `viewSize` and `viewPadding`, the the @@ -1417,8 +1506,7 @@ void main() { padding: viewPadding, ); }, - child: - createAppWithButtonThatLaunchesActionSheet( + child: createAppWithButtonThatLaunchesActionSheet( CupertinoActionSheet( actions: List.generate(20, (int i) => CupertinoActionSheetAction(