cpdf_outline_page.dart 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ///
  2. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  3. ///
  4. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  5. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  6. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  7. /// This notice may not be removed from this file.
  8. import 'package:compdfkit_flutter/core/document/cpdf_document.dart';
  9. import 'package:compdfkit_flutter/core/document/cpdf_outline_data.dart';
  10. import 'package:flutter/material.dart';
  11. import 'cpdf_outline_item.dart';
  12. class CPDFOutlinePage extends StatefulWidget {
  13. final CPDFDocument document;
  14. const CPDFOutlinePage({Key? key, required this.document}) : super(key: key);
  15. @override
  16. State<CPDFOutlinePage> createState() => _CPDFOutlinePageState();
  17. }
  18. class _CPDFOutlinePageState extends State<CPDFOutlinePage> with AutomaticKeepAliveClientMixin
  19. {
  20. List<CPDFOutlineData> _outlineList = List.empty(growable: true);
  21. final globalKey = GlobalKey<AnimatedListState>();
  22. @override
  23. void initState() {
  24. super.initState();
  25. widget.document.getOutline().then((value) {
  26. setState(() {
  27. _outlineList = value;
  28. });
  29. });
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. return ListView.builder(
  34. itemCount: _outlineList.length,
  35. itemBuilder: (context, index) {
  36. return CPDFOutlineItem(
  37. outlineData: _outlineList[index],
  38. onDisplayPageIndex: (pageIndex) {
  39. Navigator.pop(context, pageIndex);
  40. },
  41. onExpanded: (expanded) {
  42. setState(() {
  43. if (expanded) {
  44. loopCollapseItem(_outlineList[index]);
  45. } else {
  46. _outlineList[index].expanded = true;
  47. _outlineList.insertAll(
  48. index + 1, _outlineList[index].childOutline ?? []);
  49. }
  50. });
  51. },
  52. );
  53. });
  54. }
  55. @override
  56. bool get wantKeepAlive => true;
  57. void loopCollapseItem(CPDFOutlineData data) {
  58. data.expanded = false;
  59. data.childOutline?.forEach((element) {
  60. if (element.childOutline != null && element.childOutline!.isNotEmpty) {
  61. loopCollapseItem(element);
  62. }
  63. _outlineList.remove(element);
  64. });
  65. }
  66. }