123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- 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<SignaturePage> createState() => _SignaturePageState();
- }
- class _SignaturePageState extends BaseLifecycleState<SignaturePage> {
- late int count;
- late List<SignatureController> ctrlList;
- late PageController pageCtrl;
- final previousEnable = false.notifier<bool>();
- final nextEnable = false.notifier<bool>();
- final submitEnable = false.notifier<bool>();
- /// 清除当前签名
- 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<Uint8List?> 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()),
- ],
- );
- }
- }
|