From 8a79ab3cb7772c8b3a3a67fc8856086d4a1d12d0 Mon Sep 17 00:00:00 2001 From: Yassin Date: Thu, 19 Dec 2024 08:57:24 +0200 Subject: [PATCH] test: working on passing the controller from the widget screen --- .../quran/reading/quran_reading_screen.dart | 62 +++++++++++++++---- .../auto_reading/auto_reading_notifier.dart | 8 ++- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/lib/src/pages/quran/reading/quran_reading_screen.dart b/lib/src/pages/quran/reading/quran_reading_screen.dart index 8aca54f1a..5bb584733 100644 --- a/lib/src/pages/quran/reading/quran_reading_screen.dart +++ b/lib/src/pages/quran/reading/quran_reading_screen.dart @@ -64,6 +64,7 @@ class FocusNodes { required this.switchScreenViewFocusNode, required this.switchQuranModeNode, }); + void setupFocusTraversal({required bool isPortrait}) { if (isPortrait) { setupPortraitFocusTraversal(); @@ -255,29 +256,54 @@ class FocusNodes { } } -class AutoScrollViewStrategy implements QuranViewStrategy { +// New ConsumerStatefulWidget to manage ScrollController +class AutoScrollReadingView extends ConsumerStatefulWidget { final AutoScrollState autoScrollState; - final int preloadDistance = 3; // Adjust preload distance for performance - AutoScrollViewStrategy(this.autoScrollState); + AutoScrollReadingView({required this.autoScrollState}); @override - Widget buildView(QuranReadingState state, WidgetRef ref, BuildContext context) { - final scalingFactor = autoScrollState.fontSize; + _AutoScrollReadingViewState createState() => _AutoScrollReadingViewState(); +} + +class _AutoScrollReadingViewState extends ConsumerState { + late ScrollController scrollController; + @override + void initState() { + super.initState(); + scrollController = ScrollController(); + // Pass the ScrollController to the notifier + WidgetsBinding.instance.addPostFrameCallback((_) { + ref.read(autoScrollNotifierProvider.notifier).setScrollController(scrollController); + }); + } + + @override + void dispose() { + scrollController.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + final scalingFactor = widget.autoScrollState.fontSize; + final readingState = ref.watch(quranReadingNotifierProvider); + final total = readingState.whenOrNull(data: (data) => data.totalPages) ?? 0; + final pages = readingState.whenOrNull(data: (data) => data.svgs) ?? []; + final preloadDistance = 3; return Stack( children: [ ListView.builder( physics: NeverScrollableScrollPhysics(), - controller: autoScrollState.scrollController, - itemCount: state.totalPages, - cacheExtent: MediaQuery.of(context).size.height * preloadDistance, // Reduce cache extent + controller: scrollController, + itemCount: total, + cacheExtent: MediaQuery.of(context).size.height * preloadDistance, itemBuilder: (context, index) { - print('Index: $index'); return GestureDetector( onTap: () { final autoScrollNotifier = ref.read(autoScrollNotifierProvider.notifier); - if (autoScrollState.isPlaying) { + if (widget.autoScrollState.isPlaying) { autoScrollNotifier.pauseAutoScroll(); } else { autoScrollNotifier.resumeAutoScroll(); @@ -287,13 +313,13 @@ class AutoScrollViewStrategy implements QuranViewStrategy { width: MediaQuery.of(context).size.width * scalingFactor, height: MediaQuery.of(context).size.height * scalingFactor, child: SvgPictureWidget( - svgPicture: state.svgs[index], + svgPicture: pages[index], ), ), ); }, ), - if (autoScrollState.isLoading) // Show loading screen only when isLoading is true + if (widget.autoScrollState.isLoading) Container( color: Colors.black.withOpacity(0.9), child: Center( @@ -306,6 +332,18 @@ class AutoScrollViewStrategy implements QuranViewStrategy { ], ); } +} + +// Update AutoScrollViewStrategy to use AutoScrollReadingView +class AutoScrollViewStrategy implements QuranViewStrategy { + final AutoScrollState autoScrollState; + + AutoScrollViewStrategy(this.autoScrollState); + + @override + Widget buildView(QuranReadingState state, WidgetRef ref, BuildContext context) { + return AutoScrollReadingView(autoScrollState: autoScrollState); + } @override List buildControls( diff --git a/lib/src/state_management/quran/reading/auto_reading/auto_reading_notifier.dart b/lib/src/state_management/quran/reading/auto_reading/auto_reading_notifier.dart index 7c245364d..b73cbe351 100644 --- a/lib/src/state_management/quran/reading/auto_reading/auto_reading_notifier.dart +++ b/lib/src/state_management/quran/reading/auto_reading/auto_reading_notifier.dart @@ -10,7 +10,7 @@ import 'package:mawaqit/src/state_management/quran/reading/quran_reading_state.d class AutoScrollNotifier extends AutoDisposeNotifier { Timer? _autoScrollTimer; Timer? _hideTimer; - late final ScrollController scrollController; + late ScrollController scrollController; @override AutoScrollState build() { @@ -25,6 +25,12 @@ class AutoScrollNotifier extends AutoDisposeNotifier { ); } + void setScrollController(ScrollController controller) { + print('Setting scroll controller...'); + scrollController = controller; + } + + Future jumpToCurrentPage(int currentPage, double pageHeight) async { if (scrollController.hasClients) { final offset = (currentPage - 1) * pageHeight;