From 20665441f0673ca1cfe9a8d44db3b0bdfe3de711 Mon Sep 17 00:00:00 2001 From: Sivan Ratson <89018301+Sivan22@users.noreply.github.com> Date: Sat, 7 Dec 2024 23:37:58 +0200 Subject: [PATCH] fix: performance improve for TOC --- .../reading/text/toc_navigator_screen.dart | 139 +++++++++--------- pubspec.yaml | 4 +- 2 files changed, 71 insertions(+), 72 deletions(-) diff --git a/lib/screens/reading/text/toc_navigator_screen.dart b/lib/screens/reading/text/toc_navigator_screen.dart index 0fe779f9..a52a6430 100644 --- a/lib/screens/reading/text/toc_navigator_screen.dart +++ b/lib/screens/reading/text/toc_navigator_screen.dart @@ -1,5 +1,4 @@ import 'dart:io'; -import 'package:expandable/expandable.dart'; import 'package:flutter/material.dart'; import 'package:scrollable_positioned_list/scrollable_positioned_list.dart'; import 'package:otzaria/models/books.dart'; @@ -41,82 +40,79 @@ class _TocViewerState extends State .where((e) => e.text.contains(searchController.text)) .toList(); - return ListView.builder(itemBuilder: (context, index) { - return Padding( - padding: EdgeInsets.fromLTRB( - 0, 0, 10 * allEntries[index].level.toDouble(), 0), - child: ListTile( - title: Text(allEntries[index].text), - onTap: () { - widget.scrollController.scrollTo( - index: allEntries[index].index, - duration: const Duration(milliseconds: 250), - curve: Curves.ease, - ); - if (Platform.isAndroid) { - widget.closeLeftPaneCallback(); - } - }, - ), - ); - }); + return ListView.builder( + shrinkWrap: true, + itemCount: allEntries.length, + itemBuilder: (context, index) { + return Padding( + padding: EdgeInsets.fromLTRB( + 0, 0, 10 * allEntries[index].level.toDouble(), 0), + child: ListTile( + title: Text(allEntries[index].text), + onTap: () { + widget.scrollController.scrollTo( + index: allEntries[index].index, + duration: const Duration(milliseconds: 250), + curve: Curves.ease, + ); + if (Platform.isAndroid) { + widget.closeLeftPaneCallback(); + } + }, + ), + ); + }); } Widget _buildTocItem(TocEntry entry) { + void navigateToEntry() { + widget.scrollController.scrollTo( + index: entry.index, + duration: const Duration(milliseconds: 250), + curve: Curves.ease, + ); + if (Platform.isAndroid) { + widget.closeLeftPaneCallback(); + } + } + if (entry.children.isEmpty) { return Padding( padding: EdgeInsets.fromLTRB(0, 0, 10 * entry.level.toDouble(), 0), child: ListTile( title: Text(entry.text), - onTap: () { - widget.scrollController.scrollTo( - index: entry.index, - duration: const Duration(milliseconds: 250), - curve: Curves.ease, - ); - if (Platform.isAndroid) { - widget.closeLeftPaneCallback(); - } - }, + onTap: navigateToEntry, ), ); } else { return Padding( padding: EdgeInsets.fromLTRB(0, 0, 10 * entry.level.toDouble(), 0), - child: ExpandableNotifier( - child: ExpandablePanel( - controller: ExpandableController(initialExpanded: entry.level == 1), - key: PageStorageKey(entry), - collapsed: const SizedBox.shrink(), - header: ListTile( - contentPadding: const EdgeInsets.fromLTRB(0, 0, 0, 0), - title: Text(entry.text), - onTap: () { - widget.scrollController.scrollTo( - index: entry.index, - duration: const Duration(milliseconds: 250), - curve: Curves.ease, - ); - if (Platform.isAndroid) { - widget.closeLeftPaneCallback(); - } - }, - ), - expanded: ListView.builder(itemBuilder: (context, index) { - return _buildTocItem(entry.children[index]); - }), - theme: ExpandableThemeData( - tapBodyToCollapse: false, - tapBodyToExpand: false, - hasIcon: true, - iconPlacement: ExpandablePanelIconPlacement.left, - collapseIcon: Icons.keyboard_arrow_down_outlined, - expandIcon: Icons.chevron_right_rounded, - iconPadding: const EdgeInsets.fromLTRB(0, 12, 0, 0), - iconColor: Theme.of(context).colorScheme.primary, - tapHeaderToExpand: false, + child: Theme( + data: Theme.of(context).copyWith( + dividerColor: Colors.transparent, + ), + child: ExpansionTile( + initiallyExpanded: entry.level == 1, + title: GestureDetector( + onTap: navigateToEntry, + child: Text(entry.text), ), - // Recursively build children, + leading: const Icon(Icons.chevron_right_rounded), + trailing: const SizedBox.shrink(), + tilePadding: EdgeInsets.zero, + childrenPadding: EdgeInsets.zero, + iconColor: Theme.of(context).colorScheme.primary, + collapsedIconColor: Theme.of(context).colorScheme.primary, + children: [ + ListView.builder( + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), + itemCount: entry.children.length, + itemBuilder: (context, index) { + return _buildTocItem(entry.children[index]); + }, + ), + ], ), ), ); @@ -154,13 +150,16 @@ class _TocViewerState extends State ), ), Expanded( - child: searchController.text.isEmpty - ? ListView.builder( - shrinkWrap: true, - itemCount: snapshot.data!.length, - itemBuilder: (context, index) => - _buildTocItem(snapshot.data![index])) - : _buildFilteredList(snapshot.data!, context), + child: SingleChildScrollView( + child: searchController.text.isEmpty + ? ListView.builder( + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), + itemCount: snapshot.data!.length, + itemBuilder: (context, index) => + _buildTocItem(snapshot.data![index])) + : _buildFilteredList(snapshot.data!, context), + ), ), ], ); diff --git a/pubspec.yaml b/pubspec.yaml index 6f9b17c6..b78aff0e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,7 +8,7 @@ msix_config: display_name: אוצריא publisher_display_name: sivan22 identity_name: sivan22.Otzaria - msix_version: 0.2.3.2 + msix_version: 0.2.4.0 logo_path: assets/icon/icon.png publisher: CN=sivan22, O=sivan22, C=IL certificate_path: sivan22.pfx @@ -33,7 +33,7 @@ msix_config: # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 0.2.3+2 +version: 0.2.4-dev.0 environment: sdk: ">=3.2.6 <4.0.0"