cpdf_outline_item.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  2. ///
  3. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  4. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  5. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  6. /// This notice may not be removed from this file.
  7. import 'package:compdfkit_flutter/core/document/cpdf_outline_data.dart';
  8. import 'package:flutter/material.dart';
  9. import '../../../../theme/colors.dart';
  10. typedef OutlineExpandedCallback = Function(bool isExpanded);
  11. class CPDFOutlineItem extends StatefulWidget {
  12. final CPDFOutlineData outlineData;
  13. final ValueChanged<int>? onDisplayPageIndex;
  14. final OutlineExpandedCallback? onExpanded;
  15. const CPDFOutlineItem(
  16. {Key? key,
  17. required this.outlineData,
  18. this.onDisplayPageIndex,
  19. this.onExpanded})
  20. : super(key: key);
  21. @override
  22. State<CPDFOutlineItem> createState() => _CPDFOutlineItemState();
  23. }
  24. class _CPDFOutlineItemState extends State<CPDFOutlineItem> {
  25. bool _expanded = false;
  26. @override
  27. void initState() {
  28. super.initState();
  29. _expanded = widget.outlineData.expanded;
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. CPDFOutlineData data = widget.outlineData;
  34. _expanded = widget.outlineData.expanded;
  35. return Ink(
  36. color: _backgroundColor(context, data.level == 1),
  37. child: InkWell(
  38. child: Column(
  39. children: [
  40. Row(
  41. children: [
  42. Padding(
  43. padding: EdgeInsets.only(left: (data.level - 1) * 8),
  44. child: AnimatedOpacity(
  45. opacity: data.childOutline != null &&
  46. data.childOutline!.isNotEmpty
  47. ? 1.0
  48. : 0.0,
  49. duration: const Duration(milliseconds: 100),
  50. child: IconButton(
  51. splashRadius: 20,
  52. icon: ImageIcon(AssetImage(
  53. _expanded
  54. ? 'assets/images/ic_arrow_down.png'
  55. : 'assets/images/ic_arrow_right.png',
  56. package: 'compdfkit_flutter')),
  57. onPressed: () {
  58. if (data.childOutline != null &&
  59. data.childOutline!.isNotEmpty) {
  60. if (widget.onExpanded != null) {
  61. widget.onExpanded!(_expanded);
  62. }
  63. _expanded = !_expanded;
  64. widget.outlineData.expanded = _expanded;
  65. }
  66. },
  67. ),
  68. )),
  69. Expanded(
  70. child: Text(
  71. widget.outlineData.title,
  72. style: Theme.of(context).textTheme.titleSmall,
  73. maxLines: 1,
  74. overflow: TextOverflow.ellipsis,
  75. )),
  76. Padding(
  77. padding: const EdgeInsets.only(right: 16),
  78. child: Text(
  79. (widget.outlineData.pageIndex + 1).toString(),
  80. style: const TextStyle(fontSize: 12),
  81. ),
  82. )
  83. ],
  84. ),
  85. const Divider(height: 0.5)
  86. ],
  87. ),
  88. onTap: () {
  89. if (widget.onDisplayPageIndex != null) {
  90. widget.onDisplayPageIndex!(data.pageIndex);
  91. }
  92. },
  93. ),
  94. );
  95. }
  96. Color _backgroundColor(BuildContext context, bool root) {
  97. if (root) {
  98. if (Theme.of(context).brightness == Brightness.dark) {
  99. return CPDFColors.readerSettingHeadBgColorDark;
  100. } else {
  101. return CPDFColors.readerSettingHeadBgColorLight;
  102. }
  103. } else {
  104. if (Theme.of(context).brightness == Brightness.dark) {
  105. return const Color(0xFF414347);
  106. } else {
  107. return Colors.white;
  108. }
  109. }
  110. }
  111. }