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 {
}
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.
static const int minYears = 18;
......@@ -1158,11 +1159,18 @@ class _YearPickerState extends State<YearPicker> {
_scrollController = ScrollController(initialScrollOffset: _scrollOffsetForYear(widget.selectedDate ?? widget.firstDate));
}
@override
void dispose() {
_scrollController?.dispose();
_statesController.dispose();
super.dispose();
}
@override
void didUpdateWidget(YearPicker oldWidget) {
super.didUpdateWidget(oldWidget);
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> {
assert(date.year == widget.lastDate.year);
date = DateTime(year, widget.lastDate.month);
}
_statesController.value = states;
yearItem = InkWell(
key: ValueKey<int>(year),
onTap: () => widget.onChanged(date),
statesController: MaterialStatesController(states),
statesController: _statesController,
overlayColor: overlayColor,
child: yearItem,
);
......
......@@ -57,8 +57,6 @@ typedef ScrollControllerCallback = void Function(ScrollPosition position);
/// listen to scrolling occur without using a [ScrollController].
class ScrollController extends ChangeNotifier {
/// Creates a controller for a scrollable widget.
///
/// The values of `initialScrollOffset` and `keepScrollOffset` must not be null.
ScrollController({
double initialScrollOffset = 0.0,
this.keepScrollOffset = true,
......
......@@ -5,6 +5,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
import 'feedback_tester.dart';
......@@ -211,7 +212,7 @@ void main() {
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;
await tester.pumpWidget(calendarDatePicker(
initialDate: DateTime(2020, DateTime.february, 29),
......@@ -230,7 +231,7 @@ void main() {
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;
await tester.pumpWidget(calendarDatePicker(
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