Unverified Commit cef737c4 authored by Polina Cherkasova's avatar Polina Cherkasova Committed by GitHub

_YearPicker should dispose ScrollController and MaterialSatesController. (#134393)

parent 7d0c9b72
...@@ -1147,7 +1147,8 @@ class YearPicker extends StatefulWidget { ...@@ -1147,7 +1147,8 @@ class YearPicker extends StatefulWidget {
} }
class _YearPickerState extends State<YearPicker> { class _YearPickerState extends State<YearPicker> {
late ScrollController _scrollController; ScrollController? _scrollController;
final MaterialStatesController _statesController = MaterialStatesController();
// The approximate number of years necessary to fill the available space. // The approximate number of years necessary to fill the available space.
static const int minYears = 18; static const int minYears = 18;
...@@ -1158,11 +1159,18 @@ class _YearPickerState extends State<YearPicker> { ...@@ -1158,11 +1159,18 @@ class _YearPickerState extends State<YearPicker> {
_scrollController = ScrollController(initialScrollOffset: _scrollOffsetForYear(widget.selectedDate ?? widget.firstDate)); _scrollController = ScrollController(initialScrollOffset: _scrollOffsetForYear(widget.selectedDate ?? widget.firstDate));
} }
@override
void dispose() {
_scrollController?.dispose();
_statesController.dispose();
super.dispose();
}
@override @override
void didUpdateWidget(YearPicker oldWidget) { void didUpdateWidget(YearPicker oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
if (widget.selectedDate != oldWidget.selectedDate && widget.selectedDate != null) { if (widget.selectedDate != oldWidget.selectedDate && widget.selectedDate != null) {
_scrollController.jumpTo(_scrollOffsetForYear(widget.selectedDate!)); _scrollController!.jumpTo(_scrollOffsetForYear(widget.selectedDate!));
} }
} }
...@@ -1255,10 +1263,11 @@ class _YearPickerState extends State<YearPicker> { ...@@ -1255,10 +1263,11 @@ class _YearPickerState extends State<YearPicker> {
assert(date.year == widget.lastDate.year); assert(date.year == widget.lastDate.year);
date = DateTime(year, widget.lastDate.month); date = DateTime(year, widget.lastDate.month);
} }
_statesController.value = states;
yearItem = InkWell( yearItem = InkWell(
key: ValueKey<int>(year), key: ValueKey<int>(year),
onTap: () => widget.onChanged(date), onTap: () => widget.onChanged(date),
statesController: MaterialStatesController(states), statesController: _statesController,
overlayColor: overlayColor, overlayColor: overlayColor,
child: yearItem, child: yearItem,
); );
......
...@@ -57,8 +57,6 @@ typedef ScrollControllerCallback = void Function(ScrollPosition position); ...@@ -57,8 +57,6 @@ typedef ScrollControllerCallback = void Function(ScrollPosition position);
/// listen to scrolling occur without using a [ScrollController]. /// listen to scrolling occur without using a [ScrollController].
class ScrollController extends ChangeNotifier { class ScrollController extends ChangeNotifier {
/// Creates a controller for a scrollable widget. /// Creates a controller for a scrollable widget.
///
/// The values of `initialScrollOffset` and `keepScrollOffset` must not be null.
ScrollController({ ScrollController({
double initialScrollOffset = 0.0, double initialScrollOffset = 0.0,
this.keepScrollOffset = true, this.keepScrollOffset = true,
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
import 'feedback_tester.dart'; import 'feedback_tester.dart';
...@@ -211,7 +212,7 @@ void main() { ...@@ -211,7 +212,7 @@ void main() {
expect(selectedDate, equals(DateTime(2018, DateTime.january, 4))); expect(selectedDate, equals(DateTime(2018, DateTime.january, 4)));
}); });
testWidgets('Changing year for february 29th', (WidgetTester tester) async { testWidgetsWithLeakTracking('Changing year for february 29th', (WidgetTester tester) async {
DateTime? selectedDate; DateTime? selectedDate;
await tester.pumpWidget(calendarDatePicker( await tester.pumpWidget(calendarDatePicker(
initialDate: DateTime(2020, DateTime.february, 29), initialDate: DateTime(2020, DateTime.february, 29),
...@@ -230,7 +231,7 @@ void main() { ...@@ -230,7 +231,7 @@ void main() {
expect(selectedDate, equals(DateTime(2020, DateTime.february, 28))); expect(selectedDate, equals(DateTime(2020, DateTime.february, 28)));
}); });
testWidgets('Changing year does not change the month', (WidgetTester tester) async { testWidgetsWithLeakTracking('Changing year does not change the month', (WidgetTester tester) async {
DateTime? displayedMonth; DateTime? displayedMonth;
await tester.pumpWidget(calendarDatePicker( await tester.pumpWidget(calendarDatePicker(
initialDate: DateTime(2016, DateTime.january, 15), initialDate: DateTime(2016, DateTime.january, 15),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment