cpdf_reader_widget_display_setting_page.dart 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright © 2014-2025 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. *
  9. */
  10. import 'dart:io';
  11. import 'package:compdfkit_flutter/configuration/cpdf_options.dart';
  12. import 'package:compdfkit_flutter/util/extension/cpdf_color_extension.dart';
  13. import 'package:compdfkit_flutter/widgets/cpdf_reader_widget_controller.dart';
  14. import 'package:flutter/material.dart';
  15. class CpdfReaderWidgetDisplaySettingPage extends StatefulWidget {
  16. final CPDFReaderWidgetController controller;
  17. const CpdfReaderWidgetDisplaySettingPage(
  18. {super.key, required this.controller});
  19. @override
  20. State<CpdfReaderWidgetDisplaySettingPage> createState() =>
  21. _CpdfReaderWidgetDisplaySettingPageState();
  22. }
  23. class _CpdfReaderWidgetDisplaySettingPageState
  24. extends State<CpdfReaderWidgetDisplaySettingPage> {
  25. bool _isVertical = true;
  26. CPDFDisplayMode _displayMode = CPDFDisplayMode.singlePage;
  27. bool _isLinkHighlight = true;
  28. bool _isFormFieldHighlight = true;
  29. bool _isContinuous = true;
  30. bool _isCrop = true;
  31. bool _canScale = true;
  32. CPDFThemes _themes = CPDFThemes.light;
  33. @override
  34. void initState() {
  35. super.initState();
  36. initReaderViewWidgetStates();
  37. }
  38. void initReaderViewWidgetStates() async {
  39. CPDFReaderWidgetController controller = widget.controller;
  40. bool isVer = await controller.isVerticalMode();
  41. bool isDoublePageMode = await controller.isDoublePageMode();
  42. bool isCoverPageMode = await controller.isCoverPageMode();
  43. bool isLinkHighlight = await controller.isLinkHighlight();
  44. bool isFormFieldHighlight = await controller.isFormFieldHighlight();
  45. bool isContinuous = await controller.isContinueMode();
  46. bool isCrop = await controller.isCropMode();
  47. Color themeColor = await controller.getReadBackgroundColor();
  48. debugPrint('themeColor: ${themeColor.toHex()}');
  49. setState(() {
  50. _isVertical = isVer;
  51. if (isDoublePageMode) {
  52. if (isCoverPageMode) {
  53. _displayMode = CPDFDisplayMode.coverPage;
  54. } else {
  55. _displayMode = CPDFDisplayMode.doublePage;
  56. }
  57. } else {
  58. _displayMode = CPDFDisplayMode.singlePage;
  59. }
  60. _isLinkHighlight = isLinkHighlight;
  61. _isFormFieldHighlight = isFormFieldHighlight;
  62. _isContinuous = isContinuous;
  63. _isCrop = isCrop;
  64. _themes = CPDFThemes.of(themeColor);
  65. });
  66. }
  67. @override
  68. Widget build(BuildContext context) {
  69. return Container(
  70. height: 600,
  71. decoration: BoxDecoration(
  72. color: Theme.of(context).scaffoldBackgroundColor,
  73. borderRadius: const BorderRadius.only(
  74. topLeft: Radius.circular(12), topRight: Radius.circular(12))),
  75. child: Column(children: [
  76. Container(
  77. margin: const EdgeInsets.only(left: 16),
  78. alignment: Alignment.centerLeft,
  79. width: double.infinity,
  80. height: 56,
  81. child: Text(
  82. 'Settings',
  83. style: Theme.of(context).textTheme.headlineSmall,
  84. ),
  85. ),
  86. Expanded(child: SingleChildScrollView(
  87. physics: const BouncingScrollPhysics(),
  88. child: Column(
  89. children: [
  90. _scrollItem(),
  91. _displayModeItem(),
  92. _otherItem(),
  93. _themesItem()
  94. ],
  95. ))),
  96. ],),
  97. );
  98. }
  99. Widget _scrollItem() {
  100. return Column(
  101. children: [
  102. _dividerLine('Scrolling'),
  103. _checkItem(
  104. 'Vertical Scrolling',
  105. _isVertical,
  106. () async {
  107. await _updateVerticalMode(true);
  108. },
  109. ),
  110. _checkItem('Horizontal Scrolling', !_isVertical, () async {
  111. await _updateVerticalMode(false);
  112. })
  113. ],
  114. );
  115. }
  116. Future<void> _updateVerticalMode(bool isVertical) async {
  117. if (mounted) {
  118. setState(() {
  119. _isVertical = isVertical;
  120. });
  121. }
  122. await widget.controller.setVerticalMode(isVertical);
  123. }
  124. Widget _displayModeItem() {
  125. return Column(
  126. children: [
  127. _dividerLine('Display Mode'),
  128. _checkItem(
  129. 'Single Page',
  130. _displayMode == CPDFDisplayMode.singlePage,
  131. () async {
  132. await _updateDisplayMode(CPDFDisplayMode.singlePage);
  133. },
  134. ),
  135. _checkItem('Two Page', _displayMode == CPDFDisplayMode.doublePage,
  136. () async {
  137. await _updateDisplayMode(CPDFDisplayMode.doublePage);
  138. }),
  139. _checkItem('Cover Mode', _displayMode == CPDFDisplayMode.coverPage,
  140. () async {
  141. await _updateDisplayMode(CPDFDisplayMode.coverPage);
  142. })
  143. ],
  144. );
  145. }
  146. Future<void> _updateDisplayMode(CPDFDisplayMode displayMode) async {
  147. if (mounted) {
  148. setState(() {
  149. _displayMode = displayMode;
  150. });
  151. }
  152. if (displayMode == CPDFDisplayMode.singlePage) {
  153. await widget.controller.setDoublePageMode(false);
  154. } else if (displayMode == CPDFDisplayMode.doublePage) {
  155. await widget.controller.setDoublePageMode(true);
  156. } else {
  157. await widget.controller.setCoverPageMode(true);
  158. }
  159. }
  160. Widget _otherItem() {
  161. return Column(
  162. children: [
  163. _dividerLine(''),
  164. _switchItem('Highlight Links', _isLinkHighlight, (check) async {
  165. setState(() {
  166. _isLinkHighlight = check;
  167. });
  168. await widget.controller.setLinkHighlight(check);
  169. }),
  170. _switchItem('Highlight Form Fields', _isFormFieldHighlight,
  171. (check) async {
  172. setState(() {
  173. _isFormFieldHighlight = check;
  174. });
  175. await widget.controller.setFormFieldHighlight(check);
  176. }),
  177. _switchItem('Continuous Scrolling', _isContinuous, (check) async {
  178. setState(() {
  179. _isContinuous = check;
  180. });
  181. await widget.controller.setContinueMode(check);
  182. }),
  183. _switchItem('Crop', _isCrop, (check) async {
  184. setState(() {
  185. _isCrop = check;
  186. });
  187. await widget.controller.setCropMode(check);
  188. }),
  189. if (Platform.isAndroid) ...{
  190. _switchItem('Can Scale', _canScale, (check) async {
  191. setState(() {
  192. _canScale = check;
  193. });
  194. await widget.controller.setCanScale(check);
  195. }),
  196. }
  197. ],
  198. );
  199. }
  200. Widget _themesItem() {
  201. return Column(
  202. children: [
  203. _dividerLine('Themes'),
  204. _checkItem('Light', _themes == CPDFThemes.light, () async {
  205. await _updateTheme(CPDFThemes.light);
  206. }),
  207. _checkItem('Dark', _themes == CPDFThemes.dark, () async {
  208. await _updateTheme(CPDFThemes.dark);
  209. }),
  210. _checkItem('Sepia', _themes == CPDFThemes.sepia, () async {
  211. await _updateTheme(CPDFThemes.sepia);
  212. }),
  213. _checkItem('Reseda', _themes == CPDFThemes.reseda, () async {
  214. await _updateTheme(CPDFThemes.reseda);
  215. })
  216. ],
  217. );
  218. }
  219. Future<void> _updateTheme(CPDFThemes themes) async {
  220. if (mounted) {
  221. setState(() {
  222. _themes = themes;
  223. });
  224. }
  225. await widget.controller.setReadBackgroundColor(themes);
  226. }
  227. Widget _dividerLine(String title) {
  228. return Container(
  229. decoration: BoxDecoration(
  230. borderRadius: BorderRadius.circular(4),
  231. color: Theme.of(context).appBarTheme.backgroundColor),
  232. padding: const EdgeInsets.only(left: 8, top: 4, bottom: 4),
  233. margin: const EdgeInsets.symmetric(horizontal: 8),
  234. width: double.infinity,
  235. child: Text(title,
  236. style: Theme.of(context)
  237. .textTheme
  238. .bodyLarge
  239. ?.copyWith(fontWeight: FontWeight.w500)),
  240. );
  241. }
  242. Widget _checkItem(String title, bool isCheck, GestureTapCallback onTap) {
  243. return ListTile(
  244. onTap: onTap,
  245. title: Text(title, style: Theme.of(context).textTheme.bodyMedium),
  246. trailing: isCheck
  247. ? Icon(Icons.check, color: Theme.of(context).colorScheme.primary)
  248. : null,
  249. );
  250. }
  251. Widget _switchItem(String title, bool isCheck, ValueChanged<bool> changed) {
  252. return ListTile(
  253. title: Text(title, style: Theme.of(context).textTheme.bodyMedium),
  254. trailing: Transform.scale(
  255. scale: 0.8,
  256. child: Switch(value: isCheck, onChanged: changed),
  257. ),
  258. );
  259. }
  260. }