Fix: Dispose controller during dispose of homeWidget

Signed-off-by: Neeraj Gupta <254676+ua741@users.noreply.github.com>
This commit is contained in:
Neeraj Gupta 2023-10-27 09:31:50 +05:30
parent c513957056
commit f855c1b5c6
3 changed files with 10 additions and 20 deletions

View file

@ -7,11 +7,6 @@ import 'package:flutter/widgets.dart' hide PageView;
///
/// Based on commit 3932ffb1cd5dfa0c3891c60977ee4f9cd70ade66 on channel dev
// Having this global (mutable) page controller is a bit of a hack. We need it
// to plumb in the factory for _PagePosition, but it will end up accumulating
// a large list of scroll positions. As long as you don't try to actually
// control the scroll positions, everything should be fine.
final PageController _defaultPageController = PageController();
const PageScrollPhysics _kPagePhysics = PageScrollPhysics();
/// A scrollable list that works page by page.
@ -50,15 +45,14 @@ class ExtentsPageView extends StatefulWidget {
Key? key,
this.scrollDirection = Axis.horizontal,
this.reverse = false,
PageController? controller,
required this.controller,
this.physics,
this.pageSnapping = true,
this.onPageChanged,
List<Widget> children = const <Widget>[],
this.dragStartBehavior = DragStartBehavior.start,
this.openDrawer,
}) : controller = controller ?? _defaultPageController,
childrenDelegate = SliverChildListDelegate(children),
}) : childrenDelegate = SliverChildListDelegate(children),
extents = children.length,
super(key: key);
@ -82,7 +76,7 @@ class ExtentsPageView extends StatefulWidget {
Key? key,
this.scrollDirection = Axis.horizontal,
this.reverse = false,
PageController? controller,
required this.controller,
this.physics,
this.pageSnapping = true,
this.onPageChanged,
@ -90,8 +84,7 @@ class ExtentsPageView extends StatefulWidget {
int? itemCount,
this.dragStartBehavior = DragStartBehavior.start,
this.openDrawer,
}) : controller = controller ?? _defaultPageController,
childrenDelegate =
}) : childrenDelegate =
SliverChildBuilderDelegate(itemBuilder, childCount: itemCount),
extents = 0,
super(key: key);
@ -101,7 +94,7 @@ class ExtentsPageView extends StatefulWidget {
this.extents = 1,
this.scrollDirection = Axis.horizontal,
this.reverse = false,
PageController? controller,
required this.controller,
this.physics,
this.pageSnapping = true,
this.onPageChanged,
@ -109,8 +102,7 @@ class ExtentsPageView extends StatefulWidget {
int? itemCount,
this.dragStartBehavior = DragStartBehavior.start,
this.openDrawer,
}) : controller = controller ?? _defaultPageController,
childrenDelegate = SliverChildBuilderDelegate(
}) : childrenDelegate = SliverChildBuilderDelegate(
itemBuilder,
childCount: itemCount,
addAutomaticKeepAlives: false,
@ -202,7 +194,7 @@ class ExtentsPageView extends StatefulWidget {
Key? key,
this.scrollDirection = Axis.horizontal,
this.reverse = false,
PageController? controller,
required this.controller,
this.physics,
this.pageSnapping = true,
this.onPageChanged,
@ -210,7 +202,6 @@ class ExtentsPageView extends StatefulWidget {
this.dragStartBehavior = DragStartBehavior.start,
this.openDrawer,
}) : extents = 0,
controller = controller ?? _defaultPageController,
super(key: key);
/// The number of pages to build off screen.
@ -297,7 +288,6 @@ class _PageViewState extends State<ExtentsPageView> {
@override
void dispose() {
widget.controller.dispose();
super.dispose();
}

View file

@ -60,7 +60,6 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
@override
Widget build(BuildContext context) {
_pageController ??= PageController(initialPage: _index);
final file = widget.memories[_index].file;
return Scaffold(
appBar: AppBar(
@ -292,7 +291,7 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
debugPrint(
"FullScreenbuildSwiper: $_index and total ${widget.memories.length}",
);
_pageController = PageController(initialPage: _index);
_pageController ??= PageController(initialPage: _index);
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTapDown: (TapDownDetails details) {
@ -343,7 +342,7 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {
);
},
itemCount: widget.memories.length,
controller: _pageController,
controller: _pageController!,
onPageChanged: (index) async {
unawaited(
MemoriesService.instance.markMemoryAsSeen(widget.memories[index]),

View file

@ -263,6 +263,7 @@ class _HomeWidgetState extends State<HomeWidget> {
_accountConfiguredEvent.cancel();
_intentDataStreamSubscription?.cancel();
_collectionUpdatedEvent.cancel();
_pageController.dispose();
super.dispose();
}