Skip to content

Commit

Permalink
fix: performance improve for TOC
Browse files Browse the repository at this point in the history
  • Loading branch information
Sivan22 committed Dec 7, 2024
1 parent a3871b6 commit 2066544
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 72 deletions.
139 changes: 69 additions & 70 deletions lib/screens/reading/text/toc_navigator_screen.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -41,82 +40,79 @@ class _TocViewerState extends State<TocViewer>
.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]);
},
),
],
),
),
);
Expand Down Expand Up @@ -154,13 +150,16 @@ class _TocViewerState extends State<TocViewer>
),
),
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),
),
),
],
);
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit 2066544

Please sign in to comment.