cpd_input_dialog_widget.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import 'package:compdfkit_flutter/common/util/Strings.dart';
  2. import 'package:compdfkit_flutter/theme/themes.dart';
  3. import 'package:flutter/material.dart';
  4. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  5. ///
  6. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  7. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  8. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  9. /// This notice may not be removed from this file.
  10. Future<String?> showCPDFInputDialog(BuildContext context,
  11. {bool isDark = false,
  12. String title = '',
  13. String? text,
  14. String hintText = '',
  15. String? cancelText,
  16. String positiveText = '',
  17. VoidCallback? onCancel,
  18. ValueChanged<String>? onPositive}) async {
  19. String? value = await showDialog<String>(
  20. context: context,
  21. builder: (context) {
  22. return CPDFInputDialogWidget(
  23. title: title,
  24. text: text,
  25. isDark: isDark,
  26. hintText: hintText,
  27. cancelText: cancelText,
  28. positiveText: positiveText,
  29. onCancel: onCancel,
  30. onPositive: onPositive);
  31. });
  32. return value;
  33. }
  34. class CPDFInputDialogWidget extends StatefulWidget {
  35. final String title;
  36. final String hintText;
  37. final String? text;
  38. final String? cancelText;
  39. final String positiveText;
  40. final bool isDark;
  41. final VoidCallback? onCancel;
  42. final ValueChanged<String>? onPositive;
  43. const CPDFInputDialogWidget(
  44. {this.title = '',
  45. this.text,
  46. this.hintText = '',
  47. this.cancelText,
  48. this.positiveText = Strings.OK,
  49. this.isDark = false,
  50. this.onCancel,
  51. this.onPositive,
  52. super.key});
  53. @override
  54. State<CPDFInputDialogWidget> createState() => _CPDFInputDialogWidgetState();
  55. }
  56. class _CPDFInputDialogWidgetState extends State<CPDFInputDialogWidget> {
  57. final TextEditingController _controller = TextEditingController();
  58. @override
  59. void initState() {
  60. super.initState();
  61. if (widget.text != null) {
  62. _controller.text = widget.text!;
  63. }
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. return Theme(
  68. data: widget.isDark ? comPDFKitDarkTheme : comPDFKitLightTheme,
  69. child: Builder(builder: (context) {
  70. return AlertDialog(
  71. contentPadding:
  72. const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
  73. title: Text(widget.title,
  74. style: Theme.of(context).textTheme.titleMedium),
  75. content: SizedBox(
  76. width: 260,
  77. child: TextField(
  78. keyboardType: TextInputType.number,
  79. controller: _controller,
  80. decoration: textFieldStyle(context).copyWith(
  81. hintText: widget.hintText,
  82. ),
  83. onChanged: (v) {},
  84. ),
  85. ),
  86. actions: <Widget>[
  87. if (widget.cancelText != null) ...{
  88. TextButton(
  89. onPressed: () {
  90. if (widget.onCancel != null) {
  91. widget.onCancel!();
  92. }
  93. },
  94. child: Text(widget.cancelText!))
  95. },
  96. TextButton(
  97. onPressed: () {
  98. if (widget.onPositive != null) {
  99. widget.onPositive!(_controller.text);
  100. }
  101. },
  102. child: Text(
  103. widget.positiveText,
  104. )),
  105. ],
  106. );
  107. }));
  108. }
  109. }