[CupertinoActionSheet] Fix padding and font size of buttons (#151199)
This PR updates the padding and font size of `CupertinoActionSheet`'s buttons to match native. The rules are derived from measuring on simulators: - The vertical padding is proportional to font size. - The horizontal padding is fixed. - The font size vary with text scaling in a way that doesn't exactly fit into any category within the HIG specification. This PR uses a segmented curve with interpolation to roughly match this non-linear curve. ### Comparison (Left to right: Native, Flutter after PR, Flutter before PR. The number at the bottom is the system text scale level, from 1 being extra small to 12 being accessibility large 5.) <img width="1019" alt="image" src="https://github.com/flutter/flutter/assets/1596656/65c2f222-1d6d-4485-b5f7-7d0a0c0ad56a"> <img width="1141" alt="image" src="https://github.com/flutter/flutter/assets/1596656/bc44e930-4cc8-4af4-944b-3056bb47de69"> <img width="1075" alt="image" src="https://github.com/flutter/flutter/assets/1596656/84659081-d518-494d-8b9c-a83fe23b8ffd">
This commit is contained in:
@@ -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<CupertinoActionSheet> {
|
||||
} else if (x >= x2) {
|
||||
return y2;
|
||||
} else {
|
||||
return Tween<double>(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<CupertinoActionSheetAction>
|
||||
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<CupertinoActionSheetAction>
|
||||
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<CupertinoActionSheetAction>
|
||||
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,
|
||||
|
||||
@@ -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: <Widget>[
|
||||
Row(children: <Widget>[
|
||||
/*xs*/ testButton(14),
|
||||
/*s*/ testButton(15),
|
||||
]),
|
||||
Row(children: <Widget>[
|
||||
/*m*/ testButton(16),
|
||||
/*l*/ testButton(17),
|
||||
]),
|
||||
Row(children: <Widget>[
|
||||
/*xl*/ testButton(19),
|
||||
/*xxl*/testButton(21),
|
||||
]),
|
||||
Row(children: <Widget>[
|
||||
/*xxxl*/testButton(23),
|
||||
/*ax1*/ testButton(28),
|
||||
]),
|
||||
Row(children: <Widget>[
|
||||
/*ax2*/ testButton(33),
|
||||
/*ax3*/ testButton(40),
|
||||
]),
|
||||
Row(children: <Widget>[
|
||||
/*ax4*/ testButton(47),
|
||||
/*ax5*/ testButton(53),
|
||||
]),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final Iterable<RichText> buttons = tester.widgetList<RichText>(
|
||||
find.text('Button', findRichText: true));
|
||||
final Iterable<double?> sizes = buttons.map((RichText text) {
|
||||
return text.textScaler.scale(text.text.style!.fontSize!);
|
||||
});
|
||||
expect(sizes,
|
||||
<double>[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<Widget>.generate(20, (int i) =>
|
||||
CupertinoActionSheetAction(
|
||||
|
||||
Reference in New Issue
Block a user