Skip to content

Commit 7646430

Browse files
authored
Cupertino date picker should dispose scroll controllers it created. (flutter#135355)
1 parent 24adfe1 commit 7646430

File tree

3 files changed

+83
-64
lines changed

3 files changed

+83
-64
lines changed

packages/flutter/lib/src/cupertino/date_picker.dart

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,10 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
19901990
late double totalWidth;
19911991
late double pickerColumnWidth;
19921992

1993+
FixedExtentScrollController? _hourScrollController;
1994+
FixedExtentScrollController? _minuteScrollController;
1995+
FixedExtentScrollController? _secondScrollController;
1996+
19931997
@override
19941998
void initState() {
19951999
super.initState();
@@ -2019,6 +2023,10 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
20192023
void dispose() {
20202024
PaintingBinding.instance.systemFonts.removeListener(_handleSystemFontsChange);
20212025
textPainter.dispose();
2026+
2027+
_hourScrollController?.dispose();
2028+
_minuteScrollController?.dispose();
2029+
_secondScrollController?.dispose();
20222030
super.dispose();
20232031
}
20242032

@@ -2164,8 +2172,11 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
21642172
}
21652173

21662174
Widget _buildHourPicker(EdgeInsetsDirectional additionalPadding, Widget selectionOverlay) {
2175+
_hourScrollController ??= FixedExtentScrollController(
2176+
initialItem: selectedHour!
2177+
);
21672178
return CupertinoPicker(
2168-
scrollController: FixedExtentScrollController(initialItem: selectedHour!),
2179+
scrollController: _hourScrollController,
21692180
magnification: _kMagnification,
21702181
offAxisFraction: _calculateOffAxisFraction(additionalPadding.start, 0),
21712182
itemExtent: widget.itemExtent,
@@ -2223,10 +2234,11 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
22232234
}
22242235

22252236
Widget _buildMinutePicker(EdgeInsetsDirectional additionalPadding, Widget selectionOverlay) {
2237+
_minuteScrollController ??= FixedExtentScrollController(
2238+
initialItem: selectedMinute ~/ widget.minuteInterval,
2239+
);
22262240
return CupertinoPicker(
2227-
scrollController: FixedExtentScrollController(
2228-
initialItem: selectedMinute ~/ widget.minuteInterval,
2229-
),
2241+
scrollController: _minuteScrollController,
22302242
magnification: _kMagnification,
22312243
offAxisFraction: _calculateOffAxisFraction(
22322244
additionalPadding.start,
@@ -2289,10 +2301,11 @@ class _CupertinoTimerPickerState extends State<CupertinoTimerPicker> {
22892301
}
22902302

22912303
Widget _buildSecondPicker(EdgeInsetsDirectional additionalPadding, Widget selectionOverlay) {
2304+
_secondScrollController ??= FixedExtentScrollController(
2305+
initialItem: selectedSecond! ~/ widget.secondInterval,
2306+
);
22922307
return CupertinoPicker(
2293-
scrollController: FixedExtentScrollController(
2294-
initialItem: selectedSecond! ~/ widget.secondInterval,
2295-
),
2308+
scrollController: _secondScrollController,
22962309
magnification: _kMagnification,
22972310
offAxisFraction: _calculateOffAxisFraction(
22982311
additionalPadding.start,

packages/flutter/lib/src/cupertino/picker.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class _CupertinoPickerState extends State<CupertinoPicker> {
218218
void didUpdateWidget(CupertinoPicker oldWidget) {
219219
super.didUpdateWidget(oldWidget);
220220
if (widget.scrollController != null && oldWidget.scrollController == null) {
221+
_controller?.dispose();
221222
_controller = null;
222223
} else if (widget.scrollController == null && oldWidget.scrollController != null) {
223224
assert(_controller == null);

0 commit comments

Comments
 (0)