view.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import '../../../common/routers/index.dart';
  4. import 'index.dart';
  5. import 'widgets/hw_painter.dart';
  6. class HandwritingPage extends GetView<HandwritingController> {
  7. const HandwritingPage({Key? key}) : super(key: key);
  8. /// 打开手写板
  9. static Future<dynamic> startWriting() async {
  10. return await Get.toNamed(O2OARoutes.commonHandwriting);
  11. }
  12. // 主视图
  13. Widget _buildView(BuildContext context) {
  14. return Container(
  15. color: Colors.white,
  16. constraints: const BoxConstraints.expand(),
  17. child: Stack(children: [
  18. _painterView(context),
  19. _closeBtn(),
  20. _saveBtn(context),
  21. _clearBtn()
  22. ]));
  23. }
  24. /// 手写板画布
  25. Widget _painterView(BuildContext context) {
  26. return GestureDetector(
  27. onPanUpdate: (detail) {
  28. RenderObject? referenceBox = context.findRenderObject();
  29. if (referenceBox != null && referenceBox is RenderBox) {
  30. Offset localPosition =
  31. referenceBox.globalToLocal(detail.globalPosition);
  32. controller.paintByData(localPosition);
  33. }
  34. },
  35. onPanEnd: (detail) => controller.paintByData(null),
  36. child: Obx(() {
  37. final points = controller.state.points.toList();
  38. return CustomPaint(
  39. size: MediaQuery.of(context).size,
  40. painter: HWPainter(points),
  41. );
  42. }),
  43. );
  44. }
  45. /// 清空画布按钮
  46. Widget _clearBtn() {
  47. return Positioned(
  48. right: 16,
  49. bottom: 16,
  50. child: IconButton(
  51. icon: const Icon(
  52. Icons.refresh,
  53. size: 32,
  54. color: Colors.black,
  55. ),
  56. onPressed: () => controller.refreshPainter(),
  57. ),
  58. );
  59. }
  60. /// 保存按钮
  61. Widget _saveBtn(BuildContext context) {
  62. return Positioned(
  63. right: 16,
  64. top: 16,
  65. child: TextButton(
  66. child: Text('save'.tr),
  67. onPressed: ()=> controller.savePainterToImage(MediaQuery.of(context).size),
  68. ),
  69. );
  70. }
  71. /// 关闭按钮
  72. Widget _closeBtn() {
  73. return Positioned(
  74. left: 16,
  75. top: 16,
  76. child: IconButton(
  77. icon: const Icon(
  78. Icons.close,
  79. size: 32,
  80. color: Colors.black,
  81. ),
  82. onPressed: () {
  83. Get.back();
  84. },
  85. ),
  86. );
  87. }
  88. @override
  89. Widget build(BuildContext context) {
  90. return GetBuilder<HandwritingController>(
  91. builder: (_) {
  92. return Scaffold(
  93. body: SafeArea(
  94. child: _buildView(context),
  95. ),
  96. );
  97. },
  98. );
  99. }
  100. }