Skip to content

Commit

Permalink
Cupertino date picker should dispose scroll controllers it created. …
Browse files Browse the repository at this point in the history
…(#135355)
  • Loading branch information
derdilla authored Nov 2, 2023
1 parent 24adfe1 commit 7646430
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 64 deletions.
27 changes: 20 additions & 7 deletions packages/flutter/lib/src/cupertino/date_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,10 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
late double totalWidth;
late double pickerColumnWidth;

FixedExtentScrollController? _hourScrollController;
FixedExtentScrollController? _minuteScrollController;
FixedExtentScrollController? _secondScrollController;

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -2019,6 +2023,10 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
void dispose() {
PaintingBinding.instance.systemFonts.removeListener(_handleSystemFontsChange);
textPainter.dispose();

_hourScrollController?.dispose();
_minuteScrollController?.dispose();
_secondScrollController?.dispose();
super.dispose();
}

Expand Down Expand Up @@ -2164,8 +2172,11 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
}

Widget _buildHourPicker(EdgeInsetsDirectional additionalPadding, Widget selectionOverlay) {
_hourScrollController ??= FixedExtentScrollController(
initialItem: selectedHour!
);
return CupertinoPicker(
scrollController: FixedExtentScrollController(initialItem: selectedHour!),
scrollController: _hourScrollController,
magnification: _kMagnification,
offAxisFraction: _calculateOffAxisFraction(additionalPadding.start, 0),
itemExtent: widget.itemExtent,
Expand Down Expand Up @@ -2223,10 +2234,11 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
}

Widget _buildMinutePicker(EdgeInsetsDirectional additionalPadding, Widget selectionOverlay) {
_minuteScrollController ??= FixedExtentScrollController(
initialItem: selectedMinute ~/ widget.minuteInterval,
);
return CupertinoPicker(
scrollController: FixedExtentScrollController(
initialItem: selectedMinute ~/ widget.minuteInterval,
),
scrollController: _minuteScrollController,
magnification: _kMagnification,
offAxisFraction: _calculateOffAxisFraction(
additionalPadding.start,
Expand Down Expand Up @@ -2289,10 +2301,11 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
}

Widget _buildSecondPicker(EdgeInsetsDirectional additionalPadding, Widget selectionOverlay) {
_secondScrollController ??= FixedExtentScrollController(
initialItem: selectedSecond! ~/ widget.secondInterval,
);
return CupertinoPicker(
scrollController: FixedExtentScrollController(
initialItem: selectedSecond! ~/ widget.secondInterval,
),
scrollController: _secondScrollController,
magnification: _kMagnification,
offAxisFraction: _calculateOffAxisFraction(
additionalPadding.start,
Expand Down
1 change: 1 addition & 0 deletions packages/flutter/lib/src/cupertino/picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ class _CupertinoPickerState extends State<CupertinoPicker> {
void didUpdateWidget(CupertinoPicker oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.scrollController != null && oldWidget.scrollController == null) {
_controller?.dispose();
_controller = null;
} else if (widget.scrollController == null && oldWidget.scrollController != null) {
assert(_controller == null);
Expand Down
Loading

0 comments on commit 7646430

Please sign in to comment.