cpdf_page_slider_bar.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import 'package:compdfkit_flutter/core/cpdf_view_ctrl.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'dart:ui' as ui;
  5. import '../slider/cpdf_slider_theme.dart';
  6. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  7. ///
  8. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  9. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  10. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  11. /// This notice may not be removed from this file.
  12. class CPDFPageNavigationSliderBar extends StatefulWidget {
  13. final CPDFSliderIndicatorPosition position;
  14. final CPDFViewCtrl ctrl;
  15. const CPDFPageNavigationSliderBar(
  16. {super.key, required this.ctrl, required this.position});
  17. @override
  18. State<CPDFPageNavigationSliderBar> createState() =>
  19. _CPDFPageNavigationSliderBarState();
  20. }
  21. class _CPDFPageNavigationSliderBarState
  22. extends State<CPDFPageNavigationSliderBar> {
  23. int _pageCount = 1;
  24. @override
  25. void initState() {
  26. super.initState();
  27. _initDocumentData();
  28. }
  29. void _initDocumentData() async {
  30. try{
  31. int pageCount = await widget.ctrl.document.getPageCount();
  32. setState(() {
  33. _pageCount = pageCount;
  34. });
  35. }catch(e){
  36. }
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return StreamBuilder(
  41. initialData: 0,
  42. stream: widget.ctrl.currentPageIndexStream.stream,
  43. builder: (context, pageIndex) {
  44. if (pageIndex.data != null) {
  45. return CPDFSliderBar(
  46. position: widget.position,
  47. value: (pageIndex.data! + 1).toDouble(),
  48. maxValue: _pageCount.toDouble(),
  49. callback: (pageIndex) {
  50. widget.ctrl.setDisplayPageIndex(pageIndex);
  51. },
  52. );
  53. } else {
  54. return CPDFSliderBar(
  55. position: widget.position,
  56. value: 1,
  57. maxValue: _pageCount.toDouble(),
  58. callback: (pageIndex) {
  59. widget.ctrl.setDisplayPageIndex(pageIndex);
  60. },
  61. );
  62. }
  63. },
  64. );
  65. }
  66. }
  67. typedef CPDFPageNavigationCallback = void Function(int pageIndex);
  68. class CPDFSliderBar extends StatefulWidget {
  69. final double sliderBarWidth;
  70. final double sliderBarHeight;
  71. final double maxValue;
  72. double value;
  73. final CPDFSliderIndicatorPosition position;
  74. final CPDFPageNavigationCallback? callback;
  75. CPDFSliderBar(
  76. {super.key,
  77. required this.value,
  78. required this.maxValue,
  79. this.position = CPDFSliderIndicatorPosition.right,
  80. this.sliderBarWidth = 36,
  81. this.sliderBarHeight = 24,
  82. this.callback});
  83. @override
  84. State<CPDFSliderBar> createState() => _CPDFSliderBarState();
  85. }
  86. class _CPDFSliderBarState extends State<CPDFSliderBar> {
  87. ui.Image? image;
  88. @override
  89. void initState() {
  90. super.initState();
  91. _initImage();
  92. }
  93. void _initImage() async {
  94. final data = await rootBundle
  95. .load('packages/compdfkit_flutter/assets/images/ic_slider_bar.png');
  96. final bytes = data.buffer.asUint8List();
  97. final image1 = await decodeImageFromList(bytes);
  98. setState(() {
  99. image = image1;
  100. });
  101. }
  102. @override
  103. void dispose() {
  104. super.dispose();
  105. }
  106. @override
  107. Widget build(BuildContext context) {
  108. bool isVertical = true;
  109. if (widget.position == CPDFSliderIndicatorPosition.top ||
  110. widget.position == CPDFSliderIndicatorPosition.bottom) {
  111. isVertical = false;
  112. }
  113. if (image != null) {
  114. return RotatedBox(
  115. quarterTurns: isVertical ? 1 : 0,
  116. child: SliderTheme(
  117. data: SliderTheme.of(context).copyWith(
  118. inactiveTickMarkColor: Colors.transparent,
  119. inactiveTrackColor: Colors.transparent,
  120. activeTrackColor: Colors.transparent,
  121. trackHeight: 10,
  122. overlayShape: SliderComponentShape.noOverlay,
  123. thumbShape: CPDFImageSliderThumbShape(
  124. image: image!,
  125. imageWidth: widget.sliderBarWidth,
  126. imageHeight: widget.sliderBarHeight),
  127. thumbColor: Colors.white,
  128. valueIndicatorShape: CPDFPageSliderValueIndicatorShape(
  129. position: widget.position),
  130. showValueIndicator: ShowValueIndicator.always,
  131. valueIndicatorColor: Colors.black,
  132. valueIndicatorTextStyle:
  133. const TextStyle(color: Colors.white, fontSize: 12)),
  134. child: Slider(
  135. value: widget.value,
  136. min: 1,
  137. label: '${widget.value.toInt()}/${widget.maxValue.toInt()}',
  138. max: widget.maxValue,
  139. onChanged: (newValue) {
  140. setState(() {
  141. widget.value = newValue;
  142. });
  143. },
  144. onChangeEnd: (newValue) {
  145. if (widget.callback != null) {
  146. widget.callback!((newValue - 1).toInt());
  147. }
  148. },
  149. ),
  150. ));
  151. } else {
  152. return const Padding(padding: EdgeInsets.zero);
  153. }
  154. }
  155. }