Skip to content

Commit 0896292

Browse files
authored
Refactor bottom sheet & related widgets (#159257)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> This PR refactors Material's bottom sheet & related widgets. It also replaces internal usage of deprecated types such as `MaterialState` with `WidgetState`. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 8563215 commit 0896292

File tree

1 file changed

+29
-62
lines changed

1 file changed

+29
-62
lines changed

packages/flutter/lib/src/material/bottom_sheet.dart

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@ import 'bottom_sheet_theme.dart';
1515
import 'color_scheme.dart';
1616
import 'colors.dart';
1717
import 'constants.dart';
18-
import 'curves.dart';
1918
import 'debug.dart';
2019
import 'material.dart';
2120
import 'material_localizations.dart';
22-
import 'material_state.dart';
21+
import 'motion.dart';
2322
import 'scaffold.dart';
2423
import 'theme.dart';
2524

2625
const Duration _bottomSheetEnterDuration = Duration(milliseconds: 250);
2726
const Duration _bottomSheetExitDuration = Duration(milliseconds: 200);
28-
const Curve _modalBottomSheetCurve = decelerateEasing;
27+
const Curve _modalBottomSheetCurve = Easing.legacyDecelerate;
2928
const double _minFlingVelocity = 700.0;
3029
const double _closeProgressThreshold = 0.5;
3130
const double _defaultScrollControlDisabledMaxHeightRatio = 9.0 / 16.0;
@@ -266,11 +265,11 @@ class _BottomSheetState extends State<BottomSheet> {
266265

267266
bool get _dismissUnderway => widget.animationController!.status == AnimationStatus.reverse;
268267

269-
Set<MaterialState> dragHandleMaterialState = <MaterialState>{};
268+
Set<WidgetState> dragHandleStates = <WidgetState>{};
270269

271270
void _handleDragStart(DragStartDetails details) {
272271
setState(() {
273-
dragHandleMaterialState.add(MaterialState.dragged);
272+
dragHandleStates.add(WidgetState.dragged);
274273
});
275274
widget.onDragStart?.call(details);
276275
}
@@ -297,7 +296,7 @@ class _BottomSheetState extends State<BottomSheet> {
297296
return;
298297
}
299298
setState(() {
300-
dragHandleMaterialState.remove(MaterialState.dragged);
299+
dragHandleStates.remove(WidgetState.dragged);
301300
});
302301
bool isClosing = false;
303302
if (details.velocity.pixelsPerSecond.dy > _minFlingVelocity) {
@@ -335,12 +334,12 @@ class _BottomSheetState extends State<BottomSheet> {
335334
}
336335

337336
void _handleDragHandleHover(bool hovering) {
338-
if (hovering != dragHandleMaterialState.contains(MaterialState.hovered)) {
337+
if (hovering != dragHandleStates.contains(WidgetState.hovered)) {
339338
setState(() {
340339
if (hovering){
341-
dragHandleMaterialState.add(MaterialState.hovered);
340+
dragHandleStates.add(WidgetState.hovered);
342341
} else {
343-
dragHandleMaterialState.remove(MaterialState.hovered);
342+
dragHandleStates.remove(WidgetState.hovered);
344343
}
345344
});
346345
}
@@ -361,11 +360,11 @@ class _BottomSheetState extends State<BottomSheet> {
361360
final bool showDragHandle = widget.showDragHandle ?? (widget.enableDrag && (bottomSheetTheme.showDragHandle ?? false));
362361

363362
Widget? dragHandle;
364-
if (showDragHandle){
363+
if (showDragHandle) {
365364
dragHandle = _DragHandle(
366365
onSemanticsTap: widget.onClosing,
367366
handleHover: _handleDragHandleHover,
368-
materialState: dragHandleMaterialState,
367+
states: dragHandleStates,
369368
dragHandleColor: widget.dragHandleColor,
370369
dragHandleSize: widget.dragHandleSize,
371370
);
@@ -431,20 +430,18 @@ class _BottomSheetState extends State<BottomSheet> {
431430

432431
// See scaffold.dart
433432

434-
typedef _SizeChangeCallback<Size> = void Function(Size size);
435-
436433
class _DragHandle extends StatelessWidget {
437434
const _DragHandle({
438435
required this.onSemanticsTap,
439436
required this.handleHover,
440-
required this.materialState,
437+
required this.states,
441438
this.dragHandleColor,
442439
this.dragHandleSize,
443440
});
444441

445442
final VoidCallback? onSemanticsTap;
446443
final ValueChanged<bool> handleHover;
447-
final Set<MaterialState> materialState;
444+
final Set<WidgetState> states;
448445
final Color? dragHandleColor;
449446
final Size? dragHandleSize;
450447

@@ -470,8 +467,8 @@ class _DragHandle extends StatelessWidget {
470467
width: handleSize.width,
471468
decoration: BoxDecoration(
472469
borderRadius: BorderRadius.circular(handleSize.height/2),
473-
color: MaterialStateProperty.resolveAs<Color?>(dragHandleColor, materialState)
474-
?? MaterialStateProperty.resolveAs<Color?>(bottomSheetTheme.dragHandleColor, materialState)
470+
color: WidgetStateProperty.resolveAs<Color?>(dragHandleColor, states)
471+
?? WidgetStateProperty.resolveAs<Color?>(bottomSheetTheme.dragHandleColor, states)
475472
?? m3Defaults.dragHandleColor,
476473
),
477474
),
@@ -491,7 +488,7 @@ class _BottomSheetLayoutWithSizeListener extends SingleChildRenderObjectWidget {
491488
super.child,
492489
});
493490

494-
final _SizeChangeCallback<Size> onChildSizeChanged;
491+
final ValueChanged<Size> onChildSizeChanged;
495492
final double animationValue;
496493
final bool isScrollControlled;
497494
final double scrollControlDisabledMaxHeightRatio;
@@ -518,7 +515,7 @@ class _BottomSheetLayoutWithSizeListener extends SingleChildRenderObjectWidget {
518515
class _RenderBottomSheetLayoutWithSizeListener extends RenderShiftedBox {
519516
_RenderBottomSheetLayoutWithSizeListener({
520517
RenderBox? child,
521-
required _SizeChangeCallback<Size> onChildSizeChanged,
518+
required ValueChanged<Size> onChildSizeChanged,
522519
required double animationValue,
523520
required bool isScrollControlled,
524521
required double scrollControlDisabledMaxHeightRatio,
@@ -530,9 +527,9 @@ class _RenderBottomSheetLayoutWithSizeListener extends RenderShiftedBox {
530527

531528
Size _lastSize = Size.zero;
532529

533-
_SizeChangeCallback<Size> get onChildSizeChanged => _onChildSizeChanged;
534-
_SizeChangeCallback<Size> _onChildSizeChanged;
535-
set onChildSizeChanged(_SizeChangeCallback<Size> newCallback) {
530+
ValueChanged<Size> get onChildSizeChanged => _onChildSizeChanged;
531+
ValueChanged<Size> _onChildSizeChanged;
532+
set onChildSizeChanged(ValueChanged<Size> newCallback) {
536533
if (_onChildSizeChanged == newCallback) {
537534
return;
538535
}
@@ -574,50 +571,20 @@ class _RenderBottomSheetLayoutWithSizeListener extends RenderShiftedBox {
574571
markNeedsLayout();
575572
}
576573

577-
Size _getSize(BoxConstraints constraints) {
578-
return constraints.constrain(constraints.biggest);
579-
}
580-
581574
@override
582-
double computeMinIntrinsicWidth(double height) {
583-
final double width = _getSize(BoxConstraints.tightForFinite(height: height)).width;
584-
if (width.isFinite) {
585-
return width;
586-
}
587-
return 0.0;
588-
}
575+
double computeMinIntrinsicWidth(double height) => 0.0;
589576

590577
@override
591-
double computeMaxIntrinsicWidth(double height) {
592-
final double width = _getSize(BoxConstraints.tightForFinite(height: height)).width;
593-
if (width.isFinite) {
594-
return width;
595-
}
596-
return 0.0;
597-
}
578+
double computeMaxIntrinsicWidth(double height) => 0.0;
598579

599580
@override
600-
double computeMinIntrinsicHeight(double width) {
601-
final double height = _getSize(BoxConstraints.tightForFinite(width: width)).height;
602-
if (height.isFinite) {
603-
return height;
604-
}
605-
return 0.0;
606-
}
581+
double computeMinIntrinsicHeight(double width) => 0.0;
607582

608583
@override
609-
double computeMaxIntrinsicHeight(double width) {
610-
final double height = _getSize(BoxConstraints.tightForFinite(width: width)).height;
611-
if (height.isFinite) {
612-
return height;
613-
}
614-
return 0.0;
615-
}
584+
double computeMaxIntrinsicHeight(double width) => 0.0;
616585

617586
@override
618-
Size computeDryLayout(BoxConstraints constraints) {
619-
return _getSize(constraints);
620-
}
587+
Size computeDryLayout(BoxConstraints constraints) => constraints.biggest;
621588

622589
@override
623590
double? computeDryBaseline(covariant BoxConstraints constraints, TextBaseline baseline) {
@@ -631,7 +598,7 @@ class _RenderBottomSheetLayoutWithSizeListener extends RenderShiftedBox {
631598
return null;
632599
}
633600
final Size childSize = childConstraints.isTight ? childConstraints.smallest : child.getDryLayout(childConstraints);
634-
return result + _getPositionForChild(_getSize(constraints), childSize).dy;
601+
return result + _getPositionForChild(constraints.biggest, childSize).dy;
635602
}
636603

637604
BoxConstraints _getConstraintsForChild(BoxConstraints constraints) {
@@ -650,7 +617,7 @@ class _RenderBottomSheetLayoutWithSizeListener extends RenderShiftedBox {
650617

651618
@override
652619
void performLayout() {
653-
size = _getSize(constraints);
620+
size = constraints.biggest;
654621
final RenderBox? child = this.child;
655622
if (child == null) {
656623
return;
@@ -1137,11 +1104,11 @@ class ModalBottomSheetRoute<T> extends PopupRoute<T> {
11371104

11381105
@override
11391106
Widget buildModalBarrier() {
1140-
if (barrierColor.alpha != 0 && !offstage) { // changedInternalState is called if barrierColor or offstage updates
1141-
assert(barrierColor != barrierColor.withOpacity(0.0));
1107+
if (barrierColor.a != 0 && !offstage) { // changedInternalState is called if barrierColor or offstage updates
1108+
assert(barrierColor != barrierColor.withValues(alpha: 0.0));
11421109
final Animation<Color?> color = animation!.drive(
11431110
ColorTween(
1144-
begin: barrierColor.withOpacity(0.0),
1111+
begin: barrierColor.withValues(alpha: 0.0),
11451112
end: barrierColor, // changedInternalState is called if barrierColor updates
11461113
).chain(CurveTween(curve: barrierCurve)), // changedInternalState is called if barrierCurve updates
11471114
);

0 commit comments

Comments
 (0)