Browse Source

ComPDFKit(flutter) - 1.代码优化

liuxiaolong 1 year ago
parent
commit
661c157cf2

+ 2 - 2
example/lib/main.dart

@@ -57,12 +57,12 @@ class _MyAppState extends State<MyApp> {
           Positioned(
             top: 20,
             left: 10,
-            child: Text('VersionCode:$_version', style: TextStyle(fontSize: 10),),
+            child: Text('VersionCode:$_version', style: const TextStyle(fontSize: 10),),
           ),
           Positioned(
             top: 50,
             left: 10,
-            child: Text('BuildTag:$_buildTag', style: TextStyle(fontSize: 10),),
+            child: Text('BuildTag:$_buildTag', style: const TextStyle(fontSize: 10),),
           )
         ],
       ))),

+ 8 - 10
example/lib/widgets/cpdf_readerview_page.dart

@@ -1,9 +1,9 @@
 import 'package:compdfkit_flutter/core/cpdf_view_ctrl.dart';
 import 'package:compdfkit_flutter/cpdf_configuration.dart';
-import 'package:compdfkit_flutter/tools/common/views/pdfview/cpdf_page_indicator.dart';
-import 'package:compdfkit_flutter/tools/common/views/pdfview/cpdf_page_slider_bar.dart';
-import 'package:compdfkit_flutter/tools/common/views/pdfview/cpdf_reader_widget.dart';
-import 'package:compdfkit_flutter/tools/common/views/slider/cpdf_slider_theme.dart';
+import 'package:compdfkit_flutter/widgets/common/pdfview/cpdf_page_indicator.dart';
+import 'package:compdfkit_flutter/widgets/common/pdfview/cpdf_page_slider_bar.dart';
+import 'package:compdfkit_flutter/widgets/common/pdfview/cpdf_reader_widget.dart';
+import 'package:compdfkit_flutter/widgets/common/slider/cpdf_slider_theme.dart';
 import 'package:flutter/material.dart';
 
 ///  Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
@@ -44,14 +44,13 @@ class _CPDFReaderViewPageState extends State<CPDFReaderViewPage> {
           },
         ),
         if (ctrl != null) ...{
-          Positioned(
-              bottom: 20,
-              child: CPDFPageIndicator(ctrl: ctrl!)),
+          Positioned(bottom: 20, child: CPDFPageIndicator(ctrl: ctrl!)),
           Positioned(
               right: 0,
               top: 0,
               bottom: 0,
-              child: CPDFPageNavigationSliderBar(ctrl: ctrl!, position: CPDFSliderIndicatorPosition.right))
+              child: CPDFPageNavigationSliderBar(
+                  ctrl: ctrl!, position: CPDFSliderIndicatorPosition.right))
         },
       ],
     );
@@ -59,8 +58,7 @@ class _CPDFReaderViewPageState extends State<CPDFReaderViewPage> {
 
   void setReaderViewCallback() {
     ctrl!.setReaderViewCallback(
-        onMoveToChild: (pageIndex) {
-        },
+        onMoveToChild: (pageIndex) {},
         onScrollEnd: () {},
         onRecordLastJumpPageNum: (pageIndex) {});
   }

+ 9 - 1
lib/core/cpdf_view_ctrl.dart

@@ -24,7 +24,7 @@ class CPDFViewCtrl {
   var currentPageIndexStream = StreamController<int>.broadcast();
   var _currentPageIndex = 0;
 
-  var scrollStatusController = StreamController<int>.broadcast();
+  var scrollStatusController = StreamController<bool>.broadcast();
   var _isScrolling = false;
 
   StreamSubscription? _streamSubscription;
@@ -93,11 +93,17 @@ class CPDFViewCtrl {
           if (onScrollEnd != null) {
             onScrollEnd();
           }
+          if (_isScrolling){
+            _isScrolling = false;
+            scrollStatusController.sink.add(_isScrolling);
+          }
           break;
         case 'onScrolling':
           if (onScrolling != null) {
             onScrolling();
           }
+          _isScrolling = true;
+          scrollStatusController.sink.add(_isScrolling);
           break;
         default:
           break;
@@ -106,6 +112,8 @@ class CPDFViewCtrl {
   }
 
   void dispose(){
+    currentPageIndexStream.close();
+    scrollStatusController.close();
     _streamSubscription?.cancel();
   }
 }

+ 0 - 86
lib/tools/common/views/pdfview/cpdf_page_indicator.dart

@@ -1,86 +0,0 @@
-import 'package:compdfkit_flutter/core/cpdf_view_ctrl.dart';
-import 'package:flutter/material.dart';
-
-import '../dialog/cpdf_base_input_dialog_widget.dart';
-
-///  Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
-///
-///  THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
-///  AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
-///  UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
-///  This notice may not be removed from this file.
-
-class CPDFPageIndicator extends StatefulWidget {
-  final CPDFViewCtrl ctrl;
-
-  Decoration? decoration;
-
-  TextStyle? textStyle;
-
-  CPDFPageIndicator(
-      {required this.ctrl, this.decoration, this.textStyle, super.key}) {
-    decoration ??= const BoxDecoration(
-        color: Color(0xCC000000),
-        borderRadius: BorderRadius.all(Radius.circular(2)));
-    textStyle ??= const TextStyle(color: Colors.white);
-  }
-
-  @override
-  State<CPDFPageIndicator> createState() => _CPDFPageIndicatorState();
-}
-
-class _CPDFPageIndicatorState extends State<CPDFPageIndicator> {
-  int _pageCount = 0;
-
-  @override
-  void initState() {
-    super.initState();
-    _initDocumentData();
-  }
-
-  void _initDocumentData() async {
-    widget.ctrl.document.getPageCount().then((value) {
-      setState(() {
-        _pageCount = value;
-      });
-    });
-  }
-
-  @override
-  Widget build(BuildContext context) {
-    return GestureDetector(
-      child: Container(
-        decoration: widget.decoration,
-        padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
-        child: Row(
-          mainAxisAlignment: MainAxisAlignment.center,
-          mainAxisSize: MainAxisSize.min,
-          children: [
-            StreamBuilder(
-                initialData: 1,
-                stream: widget.ctrl.currentPageIndexStream.stream,
-                builder: (context, pageIndex) {
-                  return Text((pageIndex.data! + 1).toString(),
-                      style: widget.textStyle);
-                }),
-            Text('/', style: widget.textStyle),
-            Text(_pageCount.toString(), style: widget.textStyle)
-          ],
-        ),
-      ),
-      onTap: () async {
-        int? jumpPageIndex = await showDialog<int?>(
-            context: context,
-            builder: (BuildContext context) {
-              return CPDFPageNavigationWidget(
-                pageCount: _pageCount,
-                hintText: 'Page (1/$_pageCount)',
-              );
-            });
-        if (jumpPageIndex != null) {
-          widget.ctrl.setDisplayPageIndex(jumpPageIndex);
-        }
-      },
-    );
-  }
-}

+ 0 - 0
lib/tools/common/views/test..dart


lib/tools/common/views/dialog/cpdf_base_input_dialog_widget.dart → lib/widgets/common/dialog/cpdf_base_input_dialog_widget.dart


+ 132 - 0
lib/widgets/common/pdfview/cpdf_page_indicator.dart

@@ -0,0 +1,132 @@
+import 'dart:async';
+
+import 'package:compdfkit_flutter/core/cpdf_view_ctrl.dart';
+import 'package:flutter/material.dart';
+
+import '../dialog/cpdf_base_input_dialog_widget.dart';
+
+
+///  Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
+///
+///  THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
+///  AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
+///  UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
+///  This notice may not be removed from this file.
+
+class CPDFPageIndicator extends StatefulWidget {
+  final CPDFViewCtrl ctrl;
+
+  Decoration? decoration;
+
+  TextStyle? textStyle;
+
+  bool alwaysShow;
+
+  CPDFPageIndicator(
+      {required this.ctrl,
+      this.decoration,
+      this.textStyle,
+      this.alwaysShow = false,
+      super.key}) {
+    decoration ??= const BoxDecoration(
+        color: Color(0xCC000000),
+        borderRadius: BorderRadius.all(Radius.circular(2)));
+    textStyle ??= const TextStyle(color: Colors.white, fontSize: 12);
+  }
+
+  @override
+  State<CPDFPageIndicator> createState() => _CPDFPageIndicatorState();
+}
+
+class _CPDFPageIndicatorState extends State<CPDFPageIndicator> {
+  int _pageCount = 0;
+
+  Timer? _timer;
+
+  bool _showIndicator = true;
+
+  @override
+  void initState() {
+    super.initState();
+    _initDocumentData();
+  }
+
+  void _initDocumentData() async {
+    widget.ctrl.document.getPageCount().then((value) {
+      setState(() {
+        _pageCount = value;
+      });
+    });
+    if (!widget.alwaysShow) {
+      widget.ctrl.scrollStatusController.stream.listen((event) {
+        if (event) {
+          if (!_showIndicator) {
+            setState(() {
+              _timer?.cancel();
+              _showIndicator = event;
+            });
+          }
+        } else {
+          handleScrollEnd();
+        }
+      });
+    }
+  }
+
+  @override
+  void dispose() {
+    super.dispose();
+    _timer?.cancel();
+  }
+
+  void handleScrollEnd() {
+    _timer?.cancel();
+    _timer = Timer(const Duration(seconds: 3), () {
+      setState(() {
+        _showIndicator = false;
+      });
+    });
+  }
+
+  @override
+  Widget build(BuildContext context) {
+    return AnimatedOpacity(
+        opacity: _showIndicator ? 1.0 : 0.0,
+        duration: const Duration(milliseconds: 200),
+        child: GestureDetector(
+          child: Container(
+            constraints: BoxConstraints(minWidth: 50),
+            decoration: widget.decoration,
+            padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
+            child: Row(
+              mainAxisAlignment: MainAxisAlignment.center,
+              mainAxisSize: MainAxisSize.min,
+              children: [
+                StreamBuilder(
+                    initialData: 0,
+                    stream: widget.ctrl.currentPageIndexStream.stream,
+                    builder: (context, pageIndex) {
+                      return Text((pageIndex.data! + 1).toString(),
+                          style: widget.textStyle);
+                    }),
+                Text('/', style: widget.textStyle),
+                Text(_pageCount.toString(), style: widget.textStyle)
+              ],
+            ),
+          ),
+          onTap: () async {
+            int? jumpPageIndex = await showDialog<int?>(
+                context: context,
+                builder: (BuildContext context) {
+                  return CPDFPageNavigationWidget(
+                    pageCount: _pageCount,
+                    hintText: 'Page (1/$_pageCount)',
+                  );
+                });
+            if (jumpPageIndex != null) {
+              widget.ctrl.setDisplayPageIndex(jumpPageIndex);
+            }
+          },
+        ));
+  }
+}

+ 2 - 1
lib/tools/common/views/pdfview/cpdf_page_slider_bar.dart

@@ -2,9 +2,10 @@ import 'package:compdfkit_flutter/core/cpdf_view_ctrl.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
 
-import '../slider/cpdf_slider_theme.dart';
 import 'dart:ui' as ui;
 
+import '../slider/cpdf_slider_theme.dart';
+
 ///  Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
 ///
 ///  THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW

lib/tools/common/views/pdfview/cpdf_reader_widget.dart → lib/widgets/common/pdfview/cpdf_reader_widget.dart


lib/tools/common/views/slider/cpdf_slider_theme.dart → lib/widgets/common/slider/cpdf_slider_theme.dart