{"id":661,"date":"2023-03-07T21:07:00","date_gmt":"2023-03-07T13:07:00","guid":{"rendered":"http:\/\/gjweb.top\/?p=661"},"modified":"2023-03-07T21:07:01","modified_gmt":"2023-03-07T13:07:01","slug":"flutter-%e7%94%b5%e5%ad%90%e7%ad%be%e5%90%8d","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=661","title":{"rendered":"flutter \u7535\u5b50\u7b7e\u540d"},"content":{"rendered":"\n<p>\u4f7f\u7528 signature \u7b2c\u4e09\u65b9\u5e93\u5b8c\u6210 flutter \u7684\u7535\u5b50\u7b7e\u540d, \u5728 flutter3.0 \u4e2d\u9a8c\u8bc1\u4f7f\u7528\u6b63\u5e38<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5b89\u88c5\">\u5b89\u88c5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>signature: ^5.3.2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u4f7f\u7528\">\u4f7f\u7528<\/h2>\n\n\n\n<p>\u5c01\u88c5\u4e86 signature \u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u5730\u65b9\u8c03\u7528 MySigature(onClone(){get.back();})<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'dart:developer';<br>import 'package:flutter\/material.dart';<br>import \"package:get\/get.dart\";<br>import \"dart:convert\";<br>\u200b<br>import 'package:signature\/signature.dart';<br>import 'dart:typed_data';<br>import '..\/controllers\/global_controller.dart';<br>\u200b<br>class MySigature extends StatefulWidget {<br> &nbsp;void Function() onClone; \/\/ \u7528\u4e8e\u5173\u95ed\u5f39\u7a97<br> &nbsp;MySigature({super.key,required this.onClone});<br>\u200b<br> &nbsp;@override<br> &nbsp;State&lt;MySigature&gt; createState() =&gt; _MySigatureState();<br>}<br>\u200b<br>class _MySigatureState extends State&lt;MySigature&gt; {<br> &nbsp;GlobalController globalController = Get.find();<br> &nbsp;\/\/ \u5b9e\u4f8b\u5316\u7b7e\u540d\u51fd\u6570<br> &nbsp;final SignatureController _controller = SignatureController(<br> &nbsp; &nbsp;penStrokeWidth: 2,<br> &nbsp; &nbsp;penColor: Colors.black,<br> &nbsp; &nbsp;exportBackgroundColor: Colors.white,<br> &nbsp; &nbsp;exportPenColor: Colors.black,<br> &nbsp; &nbsp;onDrawStart: () =&gt; log('onDrawStart called!'),<br> &nbsp; &nbsp;onDrawEnd: () =&gt; log('onDrawEnd called!'),<br>  );<br>\u200b<br> &nbsp;@override<br> &nbsp;void initState() {<br> &nbsp; &nbsp;super.initState();<br> &nbsp; &nbsp;_controller.addListener(() =&gt; log('Value changed'));<br>  }<br> &nbsp;@override<br> &nbsp;void dispose() {<br> &nbsp; &nbsp;\/\/ IMPORTANT to dispose of the controller<br> &nbsp; &nbsp;_controller.dispose();<br> &nbsp; &nbsp;super.dispose();<br>  }<br> &nbsp;<br> &nbsp;Future&lt;void&gt; exportImage(BuildContext context) async {<br> &nbsp; &nbsp;if (_controller.isEmpty) {<br> &nbsp; &nbsp; &nbsp;ScaffoldMessenger.of(context).showSnackBar(<br> &nbsp; &nbsp; &nbsp; &nbsp;const SnackBar(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;key: Key('snackbarPNG'),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;content: Text('\u8bf7\u7b7e\u5b57\u540e\u4fdd\u5b58'),<br> &nbsp; &nbsp; &nbsp;  ),<br> &nbsp; &nbsp;  );<br> &nbsp; &nbsp; &nbsp;return;<br> &nbsp;  }<br> &nbsp; &nbsp;final Uint8List? data = await _controller.toPngBytes(height: 500, width: 500);<br> &nbsp; &nbsp;final String dataStr = base64Encode(data!);<br> &nbsp; &nbsp;\/\/ \u5c06\u626b\u7801\u7ed3\u679c\u5b58\u653e\u5728 \u5168\u5c40\u6570\u636econtroller\u4e2d\uff0c\u8be5\u53c2\u6570\u662f Uint8List \u9700\u8981\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u7c7b\u578b\u7136\u540e\u5728\u5b58\u653e\u5168\u5c40\u7684controller<br> &nbsp; &nbsp;globalController.mockUphold&#91;0]&#91;\"userSign\"] = dataStr;<br> &nbsp; &nbsp;widget.onClone();<br> &nbsp; &nbsp;setState(() =&gt; _controller.clear());<br> &nbsp; &nbsp;\/\/ print(\"\u6570\u636e\u54cd\u5e94\u4e2d\u7684\u7b7e\u540d\u5b58\u50a8 ${globalController.mockUphold&#91;0]}\",);<br>\u200b<br> &nbsp; &nbsp;\/\/ final Uint8List decodedData = base64Decode(dataStr);<br> &nbsp; &nbsp;\/\/ print(\"\u9006\u89e3\u6790 ${decodedData}\");<br>  }<br>\u200b<br> &nbsp;@override<br> &nbsp;Widget build(BuildContext context) {<br> &nbsp; &nbsp;return Column(<br> &nbsp; &nbsp; &nbsp;children: &#91;<br> &nbsp; &nbsp; &nbsp; &nbsp;Signature(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;key: const Key('signature'),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;controller: _controller,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height: 300,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;backgroundColor: Colors.grey&#91;300]!,<br> &nbsp; &nbsp; &nbsp;  ),<br> &nbsp; &nbsp; &nbsp; &nbsp;Row(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mainAxisAlignment: MainAxisAlignment.spaceAround,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;children: &#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IconButton(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;icon: const Icon(Icons.undo),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color: Colors.blue,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;onPressed: () {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setState(() =&gt; _controller.undo());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  },<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tooltip: 'Undo',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IconButton(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;icon: const Icon(Icons.redo),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color: Colors.blue,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;onPressed: () {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setState(() =&gt; _controller.redo());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  },<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tooltip: 'Redo',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/CLEAR CANVAS<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ElevatedButton(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;key: const Key('clear'),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;onPressed: () {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setState(() =&gt; _controller.clear());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  },<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;child: const Text('\u6e05\u7a7a'),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ElevatedButton(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;key: const Key('exportPNG'),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;onPressed: (){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exportImage(context);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  } ,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;child:const Text('\u4fdd\u5b58\u56fe\u7247'),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ),<br> &nbsp; &nbsp; &nbsp; &nbsp;  ],<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp; &nbsp;  ],<br> &nbsp;  );<br>  }<br>}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 signature \u7b2c\u4e09\u65b9\u5e93\u5b8c\u6210 flutter \u7684\u7535\u5b50\u7b7e\u540d, \u5728 flutter3.0 \u4e2d\u9a8c\u8bc1\u4f7f\u7528\u6b63\u5e38 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63],"tags":[],"class_list":["post-661","post","type-post","status-publish","format-standard","hentry","category-flutter"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=661"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/661\/revisions"}],"predecessor-version":[{"id":662,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/661\/revisions\/662"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}