美文网首页
flutter 手写画板

flutter 手写画板

作者: 从容到没边的优雅 | 来源:发表于2022-09-23 15:32 被阅读0次

使用库 signature: ^5.2.1

import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flu_merchant/app_constants.dart';
import 'package:flu_merchant/ui/common/common.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:signature/signature.dart';


typedef CallBack = void Function(Uint8List? imageByte);
class DrawingBoardPage extends StatefulWidget {
  final CallBack? drawComplete;
  bool landScape; // 不用了
  DrawingBoardPage({
    required this.landScape,
    this.drawComplete,
    Key? key}) : super(key: key);

  @override
  State<DrawingBoardPage> createState() => _DrawingBoardPageState();
}

class _DrawingBoardPageState extends State<DrawingBoardPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeRight]);
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  }
  final SignatureController _signatureController = SignatureController(
    penStrokeWidth: 3,
    penColor: Colors.black,
    exportBackgroundColor: Colors.white,
    exportPenColor: Colors.black,
    onDrawStart: () => print('onDrawStart called!'),
    onDrawEnd: () => print('onDrawEnd called!'),
  );
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        // width: ScreenUtil().screenWidth,
        // height: ScreenUtil().screenHeight,
        color: Colors.white,
        child: FittedBox(
          fit: BoxFit.contain,
          child: DrawingView(
            signatureController: _signatureController,
            width: 1.sh,
            height: 1.sw,
            landScape: widget.landScape,
            drawComplete: (image) {
              if (widget.drawComplete != null) {
                widget.drawComplete!(image);
              }
            },
            resetCallback: () {
              setState(() {
              });
            },
          ),
        ),
      ),
    );
  }
}



class DrawingView extends StatefulWidget {
  final CallBack? drawComplete;
  final SignatureController signatureController;
  final resetCallback; // 还原大小回调
  final double width;
  final double height;
  late bool landScape; // 是否横屏
  DrawingView(
      {Key? key,
        this.drawComplete,
        required this.signatureController,
        this.resetCallback,
        required this.width,
        required this.height,
        required this.landScape})
      : super(key: key);

  @override
  _DrawingViewState createState() => _DrawingViewState();
}

class _DrawingViewState extends State<DrawingView> {
  bool isEmpty = true;

  @override
  void initState() {
    super.initState();

    if (widget.signatureController.value.length > 0) {
      isEmpty = false;
    } else {
      isEmpty = true;
    }

    // 监听画板
    widget.signatureController.addListener(() {
      bool tmpIsEmpty = true;
      if (widget.signatureController.value.length > 0) {
        tmpIsEmpty = false;
      } else {
        tmpIsEmpty = true;
      }
      if (isEmpty != tmpIsEmpty) {
        if (this.mounted) {
          setState(() {
            isEmpty = tmpIsEmpty;
          });
        }
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return RotatedBox(
      quarterTurns: widget.landScape ? 1 : 0,
      child: aaa(),
    );
  }

  Widget aaa() {
    return Stack(
      alignment: Alignment.center,
      children: [
        Signature(
          controller: widget.signatureController,
          width: widget.width,
          height: widget.height,
          backgroundColor: clFFF4F4F4,
        ),
        Positioned(
          top: 0,
          child:  Container(
            height: 44, width: widget.height,
            color: Colors.white,
            padding: EdgeInsets.symmetric(horizontal: MediaQuery.of(context).padding.bottom+20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  onTap: (){
                    Navigator.pop(context);
                  },
                  child: Container(
                    width: 30,
                    child: Image.asset(ImageConstants.nav_back_balck, width: 25, height: 25),),
                ),
                GestureDetector(
                  onTap: () async {
                    Navigator.pop(context);
                    ByteData? bytes = await (
                        await widget.signatureController.toImage(
                            width: widget.height.ceil(),height: widget.width.ceil()
                        ))?.toByteData(format: ui.ImageByteFormat.png);
                    if (widget.drawComplete != null) {
                      widget.drawComplete!(bytes?.buffer.asUint8List());
                    }
                  },
                  child: Text('确定', style: TextStyle(color: clFF0BA198)),
                )
              ],
            ),
          ),
        ),
        // 暂无签名
        Offstage(
          offstage: isEmpty ? false : true,
          child: Text(
            '签名(必填)',
            style: TextStyle(
              fontSize: 44.sp,
              color: clFFE5E5E5,
            ),
          ),
        ),
        Positioned(
          right: 20, bottom: 40,
          child: Row(
            children: [
              GestureDetector(
                child: Row(
                  children: [
                    Image.asset(ImageConstants.ic_reset, width: 24, height: 24),
                    Text('清除', style: TextStyle(color: clFF0BA198, fontSize: 16))
                  ],
                ),
                onTap: () {
                  setState(() {
                    widget.signatureController.clear();
                  });
                },
              ),
            ],
          ),
        ),
        Positioned(
          bottom: 40,
          child: Text(
            '用手指签名,注意字迹清晰',
            style: TextStyle(
              fontSize: 16,
              color: clFF999999,
            ),
          ),
        )
      ],
    );
  }
}

相关文章

  • flutter 手写画板

    使用库 signature: ^5.2.1[https://pub.dev/packages/signature]

  • Flutter画板实现

    原文链接 更多教程 效果 代码 原文链接 更多教程

  • Flutter自定义画板

    1. 绘制直线 绘制直线需要调用Canvas的drawLine方法,传入起点和终点坐标即可。 完整代码示例: 2....

  • ios 手写签名(生成图片)

    最近公司一个项目要用到一个手写签名的功能感觉非常有意思, 第一个想法就是类似于画板的功能, 如何实现ios画板请看...

  • iOS 签名涂鸦画板

    ZWGraphicView 签名涂鸦画板 功能 实际开发中主要用于手写签名及其它绘画涂鸦,最终生成图片,用于上传服...

  • Flutter手写签名signature

    签名用的signature插件https://pub.dev/packages/signature[https:/...

  • Kevin Learn Android:Android 手签板

    前言 Android 屏幕手写签名的原理就是把手机屏幕当作画板,把用户手指当作画笔,手指在屏幕上在屏幕上划来划去,...

  • Flutter ORM工具包

    刚入坑Flutter不久,一直没看到有flutter的orm工具包,所以就自己动手写了一个。 简介 一个以注解方式...

  • flutter-画板签名,并生成图片

    今天实现一个可以签名的画板功能,并截屏生成图片,如下: 有问题随时交流, 对你有帮助的话, 帮忙点点小心心哦~

  • Flutter 实现手写签名效果

    如何使用Flutter实现手写签名的效果 思路 需要监听用户触摸的起始点和结束点,并记录途经点,这里我使用了Str...

网友评论

      本文标题:flutter 手写画板

      本文链接:https://www.haomeiwen.com/subject/wzsnortx.html