import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:lszlgl/base/base_lifecycle_state.dart'; import 'package:signature/signature.dart'; import 'package:lszlgl/widget/button.dart'; class SignaturePageArgs { /// 签名数量 int count; SignaturePageArgs({this.count = 1}); @override String toString() { return {'count': count}.toString(); } } /// 电子签名 class SignaturePage extends StatefulWidget { final SignaturePageArgs args; const SignaturePage({ super.key, required this.args, }); @override State createState() => _SignaturePageState(); } class _SignaturePageState extends BaseLifecycleState { late int count; late List ctrlList; late PageController pageCtrl; final previousEnable = false.notifier(); final nextEnable = false.notifier(); final submitEnable = false.notifier(); /// 清除当前签名 void clearCurrent() { ctrlList[pageCtrl.page!.toInt()].clear(); } /// 上一个 void previousPage() { pageCtrl.jumpToPage(pageCtrl.page!.toInt() - 1); if (pageCtrl.page == 0) { previousEnable.value = false; } nextEnable.value = true; submitEnable.value = false; } /// 下一个 void nextPage() { if (!checkCurrentPage()) return; pageCtrl.jumpToPage(pageCtrl.page!.toInt() + 1); if (pageCtrl.page == count - 1) { nextEnable.value = false; submitEnable.value = true; } previousEnable.value = true; } void submit() async { if (!checkCurrentPage()) return; // 取出所有签名资源 List images = []; for (var ctrl in ctrlList) { images.add(await ctrl.toPngBytes()); } MyNavigator.pop(images); } bool checkCurrentPage() { if (ctrlList[pageCtrl.page!.toInt()].isEmpty) { MyNavigator.showToast('请先签名'); return false; } return true; } @override void onInit() { // 强制横屏 SystemChrome.setPreferredOrientations([ DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ]); // 创建签名列表 count = widget.args.count; ctrlList = List.generate( count, (index) => SignatureController( penStrokeWidth: 6, penColor: Colors.black, strokeJoin: StrokeJoin.round, strokeCap: StrokeCap.round, ), ).toList(); pageCtrl = PageController(); if (count == 1) { submitEnable.value = true; } else { nextEnable.value = true; } } @override void onDestroy() { SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, ]); for (var ctrl in ctrlList) { ctrl.dispose(); } } @override Widget build(BuildContext context) { return Scaffold( body: Stack( children: [ SizedBox(width: double.infinity, child: Image.asset(imgHomeTopBg, fit: BoxFit.cover)), buildBody(), ], ), ); } Widget buildBody() { return Column( children: [ myAppBar(title: '电子签名', toolbarHeight: 32), Expanded( child: Container( decoration: const BoxDecoration( color: Colors.white, ), width: double.infinity, child: PageView.builder( controller: pageCtrl, physics: const NeverScrollableScrollPhysics(), itemBuilder: (_, index) => Signature( controller: ctrlList[index], backgroundColor: Colors.transparent, ), ), ), ), buildBottom(), ], ); } Widget buildBottom() { EdgeInsets margin = EdgeInsets.only(left: 8, right: 8, top: 8, bottom: getBottomPadding(8)); return Row( children: [ Expanded(child: MyButton('清除重写', onTap: clearCurrent, margin: margin)), previousEnable.builder( (v) => v ? Expanded(child: MyButton('上一位', onTap: previousPage, margin: margin)) : const SizedBox.shrink()), nextEnable.builder( (v) => v ? Expanded(child: MyButton('下一位', onTap: nextPage, margin: margin)) : const SizedBox.shrink()), submitEnable.builder( (v) => v ? Expanded(child: MyButton('提交', onTap: submit, margin: margin)) : const SizedBox.shrink()), ], ); } }