cpdf_thumbnail_page.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import 'dart:typed_data';
  2. import 'package:compdfkit_flutter/core/document/cpdf_document.dart';
  3. import 'package:flutter/material.dart';
  4. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  5. ///
  6. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  7. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  8. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  9. /// This notice may not be removed from this file.
  10. class CPDFThumbnailPage extends StatefulWidget {
  11. final isDark;
  12. final CPDFDocument document;
  13. final int currentPageIndex;
  14. const CPDFThumbnailPage(
  15. {Key? key,this.isDark = false, required this.document, required this.currentPageIndex})
  16. : super(key: key);
  17. @override
  18. State<CPDFThumbnailPage> createState() => _CPDFThumbnailPageState();
  19. }
  20. class _CPDFThumbnailPageState extends State<CPDFThumbnailPage> with AutomaticKeepAliveClientMixin{
  21. final ScrollController _scrollController = ScrollController();
  22. final GlobalKey globalKey = GlobalKey();
  23. @override
  24. void initState() {
  25. super.initState();
  26. }
  27. @override
  28. void dispose() {
  29. super.dispose();
  30. _scrollController.dispose();
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return FutureBuilder(
  35. initialData: 0,
  36. future: widget.document.getPageCount(),
  37. builder: (context, snapshot) {
  38. final size = MediaQuery.of(context).size;
  39. final widgetWidth = size.width - 16;
  40. return OrientationBuilder(builder: (context, orientation) {
  41. int crossAxisCount =
  42. orientation == Orientation.portrait ? 3 : 6;
  43. return GridView.builder(
  44. padding: const EdgeInsets.symmetric(horizontal: 8),
  45. key: globalKey,
  46. controller: _scrollController,
  47. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  48. crossAxisCount: crossAxisCount,
  49. mainAxisSpacing: 8,
  50. crossAxisSpacing: 8,
  51. childAspectRatio: 0.62),
  52. itemCount: snapshot.data ?? 0,
  53. itemBuilder: (context, index) => Padding(
  54. padding: const EdgeInsets.only(top: 8),
  55. child: _thumbnailItem(
  56. index, ((widgetWidth - 22) / crossAxisCount)),
  57. ));
  58. });
  59. });
  60. }
  61. Widget _thumbnailItem(int index, double itemWidth) {
  62. double aspectRatio = 0.30;
  63. double itemHeight = itemWidth + (itemWidth * aspectRatio);
  64. bool selected = widget.currentPageIndex == index;
  65. return FutureBuilder(
  66. future: widget.document.getPageSize(index),
  67. builder: (context, snap) {
  68. if (snap.connectionState == ConnectionState.done && snap.hasData) {
  69. var size = snap.data!;
  70. var imageWidth = itemWidth;
  71. var imageHeight = (imageWidth / size[0]) * size[1];
  72. if (imageHeight > itemHeight) {
  73. imageHeight = itemHeight;
  74. imageWidth = (imageHeight / size[1]) * size[0];
  75. }
  76. return Column(
  77. mainAxisAlignment: MainAxisAlignment.center,
  78. children: [
  79. InkWell(
  80. child: FutureBuilder(
  81. future: widget.document.renderPageAtIndex(
  82. index, imageWidth.toInt(), imageHeight.toInt()),
  83. builder: (context, snapshot) {
  84. if (snapshot.connectionState == ConnectionState.done &&
  85. !snapshot.hasError) {
  86. Uint8List? imageData = snapshot.data;
  87. if (imageData != null) {
  88. return _imageItem(
  89. Image.memory(
  90. imageData,
  91. ),
  92. selected,
  93. imageWidth,
  94. imageHeight);
  95. } else {
  96. return const Padding(padding: EdgeInsets.zero);
  97. }
  98. } else {
  99. return const Padding(padding: EdgeInsets.zero);
  100. }
  101. }),
  102. onTap: () {
  103. Navigator.pop(context, index);
  104. },
  105. ),
  106. _pageIndicator(index, selected)
  107. ],
  108. );
  109. } else {
  110. return const Padding(padding: EdgeInsets.zero);
  111. }
  112. });
  113. }
  114. Widget _imageItem(
  115. Widget child, bool selected, double imageWidth, double imageHeight) {
  116. return Container(
  117. width: imageWidth,
  118. height: imageHeight,
  119. decoration: BoxDecoration(
  120. border: Border.all(
  121. color: selected
  122. ? Theme.of(context).colorScheme.primaryContainer
  123. : const Color(0xFFDDE9FF),
  124. width: 2),
  125. borderRadius: const BorderRadius.all(Radius.circular(2))),
  126. child: child,
  127. );
  128. }
  129. Widget _pageIndicator(int index, bool selected) {
  130. return Container(
  131. margin: const EdgeInsets.only(top: 8),
  132. decoration: BoxDecoration(
  133. color: selected
  134. ? Theme.of(context).colorScheme.primaryContainer
  135. : Colors.transparent,
  136. borderRadius:
  137. const BorderRadius.all(Radius.circular(2))),
  138. child: Padding(
  139. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 2),
  140. child: Text(
  141. (index + 1).toString(),
  142. style: TextStyle(
  143. fontSize: 12,
  144. color: selected || widget.isDark ? Colors.white : Colors.black),
  145. ),
  146. ));
  147. }
  148. // Function to scroll to a specific item in the GridView
  149. void scrollToItem(int index) {
  150. }
  151. @override
  152. bool get wantKeepAlive => true;
  153. }