cpdf_thumbnail_page.dart 5.9 KB

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