cpdf_page_indicator.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'dart:async';
  2. import 'package:compdfkit_flutter/core/cpdf_view_ctrl.dart';
  3. import 'package:flutter/material.dart';
  4. import '../dialog/cpdf_base_input_dialog_widget.dart';
  5. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. ///
  7. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. /// This notice may not be removed from this file.
  11. class CPDFPageIndicator extends StatefulWidget {
  12. final CPDFViewCtrl ctrl;
  13. Decoration? decoration;
  14. TextStyle? textStyle;
  15. bool alwaysShow;
  16. CPDFPageIndicator(
  17. {required this.ctrl,
  18. this.decoration,
  19. this.textStyle,
  20. this.alwaysShow = false,
  21. super.key}) {
  22. decoration ??= const BoxDecoration(
  23. color: Color(0xCC000000),
  24. borderRadius: BorderRadius.all(Radius.circular(2)));
  25. textStyle ??= const TextStyle(color: Colors.white, fontSize: 12);
  26. }
  27. @override
  28. State<CPDFPageIndicator> createState() => _CPDFPageIndicatorState();
  29. }
  30. class _CPDFPageIndicatorState extends State<CPDFPageIndicator> {
  31. int _pageCount = 0;
  32. Timer? _timer;
  33. bool _showIndicator = true;
  34. @override
  35. void initState() {
  36. super.initState();
  37. _initDocumentData();
  38. }
  39. void _initDocumentData() async {
  40. widget.ctrl.document.getPageCount().then((value) {
  41. setState(() {
  42. _pageCount = value;
  43. });
  44. });
  45. if (!widget.alwaysShow) {
  46. widget.ctrl.scrollStatusController.stream.listen((event) {
  47. if (event) {
  48. if (!_showIndicator) {
  49. setState(() {
  50. _timer?.cancel();
  51. _showIndicator = event;
  52. });
  53. }
  54. } else {
  55. handleScrollEnd();
  56. }
  57. });
  58. }
  59. }
  60. @override
  61. void dispose() {
  62. super.dispose();
  63. _timer?.cancel();
  64. }
  65. void handleScrollEnd() {
  66. _timer?.cancel();
  67. _timer = Timer(const Duration(seconds: 3), () {
  68. setState(() {
  69. _showIndicator = false;
  70. });
  71. });
  72. }
  73. @override
  74. Widget build(BuildContext context) {
  75. return AnimatedOpacity(
  76. opacity: _showIndicator ? 1.0 : 0.0,
  77. duration: const Duration(milliseconds: 200),
  78. child: GestureDetector(
  79. child: Container(
  80. constraints: BoxConstraints(minWidth: 50),
  81. decoration: widget.decoration,
  82. padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
  83. child: Row(
  84. mainAxisAlignment: MainAxisAlignment.center,
  85. mainAxisSize: MainAxisSize.min,
  86. children: [
  87. StreamBuilder(
  88. initialData: 0,
  89. stream: widget.ctrl.currentPageIndexStream.stream,
  90. builder: (context, pageIndex) {
  91. return Text((pageIndex.data! + 1).toString(),
  92. style: widget.textStyle);
  93. }),
  94. Text('/', style: widget.textStyle),
  95. Text(_pageCount.toString(), style: widget.textStyle)
  96. ],
  97. ),
  98. ),
  99. onTap: () async {
  100. int? jumpPageIndex = await showDialog<int?>(
  101. context: context,
  102. builder: (BuildContext context) {
  103. return CPDFPageNavigationWidget(
  104. pageCount: _pageCount,
  105. hintText: 'Page (1/$_pageCount)',
  106. );
  107. });
  108. if (jumpPageIndex != null) {
  109. widget.ctrl.setDisplayPageIndex(jumpPageIndex);
  110. }
  111. },
  112. ));
  113. }
  114. }