view.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_inappwebview/flutter_inappwebview.dart';
  4. import 'package:get/get.dart';
  5. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  6. import 'package:vitalapp/components/appbar.dart';
  7. import 'package:vitalapp/components/button.dart';
  8. import 'controller.dart';
  9. // ignore: must_be_immutable
  10. class ContractTemplatePage extends GetView<ContractTemplateController> {
  11. ContractTemplatePage({super.key});
  12. late InAppWebViewController? _webViewController;
  13. @override
  14. Widget build(BuildContext context) {
  15. return Scaffold(
  16. // backgroundColor: const Color.fromRGBO(238, 238, 238, 1),
  17. appBar: VAppBar(
  18. titleWidget: const Text(
  19. "合同",
  20. style: TextStyle(fontSize: 24),
  21. ),
  22. ),
  23. body: Stack(
  24. children: [
  25. FutureBuilder(
  26. future: controller.loadData(),
  27. builder: (context, snapshot) {
  28. if (snapshot.connectionState != ConnectionState.done) {
  29. return const Center(
  30. child: SizedBox(
  31. width: 20,
  32. height: 20,
  33. child: CircularProgressIndicator(),
  34. ),
  35. );
  36. } else {
  37. return InAppWebView(
  38. initialData: InAppWebViewInitialData(
  39. data:
  40. '<body style="padding: 100px;">${controller.state.templateContent}</body>',
  41. ),
  42. initialOptions: InAppWebViewGroupOptions(
  43. crossPlatform: InAppWebViewOptions(
  44. useShouldOverrideUrlLoading: false,
  45. mediaPlaybackRequiresUserGesture: false,
  46. cacheEnabled: true,
  47. ),
  48. android: AndroidInAppWebViewOptions(
  49. //设置为 true 以启用混合组合
  50. useHybridComposition: true,
  51. ),
  52. ),
  53. onLoadStart: (controller, url) {
  54. PromptBox.loading('加载合同中');
  55. // 显示加载指示器
  56. },
  57. onLoadStop: (controller, url) {
  58. PromptBox.dismiss();
  59. },
  60. onWebViewCreated: (InAppWebViewController controller) {
  61. _webViewController = controller;
  62. },
  63. );
  64. }
  65. },
  66. ),
  67. Obx(
  68. () => Positioned(
  69. bottom: 8,
  70. left: 200,
  71. right: 200,
  72. child: Row(
  73. mainAxisAlignment: MainAxisAlignment.spaceAround,
  74. children: [
  75. VButton(
  76. label: "签字",
  77. onTap: () {
  78. gotoSignature();
  79. },
  80. ),
  81. if (controller.state.userSignature != '')
  82. VButton(
  83. label: "提交",
  84. onTap: () => controller.submitContract(),
  85. ),
  86. ],
  87. ),
  88. ),
  89. )
  90. ],
  91. ),
  92. );
  93. }
  94. Future<void> _scrollToBottom() async {
  95. if (_webViewController != null) {
  96. await _webViewController!.scrollTo(
  97. x: 0,
  98. y: 3000,
  99. animated: true,
  100. );
  101. }
  102. }
  103. Future<void> gotoSignature() async {
  104. final result = await Get.toNamed("/contract/signature");
  105. if (kDebugMode) {
  106. // TODO: 调试记录签名
  107. // await TextStorage(
  108. // fileName: "signature_${DateTime.now().millisecondsSinceEpoch}.txt",
  109. // directory: "debug",
  110. // ).save(result);
  111. }
  112. controller.state.userSignature = result;
  113. final dto = await controller.contractTemplateManager
  114. .getContractTemplateDetail(controller.state.templateCode);
  115. if (dto != null) {
  116. controller.state.templateContent = dto.templateContent ?? '';
  117. controller.getkey();
  118. }
  119. _scrollToBottom();
  120. _webViewController?.loadData(
  121. data:
  122. '<body style="padding: 100px;">${controller.state.templateContent}</body>',
  123. mimeType: 'text/html',
  124. encoding: 'utf-8',
  125. );
  126. }
  127. }