cpdf_base_input_dialog_widget.dart 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import 'package:compdfkit_flutter/theme/themes.dart';
  2. import 'package:flutter/material.dart';
  3. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  4. ///
  5. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  6. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  7. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  8. /// This notice may not be removed from this file.
  9. class CPDFPageNavigationWidget extends StatefulWidget {
  10. int pageCount = 0;
  11. String title;
  12. String hintText;
  13. String cancelText;
  14. String positiveText;
  15. bool isDark = false;
  16. CPDFPageNavigationWidget(
  17. {required this.pageCount,
  18. this.title = 'Enter a page number',
  19. this.hintText = 'Page (1/~)',
  20. this.cancelText = 'CANCEL',
  21. this.positiveText = 'OK',
  22. this.isDark = true,
  23. super.key});
  24. @override
  25. State<CPDFPageNavigationWidget> createState() =>
  26. _CPDFPageNavigationWidgetState();
  27. }
  28. class _CPDFPageNavigationWidgetState extends State<CPDFPageNavigationWidget> {
  29. bool _enable = false;
  30. final TextEditingController _controller = TextEditingController();
  31. @override
  32. void initState() {
  33. super.initState();
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. return Theme(
  38. data: widget.isDark ? comPDFKitDarkTheme : comPDFKitLightTheme,
  39. child: Builder(builder: (context) {
  40. return AlertDialog(
  41. contentPadding:
  42. const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
  43. title: Text(widget.title,
  44. style: Theme.of(context).textTheme.titleMedium),
  45. content: SizedBox(
  46. width: 260,
  47. child: TextField(
  48. keyboardType: TextInputType.number,
  49. controller: _controller,
  50. decoration: textFieldStyle(context).copyWith(
  51. hintText: widget.hintText,
  52. ),
  53. onChanged: (v) {
  54. int jumpPageIndex = int.tryParse(v) ?? 0;
  55. setState(() {
  56. _enable = v.isNotEmpty &&
  57. jumpPageIndex > 0 &&
  58. jumpPageIndex <= widget.pageCount;
  59. });
  60. },
  61. ),
  62. ),
  63. actions: <Widget>[
  64. TextButton(
  65. onPressed: () {
  66. Navigator.pop(context);
  67. },
  68. child: Text(widget.cancelText)),
  69. TextButton(
  70. onPressed: !_enable
  71. ? null
  72. : () {
  73. int pageIndex =
  74. int.tryParse(_controller.value.text) ?? 0;
  75. Navigator.pop(context, pageIndex - 1);
  76. },
  77. child: Text(
  78. widget.positiveText,
  79. style: TextStyle(),
  80. )),
  81. ],
  82. );
  83. }));
  84. }
  85. }