Skip to content

Commit

Permalink
feat: add animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Anxcye committed Aug 10, 2024
1 parent 4c35c20 commit 5bf5fac
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 85 deletions.
31 changes: 20 additions & 11 deletions lib/page/book_detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:image_picker/image_picker.dart';

class BookDetail extends StatefulWidget {
const BookDetail({super.key, required this.book, required this.onRefresh});
const BookDetail({super.key, required this.book, this.onRefresh});

final Book book;
final Function onRefresh;
final Function? onRefresh;

@override
State<BookDetail> createState() => _BookDetailState();
Expand Down Expand Up @@ -220,7 +220,9 @@ class _BookDetailState extends State<BookDetail> {
setState(() {
coverImage = Image.file(File(image.path));
updateBook(widget.book);
widget.onRefresh();
if (widget.onRefresh != null) {
widget.onRefresh!();
}
});
}
},
Expand All @@ -237,13 +239,18 @@ class _BookDetailState extends State<BookDetail> {
),
],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image(
image: coverImage.image,
fit: BoxFit.cover,
width: 160,
height: 230,
child: SizedBox(
width: 160,
height: 230,
child: Hero(
tag: widget.book.coverFullPath,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image(
image: coverImage.image,
fit: BoxFit.cover,
),
),
),
),
),
Expand Down Expand Up @@ -338,7 +345,9 @@ class _BookDetailState extends State<BookDetail> {
setState(() {
isEditing = false;
updateBook(widget.book);
widget.onRefresh();
if (widget.onRefresh != null) {
widget.onRefresh!();
}
});
},
)
Expand Down
30 changes: 18 additions & 12 deletions lib/page/book_notes_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,21 @@ Widget bookInfo(BuildContext context, Book book, int numberOfNotes) {
);
}

ClipRRect bookCover(Book book) {
return ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.file(
File(
book.coverFullPath,
Widget bookCover(Book book) {
return SizedBox(
height: 180,
width: 120,
child: Hero(
tag: book.coverFullPath,
child: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.file(
File(
book.coverFullPath,
),
fit: BoxFit.cover,
),
),
height: 180,
width: 120,
fit: BoxFit.cover,
),
);
}
Expand Down Expand Up @@ -164,7 +169,8 @@ Row operators(BuildContext context, Book book) {
}

return Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
operateButton(context, const Icon(Icons.details), L10n.of(context).notes_page_detail,
operateButton(
context, const Icon(Icons.details), L10n.of(context).notes_page_detail,
() {
Navigator.push(
context,
Expand All @@ -174,8 +180,8 @@ Row operators(BuildContext context, Book book) {
);
}),
// operateButton(context, Icons.search, 'Search', () {}),
operateButton(context, const Icon(Icons.ios_share), L10n.of(context).notes_page_export,
() {
operateButton(context, const Icon(Icons.ios_share),
L10n.of(context).notes_page_export, () {
handleExportNotes();
}),
// operateButton(context, Icons.ios_share, 'Export', () {}),
Expand Down
27 changes: 26 additions & 1 deletion lib/page/book_player/epub_player.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:io';

import 'package:anx_reader/config/shared_preference_provider.dart';
import 'package:anx_reader/dao/book.dart';
Expand Down Expand Up @@ -36,7 +37,7 @@ class EpubPlayer extends StatefulWidget {
State<EpubPlayer> createState() => EpubPlayerState();
}

class EpubPlayerState extends State<EpubPlayer> {
class EpubPlayerState extends State<EpubPlayer> with TickerProviderStateMixin {
late InAppWebViewController webViewController;
late ContextMenu contextMenu;
String cfi = '';
Expand All @@ -47,6 +48,8 @@ class EpubPlayerState extends State<EpubPlayer> {
int chapterTotalPages = 0;
List<TocItem> toc = [];
OverlayEntry? contextMenuEntry;
late AnimationController _animationController;
late Animation<double> _animation;

void prevPage() {
webViewController.evaluateJavascript(source: 'prevPage()');
Expand Down Expand Up @@ -254,6 +257,16 @@ class EpubPlayerState extends State<EpubPlayer> {
},
onHideContextMenu: () {},
);
_animationController = AnimationController(
duration: const Duration(milliseconds: 600),
vsync: this,
);
_animation =
Tween<double>(begin: 1.0, end: 0.0).animate(_animationController);

WidgetsBinding.instance.addPostFrameCallback((_) {
_animationController.forward();
});
super.initState();
}

Expand All @@ -265,6 +278,7 @@ class EpubPlayerState extends State<EpubPlayer> {
@override
void dispose() {
super.dispose();
_animationController.dispose();
InAppWebViewController.clearAllCache();
Book book = widget.book;
book.lastReadPosition = cfi;
Expand All @@ -280,6 +294,9 @@ class EpubPlayerState extends State<EpubPlayer> {
);

Widget readingInfoWidget() {
if (chapterCurrentPage == 0) {
return const SizedBox();
}
TextStyle textStyle = TextStyle(
color:
Color(int.parse('0x${Prefs().readTheme.textColor}')).withAlpha(150),
Expand Down Expand Up @@ -356,6 +373,14 @@ class EpubPlayerState extends State<EpubPlayer> {
return Scaffold(
body: Stack(
children: [
SizedBox.expand(
child: FadeTransition(
opacity: _animation,
child: Image(
fit: BoxFit.cover,
image: FileImage(File(widget.book.coverFullPath))),
),
),
InAppWebView(
initialUrlRequest: URLRequest(url: WebUri(indexHtmlPath)),
onLoadStart: (controller, url) => onLoadStart(controller),
Expand Down
21 changes: 13 additions & 8 deletions lib/page/home_page/notes_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,20 @@ class _NotesPageState extends State<NotesPage> {
),
),
// Expanded(child: SizedBox()),
ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.file(
File(
snapshot.data!.coverFullPath,
SizedBox(
height: 130,
width: 90,
child: Hero(
tag: snapshot.data!.coverFullPath,
child: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.file(
File(
snapshot.data!.coverFullPath,
),
fit: BoxFit.cover,
),
),
height: 130,
width: 90,
fit: BoxFit.cover,
),
),
],
Expand Down
52 changes: 33 additions & 19 deletions lib/page/home_page/statistics_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:anx_reader/dao/book.dart';
import 'package:anx_reader/dao/reading_time.dart';
import 'package:anx_reader/l10n/generated/L10n.dart';
import 'package:anx_reader/models/book.dart';
import 'package:anx_reader/page/book_detail.dart';
import 'package:anx_reader/utils/convert_seconds.dart';
import 'package:anx_reader/widgets/highlight_digit.dart';
import 'package:anx_reader/widgets/statistic/chard_card.dart';
Expand Down Expand Up @@ -124,20 +125,16 @@ class _StatisticPageState extends State<StatisticPage> {
Expanded(
// child: _buildStatisticCard('{} ${L10n.of(context).statistic_notes}',
// selectTotalNumberOfNotes())),
child: highlightDigit(context, L10n.of(context).statistic_notes(totalNumberOfNotes),
smallTextStyle(), bigTextStyle())),
child: highlightDigit(
context,
L10n.of(context).statistic_notes(totalNumberOfNotes),
smallTextStyle(),
bigTextStyle())),
],
);
}
}

TextStyle totalReadTimeTextStyle() {
return const TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
);
}

TextStyle bigTextStyle() {
return const TextStyle(
fontSize: 24,
Expand All @@ -152,6 +149,13 @@ TextStyle smallTextStyle() {
}

Widget _totalReadTime() {
TextStyle totalReadTimeTextStyle() {
return const TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
);
}

return FutureBuilder<int>(
future: selectTotalReadingTime(),
builder: (context, snapshot) {
Expand Down Expand Up @@ -273,22 +277,32 @@ class BookStatisticItem extends StatelessWidget {
future: selectBookById(bookId),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return SizedBox(
// height: 150,
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BookDetail(book: snapshot.data!)));
},
child: Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.file(
File(
snapshot.data!.coverFullPath,
SizedBox(
height: 130,
width: 90,
child: Hero(
tag: snapshot.data!.coverFullPath,
child: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.file(
File(
snapshot.data!.coverFullPath,
),
fit: BoxFit.cover,
),
),
height: 130,
width: 90,
fit: BoxFit.cover,
),
),
const SizedBox(width: 15),
Expand Down
22 changes: 13 additions & 9 deletions lib/page/reading_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:ffi';
import 'dart:io';

import 'package:anx_reader/config/shared_preference_provider.dart';
import 'package:anx_reader/dao/reading_time.dart';
Expand Down Expand Up @@ -206,15 +207,18 @@ class ReadingPageState extends State<ReadingPage> with WidgetsBindingObserver {

@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
EpubPlayer(
key: epubPlayerKey,
book: _book,
showOrHideAppBarAndBottomBar: showOrHideAppBarAndBottomBar,
),
],
return Hero(
tag: _book.coverFullPath,
child: Scaffold(
body: Stack(
children: [
EpubPlayer(
key: epubPlayerKey,
book: _book,
showOrHideAppBarAndBottomBar: showOrHideAppBarAndBottomBar,
),
],
),
),
);
}
Expand Down
15 changes: 9 additions & 6 deletions lib/service/book.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ void openBook(BuildContext context, Book book, Function updateBookList) {
});

Navigator.push(
context,
CupertinoPageRoute(
builder: (context) => ReadingPage(key: readingPageKey, book: book),
),
).then((value) {
context,
PageRouteBuilder(
transitionDuration: const Duration(milliseconds: 1000),
reverseTransitionDuration: const Duration(milliseconds: 1000),
pageBuilder: (context, animation, secondaryAnimation) =>
ReadingPage(key: readingPageKey, book: book),
)).then((value) {
// wait 1s to update book which is read
Future.delayed(const Duration(seconds: 1), () {
updateBookList();
Expand All @@ -99,7 +101,8 @@ Future<void> resetBookCover(Book book) async {
EpubBookRef epubBookRef = await EpubReader.openBook(file.readAsBytesSync());

final cover = await epubBookRef.readCover();
final relativeCoverPath = 'cover/${book.title}-${DateTime.now().millisecond.toString()}.png';
final relativeCoverPath =
'cover/${book.title}-${DateTime.now().millisecond.toString()}.png';
final coverPath = getBasePath(relativeCoverPath);

saveImageToLocal(cover, coverPath);
Expand Down
Loading

0 comments on commit 5bf5fac

Please sign in to comment.