attr_link_widget.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import 'package:flutter/material.dart';
  2. import 'package:kmpdfkit_demo/widgets/contains.dart';
  3. import 'package:kmpdfkit_demo/widgets/events.dart';
  4. import '../../models/annot_attribute_bean.dart';
  5. /// attr_link_widget.dart
  6. ///
  7. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  8. ///
  9. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  10. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  11. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  12. /// This notice may not be removed from this file.
  13. typedef AttrLinkCallback = Function(LinkType linkType, String linkUrl, int linkPage, String linkEmail);
  14. typedef AttrLinkCancelCreateCallback = Function(bool cancelCreateLink);
  15. class AttrLinkWidget extends StatefulWidget {
  16. final AttrLinkCallback callback;
  17. final AttrLinkCancelCreateCallback cancelCreateCallback;
  18. final AnnotAttributeBean attributeBean;
  19. const AttrLinkWidget({Key? key,required this.attributeBean, required this.callback, required this.cancelCreateCallback}) : super(key: key);
  20. @override
  21. State<AttrLinkWidget> createState() => _AttrLinkWidgetState();
  22. }
  23. class _AttrLinkWidgetState extends State<AttrLinkWidget> {
  24. final TextEditingController _webTextController =
  25. TextEditingController(text: 'http://');
  26. final TextEditingController _pageTextController = TextEditingController();
  27. final TextEditingController _emailTextController = TextEditingController();
  28. late LinkType selectType;
  29. bool linkDataIsValid = true;
  30. int pageCount = 1;
  31. @override
  32. void initState() {
  33. super.initState();
  34. initLinkData();
  35. getDocumentPageCount().then((value) {
  36. setState(() {
  37. pageCount = value;
  38. });
  39. });
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. return AlertDialog(
  44. title: const Text('New Link'),
  45. content: Column(
  46. mainAxisSize: MainAxisSize.min,
  47. children: [
  48. Row(
  49. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  50. children: [
  51. _linkTypeWidget(LinkType.website, Icons.language, 'URL'),
  52. _linkTypeWidget(LinkType.page, Icons.description_outlined, 'Page'),
  53. _linkTypeWidget(LinkType.email, Icons.email_outlined, 'Email')
  54. ],
  55. ),
  56. if (selectType == LinkType.website) ...{
  57. TextField(
  58. controller: _webTextController,
  59. keyboardType: TextInputType.url,
  60. onChanged: (value){
  61. checkDataIsValid();
  62. },
  63. )
  64. } else if (selectType == LinkType.page) ...{
  65. TextField(
  66. controller: _pageTextController,
  67. keyboardType: TextInputType.number,
  68. decoration: InputDecoration(hintText: '1-$pageCount',hintStyle: const TextStyle(fontSize: 12)),
  69. onChanged: (value){
  70. checkDataIsValid();
  71. },
  72. )
  73. } else ...{
  74. TextField(
  75. controller: _emailTextController,
  76. keyboardType: TextInputType.emailAddress,
  77. decoration: const InputDecoration(hintText: 'support@17pdf.com',hintStyle: TextStyle(fontSize: 12)),
  78. onChanged: (value){
  79. checkDataIsValid();
  80. },
  81. )
  82. }
  83. ],
  84. ),
  85. actions: [
  86. TextButton(
  87. onPressed: () {
  88. widget.cancelCreateCallback(true);
  89. Navigator.pop(context);
  90. },
  91. child: const Text('Cancel', style: TextStyle(color: Colors.grey),)),
  92. TextButton(onPressed: linkDataIsValid ? () {
  93. widget.callback(selectType, _webTextController.text, _pageTextController.text.isNotEmpty ? int.parse(_pageTextController.text) : 0, _emailTextController.text);
  94. Navigator.pop(context);
  95. } : null, child: Text('Done',style: TextStyle(color: linkDataIsValid ? Colors.blue : Colors.grey),))
  96. ],
  97. );
  98. }
  99. void checkDataIsValid() {
  100. setState(() {
  101. switch(selectType){
  102. case LinkType.website:
  103. linkDataIsValid = _webTextController.text.isNotEmpty;
  104. break;
  105. case LinkType.page :
  106. linkDataIsValid = _pageTextController.text.isNotEmpty;
  107. break;
  108. case LinkType.email:
  109. linkDataIsValid = _emailTextController.text.isNotEmpty;
  110. break;
  111. }
  112. });
  113. }
  114. Widget _linkTypeWidget(LinkType linkType, IconData icon, String title) {
  115. return InkWell(
  116. child: Column(
  117. children: [
  118. Icon(icon, color: selectType == linkType ? Colors.blue : Colors.grey),
  119. Text(
  120. title,
  121. style: TextStyle(
  122. color: selectType == linkType ? Colors.blue : Colors.grey),
  123. )
  124. ],
  125. ),
  126. onTap: (){
  127. setState(() {
  128. selectType = linkType;
  129. });
  130. checkDataIsValid();
  131. },
  132. );
  133. }
  134. void initLinkData(){
  135. selectType = widget.attributeBean.linkType;
  136. switch(selectType){
  137. case LinkType.website:
  138. _webTextController.text = widget.attributeBean.linkWeb.isEmpty ? "http://" : widget.attributeBean.linkWeb;
  139. break;
  140. case LinkType.email:
  141. _emailTextController.text = widget.attributeBean.linkEmail;
  142. break;
  143. case LinkType.page:
  144. _pageTextController.text = widget.attributeBean.linkPage.toString();
  145. break;
  146. }
  147. }
  148. }