【flutter滑动拼图验证码】

这篇具有很好参考价值的文章主要介绍了【flutter滑动拼图验证码】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

拼图验证码,flutter
Java后台使用aj_captcha插件,提供/captcha/get(获取captcha底图和拼块图片)、/captcha/check(验证拼图偏移量)这两个接口。并且这个插件在GitHub上有源码。
1.先准备好aj_captcha的工具类:

import 'dart:convert';

import 'package:steel_crypt/steel_crypt.dart';
//import 'package:encrypt/encrypt.dart';

class EncryptUtil {
  ///aes加密
  /// [key]AesCrypt加密key
  /// [content] 需要加密的内容字符串
  static String aesEncode({String key, String content}) {
    // var aesEncrypter = AesCrypt(key, 'ecb', 'pkcs7');

    var encodeKey = base64UrlEncode(utf8.encode(key));
    var aesEncrypter = AesCrypt(padding: PaddingAES.pkcs7, key: encodeKey);
    return aesEncrypter.ecb.encrypt(inp: content);
  }

  ///aes解密
  /// [key]aes解密key
  /// [content] 需要加密的内容字符串
  static String aesDecode({String key, String content}) {
    // var aesEncrypter = AesCrypt(key, 'ecb', 'pkcs7');
    var encodeKey = base64UrlEncode(utf8.encode(key));
    var aesEncrypter = AesCrypt(key: encodeKey, padding: PaddingAES.pkcs7);
    // return aesEncrypter.decrypt(content);
    return aesEncrypter.ecb.decrypt(enc: content);
  }
}

import 'dart:convert';

class ObjectUtils {
  /// isEmpty.
  static bool isEmpty(Object value) {
    if (value == null) return true;
    if (value is String && value.isEmpty) {
      return true;
    }
    return false;
  }

  //list length == 0  || list == null
  static bool isListEmpty(Object value) {
    if (value == null) return true;
    if (value is List && value.length == 0) {
      return true;
    }
    return false;
  }

  static String jsonFormat(Map<dynamic, dynamic> map) {
    Map _map = Map<String, Object>.from(map);
    JsonEncoder encoder = JsonEncoder.withIndent('  ');
    return encoder.convert(_map);
  }
}

import 'dart:async';
import 'package:flutter/widgets.dart';
import 'object_utils.dart';

/// Widget Util.
class WidgetUtil {
  bool _hasMeasured = false;
  double _width;
  double _height;

  /// Widget rendering listener.
  /// Widget渲染监听.
  /// context: Widget context.
  /// isOnce: true,Continuous monitoring  false,Listen only once.
  /// onCallBack: Widget Rect CallBack.
  void asyncPrepare(
      BuildContext context, bool isOnce, ValueChanged<Rect> onCallBack) {
    if (_hasMeasured) return;
    WidgetsBinding.instance.addPostFrameCallback((Duration timeStamp) {
      RenderBox box = context.findRenderObject();
      if (box != null && box.semanticBounds != null) {
        if (isOnce) _hasMeasured = true;
        double width = box.semanticBounds.width;
        double height = box.semanticBounds.height;
        if (_width != width || _height != height) {
          _width = width;
          _height = height;
          if (onCallBack != null) onCallBack(box.semanticBounds);
        }
      }
    });
  }

  /// Widget渲染监听.
  void asyncPrepares(bool isOnce, ValueChanged<Rect> onCallBack) {
    if (_hasMeasured) return;
    WidgetsBinding.instance.addPostFrameCallback((Duration timeStamp) {
      if (isOnce) _hasMeasured = true;
      if (onCallBack != null) onCallBack(null);
    });
  }

  ///get Widget Bounds (width, height, left, top, right, bottom and so on).Widgets must be rendered completely.
  ///获取widget Rect
  static Rect getWidgetBounds(BuildContext context) {
    RenderBox box = context.findRenderObject();
    return (box != null && box.semanticBounds != null)
        ? box.semanticBounds
        : Rect.zero;
  }

  ///Get the coordinates of the widget on the screen.Widgets must be rendered completely.
  ///获取widget在屏幕上的坐标,widget必须渲染完成
  static Offset getWidgetLocalToGlobal(BuildContext context) {
    RenderBox box = context.findRenderObject();
    return box == null ? Offset.zero : box.localToGlobal(Offset.zero);
  }

  /// get image width height,load error return Rect.zero.(unit px)
  /// 获取图片宽高,加载错误情况返回 Rect.zero.(单位 px)
  /// image
  /// url network
  /// local url , package
  static Future<Rect> getImageWH(
      {Image image, String url, String localUrl, String package}) {
    if (ObjectUtils.isEmpty(image) &&
        ObjectUtils.isEmpty(url) &&
        ObjectUtils.isEmpty(localUrl)) {
      return Future.value(Rect.zero);
    }
    Completer<Rect> completer = Completer<Rect>();
    Image img = image != null
        ? image
        : ((url != null && url.isNotEmpty)
            ? Image.network(url)
            : Image.asset(localUrl, package: package));
    img.image
        .resolve(new ImageConfiguration())
        .addListener(new ImageStreamListener(
          (ImageInfo info, bool _) {
            completer.complete(Rect.fromLTWH(0, 0, info.image.width.toDouble(),
                info.image.height.toDouble()));
          },
          onError: (dynamic exception, StackTrace stackTrace) {
            completer.completeError(exception, stackTrace);
          },
        ));
    return completer.future;
  }

  /// get image width height, load error throw exception.(unit px)
  /// 获取图片宽高,加载错误会抛出异常.(单位 px)
  /// image
  /// url network
  /// local url (full path/全路径,example:"assets/images/ali_connors.png",""assets/images/3.0x/ali_connors.png"" );
  /// package
  static Future<Rect> getImageWHE(
      {Image image, String url, String localUrl, String package}) {
    if (ObjectUtils.isEmpty(image) &&
        ObjectUtils.isEmpty(url) &&
        ObjectUtils.isEmpty(localUrl)) {
      return Future.error("image is null.");
    }
    Completer<Rect> completer = Completer<Rect>();
    Image img = image != null
        ? image
        : ((url != null && url.isNotEmpty)
            ? Image.network(url)
            : Image.asset(localUrl, package: package));
    img.image
        .resolve(new ImageConfiguration())
        .addListener(new ImageStreamListener(
          (ImageInfo info, bool _) {
            completer.complete(Rect.fromLTWH(0, 0, info.image.width.toDouble(),
                info.image.height.toDouble()));
          },
          onError: (dynamic exception, StackTrace stackTrace) {
            completer.completeError(exception, stackTrace);
          },
        ));

    return completer.future;
  }
}

2.绘制验证弹窗

import 'dart:convert';
import 'package:test/constant.dart';
import 'package:test/generated/l10n.dart';
import 'package:test/http/DioManager.dart';
import 'package:tset/util/easy_loading_util.dart';
import 'package:test/util/encrypt_util.dart';
import 'package:test/util/object_utils.dart';
import 'package:test/util/widtet_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';



typedef VoidSuccessCallback = dynamic Function(String c);

class CaptchaPage extends StatefulWidget {
  final VoidSuccessCallback onSuccess; //拖放完成后验证成功回调
  final VoidCallback onFail; //拖放完成后验证失败回调

  CaptchaPage({this.onSuccess, this.onFail});

  
  _CaptchaPageState createState() => _CaptchaPageState();
}

class _CaptchaPageState extends State<CaptchaPage>
    with TickerProviderStateMixin {
  /// 是否启用
  bool enable = true;

//  String baseImageBase64 =
//      "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCADIAlgDASIAAhEBAxEB/8QAHAABAAMBAQEBAQAAAAAAAAAAAAUGBwQIAwIB/8QASBAAAQMDAQUEBQYLBQkAAAAAAAECAwQFEQYHEiExURMiQWEycYGRoQgUI0KCsRUkM1JicpLB0eHwNDVzorM3U2NkdZOy0vH/xAAaAQEAAwEBAQAAAAAAAAAAAAAAAgMEBQEG/8QAMhEBAAIBAgMECQQCAwAAAAAAAAECAwQRBSExEkFR8BMiMmFxgaGxwQaR0eEUI0Jisv/aAAwDAQACEQMRAD8A9UgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAR9TeaCnXD6hrndGIrvuPYiZ6I2vWkb2nZIAr8mq6Fi4SGqcnVGt/ep+otWWt/5R80P68ar/wCOSfor+DNOv00TtN4hPA56Otpa1iupKiKZE57jkXHr6HQQmNurTW1bx2qzvAADxIBE3/UNusUSOr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zH2jOm7FNQOem/WWhjc8VbJI5fduJ95O2vYVRtejrxeZ52/7uliSL2K5yuynsQurouD4fWy55t7oj+p+8Lo1XEMvKuKK/GWe602j6j11Mlpt1PJS0c/dSgo8ySz8OT3ImXJz4IiJjnnGTQtkuyBljqYL1qhsU1zjw+npEVHR0zvznLyc9PDHBq8UyuFTTNNaWsumYHR2S3w0quTD5Ey6R/6z1y5fUq8CaI63j0eh/xdBT0ePv8AGfPfzmZ8WnT6G0W9LqLdq30gAB826IAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACIq9NWWrVVmtlLvKuVcyNGOX2twpxpojTycrev/AH5P/YsYLIy3jpaWPJw/SZJ3virM++sfwiqTTtnpMdjbqZFRco5zEeqe1cqSoBCbTbrK/Fgx4Y2xVise6NgAHi0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAf//Z";
  String baseImageBase64 = "";
  String slideImageBase64 = "";
  String captchaToken = "";
  String secretKey = "";
  Size baseSize = Size.zero; //底部基类图片
  Size slideSize = Size.zero; //滑块图片

  var sliderColor = Colors.white; //滑块的背景色
  var sliderIcon = Icons.arrow_forward; //滑块的图标
  var movedXBorderColor = Colors.white; //滑块拖动时,左边已滑的区域边框颜色
  double sliderStartX = 0; //滑块未拖前的X坐标
  double sliderXMoved = 0;
  bool sliderMoveFinish = false; //滑块拖动结束
  bool checkResultAfterDrag = false; //拖动后的校验结果

  //-------------动画------------
  int _checkMilliseconds = 0; //滑动时间
  bool _showTimeLine = false; //是否显示动画部件
  bool _checkSuccess = false; //校验是否成功
  AnimationController controller;


  var _ratio = 3.0;
  var dialogWidth;
  GlobalKey _baseImageKey = new GlobalKey();
  //高度动画
  Animation<double> offsetAnimation;

  //------------动画------------

  //校验通过
  void checkSuccess(String content) {
    setState(() {
      checkResultAfterDrag = true;
      _checkSuccess = true;
      _showTimeLine = true;
    });
    _forwardAnimation();
    updateSliderColorIcon();

    //刷新验证码
    Future.delayed(Duration(milliseconds: 1000)).then((v) {
      _reverseAnimation().then((v) {
        setState(() {
          _showTimeLine = false;
        });
        //回调
        if (widget.onSuccess != null) {
          widget.onSuccess(content);
          // NavigatorUtil.pop(value: true);
        }
        Navigator.pop(context);
      });
    });
  }

  //校验失败
  void checkFail() {
    setState(() {
      _showTimeLine = true;
      _checkSuccess = false;
      checkResultAfterDrag = false;
    });
    _forwardAnimation();
    updateSliderColorIcon();

    //刷新验证码
    Future.delayed(Duration(milliseconds: 1000)).then((v) {
      _reverseAnimation().then((v) {
        setState(() {
          _showTimeLine = false;
        });
        loadCaptcha();
        //回调
        if (widget.onFail != null) {
          widget.onFail();
        }
      });
    });
  }

  //重设滑动颜色与图标
  void updateSliderColorIcon() {
    var _sliderColor = null; //滑块的背景色
    var _sliderIcon = null; //滑块的图标
    var _movedXBorderColor = null; //滑块拖动时,左边已滑的区域边框颜色

    //滑块的背景色
    if (sliderMoveFinish) {
      //拖动结束
      _sliderColor = checkResultAfterDrag ? Colors.green : Colors.red;
      _sliderIcon = checkResultAfterDrag ? Icons.check : Icons.close;
      _movedXBorderColor = checkResultAfterDrag ? Colors.green : Colors.red;
    } else {
      //拖动未开始或正在拖动中
      _sliderColor = sliderXMoved > 0 ? Color(0xffe63850) : Colors.white;
      _sliderIcon = Icons.arrow_forward;
      _movedXBorderColor = Color(0xff89F2D0);
    }

    sliderColor = _sliderColor;
    sliderIcon = _sliderIcon;
    movedXBorderColor = _movedXBorderColor;
    setState(() {});
  }

  //加载验证码
  void loadCaptcha() {
    setState(() {
      _showTimeLine = false;
      sliderMoveFinish = false;
      checkResultAfterDrag = false;
      sliderColor = Colors.white; //滑块的背景色
      sliderIcon = Icons.arrow_forward; //滑块的图标
      movedXBorderColor = Colors.white; //滑块拖动时,左边已滑的区域边框颜色
    });
    DioManager.getInstance()
        .post(Constant.baseUrl + '/captcha/get', {"captchaType": "blockPuzzle"},
            (res) async {
      if (res['repCode'] != '0000' || res['repData'] == null) {
        setState(() {
          secretKey = "";
        });
        if (res['repCode'] == '6202') {
          enable = false;
          esLoadingError('您失败的次数太多啦,请稍后试试吧!');
        }

        return;
      }

      Map<String, dynamic> repData = res['repData'];
      print("--------------");
      print(repData.keys);
      print("${repData["point"]}");

      sliderXMoved = 0;
      sliderStartX = 0;
      captchaToken = '';
      checkResultAfterDrag = false;

      baseImageBase64 = repData["originalImageBase64"];
      baseImageBase64 = baseImageBase64.replaceAll('\n', '');
      secretKey = repData['secretKey'] ?? "";
      slideImageBase64 = repData["jigsawImageBase64"];
      slideImageBase64 = slideImageBase64.replaceAll('\n', '');
      captchaToken = repData["token"];

      print(baseImageBase64);

      var baseR = await WidgetUtil.getImageWH(
          image: Image.memory(Base64Decoder().convert(baseImageBase64)));
      baseSize = baseR.size;

      var silderR = await WidgetUtil.getImageWH(
          image: Image.memory(Base64Decoder().convert(slideImageBase64)));
      slideSize = silderR.size;
      enable = true;
      setState(() {});
    }, (error) {
      print(error);
    });
  }

  //校验验证码
  void checkCaptcha(sliderXMoved, captchaToken, {BuildContext myContext}) {
    setState(() {
      sliderMoveFinish = true;
    });
    //滑动结束,改变滑块的图标及颜色
//    updateSliderColorIcon();

    //pointJson参数需要aes加密

//    MediaQueryData mediaQuery = MediaQuery.of(myContext);
    /*
    * ScreenUtil().setHeight(20)*/

    print('sliderXMoved= $sliderXMoved');
    print('_baseImageKeyWidth ${_baseImageKey.currentContext.size.width}');
    print('_baseImageKeyWidthRatio= ${_baseImageKey.currentContext.size.width / baseSize.width}');
    //由于不同屏幕分辨率或者屏幕设置放大后拖动从最右侧拖动到同一位置的偏移量是不同的(屏幕),根据底图在屏幕上的实际宽度和从接口获取的底图的宽度(baseSize.width)的百分比来计算接口偏移量参数

    var pointMap = {"x": sliderXMoved / (_baseImageKey.currentContext.size.width / baseSize.width), "y": 5};//x:拖动的偏移量  y:偏移量误差范围 
    var pointStr = json.encode(pointMap);
    var cryptedStr = pointStr;

    /// secretKey 不为空,进行as加密
    if (!ObjectUtils.isEmpty(secretKey)) {
      // var aesEncrypter = AesCrypt(secretKey, 'ecb', 'pkcs7');
      cryptedStr = EncryptUtil.aesEncode(key: secretKey, content: pointStr);
      var dcrypt = EncryptUtil.aesDecode(key: secretKey, content: cryptedStr);
      // Map _map = json.decode(dcrypt);
    }

    // print("dcrypt ---- ${_map}");
    DioManager.getInstance().post(Constant.baseUrl + '/captcha/check', {
      "pointJson": cryptedStr,
      "captchaType": "blockPuzzle",
      "token": captchaToken
    }, (res) {
      if (res['repCode'] != '0000' || res['repData'] == null) {
        checkFail();
        return;
      }

      Map<String, dynamic> repData = res['repData'];
      if (repData["result"] != null && repData["result"] == true) {
        // 如果不加密 将 token 和 坐标序列化 通过 --- 链接成字符串

        var captchaVerification = '$captchaToken---$pointStr';
        if (!ObjectUtils.isEmpty(secretKey)) {
          // 如果加密 将 token 和 坐标序列化通过 --- 链接成字符串进行加密 加密秘钥为 _clickWordCaptchaModel.secretKey
          captchaVerification = EncryptUtil.aesEncode(
              key: secretKey, content: captchaVerification);
        }

        checkSuccess(captchaVerification);
      } else {
        checkFail();
      }
    }, (error) {
      loadCaptcha();
      print(error);
    });
  }

  
  void initState() {
    super.initState();
    initAnimation();
    loadCaptcha();
  }

  
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  // 初始化动画
  void initAnimation() {
    controller =
        AnimationController(duration: Duration(milliseconds: 500), vsync: this);

    offsetAnimation = Tween<double>(begin: 0.5, end: 0)
        .animate(CurvedAnimation(parent: controller, curve: Curves.ease))
          ..addListener(() {
            this.setState(() {});
          });
  }

  // 反向执行动画
  _reverseAnimation() async {
    await controller.reverse();
  }

  // 正向执行动画
  _forwardAnimation() async {
    await controller.forward();
  }

  
  void didUpdateWidget(CaptchaPage oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
  }

  
  Widget build(BuildContext context) {
    dialogWidth = 0.9 * MediaQuery.of(context).size.width;
   _ratio = MediaQuery.of(context).devicePixelRatio;
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: MediaQuery(
        data: MediaQueryData(devicePixelRatio: _ratio),
        child: Center(
          child: UnconstrainedBox(
            child: Container(
              width: dialogWidth,
              color: Colors.white,
              child: Stack(
                children: <Widget>[
                  Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      //顶部,提示+关闭
                      Container(
                        height: 50,
                        padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                        decoration: BoxDecoration(
                          border: Border(
                              bottom:
                              BorderSide(width: 1, color: Color(0xffe5e5e5))),
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Expanded(
                                child: Text(
                                  S.current.qingwanchenganquanyanzheng,
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(fontSize: 18),
                                  textScaleFactor: 1.0,
                                )),
                            IconButton(
                                padding: EdgeInsets.all(3),
                                icon: Icon(Icons.refresh),
                                iconSize: 30,
                                color: Colors.black54,
                                onPressed: () {
                                  //刷新
                                  loadCaptcha();
                                }),
                            IconButton(
                                padding: EdgeInsets.all(3),
                                icon: Icon(Icons.highlight_off),
                                iconSize: 30,
                                color: Colors.black54,
                                onPressed: () {
                                  //退出
                                  Navigator.pop(context);
                                }),
                          ],
                        ),
                      ),

                      //显示验证码
                      Container(
                        margin: EdgeInsets.all(10),
                        child: Stack(
                          children: <Widget>[
                            //底图 310*155
                            baseImageBase64.length > 0
                                ? Image.memory(
                            Base64Decoder().convert(baseImageBase64),
                          fit: BoxFit.fitWidth,
                          key: _baseImageKey,
                          height: 310.w,
                          gaplessPlayback: true,)
                                : Container(
                              width: dialogWidth - 20,
                              height: 310.w,
                              alignment: Alignment.center,
                              child: CircularProgressIndicator(),
                            ),

                            //滑块图
                            slideImageBase64.length > 0
                                ? Container(
                              margin: EdgeInsets.fromLTRB(sliderXMoved, 0, 0, 0),
                              child: Image.memory(
                                Base64Decoder().convert(slideImageBase64),
                               height: 310.w,
                                fit: BoxFit.fitHeight,
                                gaplessPlayback: true,
                              ),
                            )
                                : Container(),

                            Positioned(
                                bottom: 0,
                                left: -10,
                                right: -10,
                                child: Offstage(
                                  offstage: !_showTimeLine,
                                  child: FractionalTranslation(
                                    translation: Offset(0, offsetAnimation.value),
                                    child: Container(
                                      margin: EdgeInsets.only(left: 10, right: 10),
                                      padding: EdgeInsets.only(left: 10),
                                      height: 40,
                                      color: _checkSuccess
                                          ? Color(0x7F66BB6A)
                                          : Color.fromRGBO(200, 100, 100, 0.4),
                                      alignment: Alignment.centerLeft,
                                      child: Text(
                                        _checkSuccess
                                            ? "${(_checkMilliseconds / (60.0 * 12)).toStringAsFixed(2)}${S.current.yanzhengchenggong}"
                                            : S.current.yanzhengshibai,
                                        style: TextStyle(color: Colors.white),
                                      ),
                                    ),
                                  ),
                                )),
                            Positioned(
                                bottom: -20,
                                left: 0,
                                right: 0,
                                child: Offstage(
                                  offstage: !_showTimeLine,
                                  child: Container(
                                    margin: EdgeInsets.only(left: 10, right: 10),
                                    height: 20,
                                    color: Colors.white,
                                  ),
                                ))
                          ],
                        ),
                      ),

                      //底部,滑动区域
                      baseSize.width > 0
                          ? Container(
                          margin: EdgeInsets.all(10),
                          height: slideSize.width * (dialogWidth - 20) / baseSize.width,
                          width: baseSize.width * 2.w,
                          child: Stack(
                            alignment: AlignmentDirectional.centerStart,
                            children: <Widget>[
                              Container(
                                height: slideSize.width * (dialogWidth - 20) / baseSize.width,
                                decoration: BoxDecoration(
                                  border: Border.all(
                                    width: 1,
                                    color: Color(0xffe5e5e5),
                                  ),
                                  color: Color(0xffe1e1e1),
                                ),
                              ),
                              Container(
                                alignment: Alignment.center,
                                child: Text(
                                  S.current.xiangyouhuadong,
                                  style: TextStyle(fontSize: 16),
                                  textScaleFactor: 1.0,
                                ),
                              ),
                              Container(
                                width: sliderXMoved,
                                height: 58,
                                decoration: BoxDecoration(
                                  border: Border.all(
                                    width: sliderXMoved > 0 ? 1 : 0,
                                    color: movedXBorderColor,
                                  ),
                                  color: Color(0xff89F2D0),
                                ),
                              ),
                              GestureDetector(
                                onPanStart: (startDetails) {
                                  if (!enable) return;

                                  _checkMilliseconds =
                                      new DateTime.now().millisecondsSinceEpoch;
                                  print("startDetails");
                                  print(startDetails.globalPosition);

                                  sliderStartX = startDetails.globalPosition.dx;
                                  print(
                                      "startDetails --- sliderStartX ${sliderStartX} ");
                                },
                                onPanUpdate: (updateDetails) {
                                  if (!enable) return;

                                  print("updateDetails");
                                  print(updateDetails.globalPosition);

                                  double offset =
                                      updateDetails.globalPosition.dx  - sliderStartX;
                                  double _w1 = baseSize.width * 2.w - 100.w;
                                  if (offset < 0) {
                                    offset = 0;
                                  } else if ((offset > _w1)) {
                                    offset = _w1;
                                  }
                                  setState(() {
                                    sliderXMoved = offset;
                                  });
                                  //滑动过程,改变滑块左边框颜色
                                  updateSliderColorIcon();
                                },
                                onPanEnd: (endDetails) {
                                  if (!enable) return;
                                  checkCaptcha(sliderXMoved, captchaToken);
                                  int _nowTime =
                                      new DateTime.now().millisecondsSinceEpoch;
                                  _checkMilliseconds =
                                      _nowTime - _checkMilliseconds;

                                  //滑动结束
                                },
                                child: Container(
                                  width: slideSize.width * (dialogWidth - 20) / baseSize.width,
                                  height: slideSize.width * (dialogWidth - 20) / baseSize.width,
                                  margin: EdgeInsets.fromLTRB(
                                      sliderXMoved > 0 ? sliderXMoved : 1,
                                      0,
                                      0,
                                      0),
                                  decoration: BoxDecoration(
                                    border: Border(
                                      top: BorderSide(
                                        width: 1,
                                        color: Color(0xffe5e5e5),
                                      ),
                                      right: BorderSide(
                                        width: 1,
                                        color: Color(0xffe5e5e5),
                                      ),
                                      bottom: BorderSide(
                                        width: 1,
                                        color: Color(0xffe5e5e5),
                                      ),
                                    ),
                                    color: sliderColor,
                                  ),
                                  child: IconButton(
                                    icon: Icon(sliderIcon),
                                    iconSize: 20,
                                    color: Colors.black54,
                                  ),
                                ),
                              )
                            ],
                          ))
                          : Container(),
                    ],
                  ),
                ],
              ),
            ),
          )

        ),
      )
    );
  }
}

3.使用:

_sendPhoneCode(setBottomSheetState) {
    showDialog<Null>(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext context) {
        return CaptchaPage(
          onSuccess: (value) async {
            Response response = await _dio.post(LoginApi.SEND_MESSAGE_URL,
                data: {
                  'ic': '+$areaCode',
                  'phone': phoneController.text,
                  'captchaVerification': value
                });
            String dataStr = json.encode(response.data);
            Map<String, dynamic> dataMap = json.decode(dataStr);
            if (dataMap != null && dataMap['code'] == 200) {
              if (mounted) {
                setBottomSheetState(() {
                  isButtonEnable = false; //按钮状态标记
                });
              }

              timer = new Timer.periodic(Duration(seconds: 1), (Timer timer) {
                if (mounted) {
                  setBottomSheetState(() {
                    count--;
                    if (count == 0) {
                      timer.cancel(); //倒计时结束取消定时器
                      isButtonEnable = true; //按钮可点击
                      count = 60; //重置时间
                      buttonText = S.current.fasongyanzhengma; //重置按钮文本
                    } else {
                      buttonText = '${count}S'; //更新文本内容
                    }
                  });
                }
              });
              esLoadingToast(S.current.fasongchenggong);
            } else {
              esLoadingError(S.current.fasongshibai);
            }
          },
          onFail: () {
            // esLoadingError('人机校验失败');
          },
        );
      },
    );
  }

滑块拼图验证码文章来源地址https://www.toymoban.com/news/detail-624462.html

到了这里,关于【flutter滑动拼图验证码】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Flutter页面滑动回调处理解决方法

    TabBarView 是 Flutter 中的一个用于显示选项卡视图的小部件。它通常与 TabBar 一起使用,用于实现选项卡式导航,允许用户在不同的选项卡之间切换内容。 以下是 TabBarView 的详细介绍: 基本用法: TabBarView 允许你在不同的选项卡之间切换内容。它接受一个 controller 属性,用于控

    2024年02月08日
    浏览(37)
  • 滑动验证码-elementui实现

    使用elementui框架实现 html代码 主要代码: 事件方法 相关变量 使用示例: 效果图

    2024年02月11日
    浏览(32)
  • python 使用ddddocr库实现滑块验证码滑动验证

    使用ddddocr识别 该算法识别准确率为95%左右,测试三轮,每轮测试100次 使用cv2识别 该算法识别准确率为95%左右,测试三轮,每轮测试100次 构造轨迹库 图片长度为300,理论上就300种轨迹,实际上应该是200+种,还要减去滑块图的长度80 手动滑他个几百次,并把距离和轨迹记录下

    2024年01月21日
    浏览(56)
  • 【验证码系列】用逆向思维深度分析滑动验证码(含轨迹算法)

      验证码是机器人防护(即爬虫)常用重要手段之一!在爬虫这个领域内专精某一项( 验证码识别 、 JS或者APP逆向 )都是可以成为大牛存在的 传统的验证码我们都知道是静态图片的形式,它们包含一些阿拉伯数字跟字母加一些干扰( 现在而言的话难度系数极低 )。目前

    2024年02月06日
    浏览(44)
  • Flutter 小技巧之滑动控件即将“抛弃” shrinkWrap 属性

    相信对于 Flutter 开发的大家来说, ListView 的 shrinkWrap 配置都不会陌生,如下图所示,每当遇到类似的 unbounded error 的时候,总会有第一反应就是给 ListView 加上 shrinkWrap: true 就可以解决问题,那为什么现在会说 shrinkWrap 即将被“抛弃”呢? 其实说完全“抛弃”也不大严谨,从

    2024年02月16日
    浏览(50)
  • flutter在windows系统上实现左右水平滑动问题

    在个问题在github也有记录:https://github.com/flutter/flutter/issues/105095 就是flutter使用listview等可以滑动的组件来左右滚动的时候,不能正常工作,也就是无效,所以下面大家开始讨论这个问题。 翻阅大家讨论的内容,发现有的windows11是可以正常工作的,但是需要加上一个滚动条和

    2024年01月21日
    浏览(33)
  • 用opencv+playwright过滑动验证码

    目录 梳理思路 编写代码 总结与提高 在本节,我们将使用opencv和playwright这两个库通过QQ空间的滑动验证码。  1. 使用playwright打开浏览器,访问qq空间登录页面。 2. 点击密码登录。   3. 输入账号密码并点击登录。 4. 出现滑动验证码图片后,我们就可以获取到验证码背景图以及

    2024年02月16日
    浏览(34)
  • Android Studio实现滑动图片验证码

    源代码链接 效果: MainActivity SlideImageView activity_main.xml

    2024年02月13日
    浏览(45)
  • selenium利用图鉴破滑动验证码,登录信息门户

    验证码类型: 总结: 1.学习到了selenium中的动作链模块。 首先 from selenium.webdriver.common.action_chains import ActionChains导入包 使用方法: 首先启动动作链:action = webdriver.ActionChains(browser) action.click_and_hold(定位滑块的位置).perform() # 按住滑块 action.move_by_offset(x坐标,y坐标)  # 移动滑块

    2024年01月23日
    浏览(47)
  • selenium爬虫|破解滑动验证码以极验为例

      爬虫访问一些网站遇到滑动验证码解决方案 这里是用selenium做模拟,如果是requests可以封装这个登录方法来获取登录后的cookies也是可以用的。 1 先讲思路,分析流程  我们输入账号密码后点击登录 ,出现的是第一张图的状态。我们要做的是模拟滑动到缺口处。 首先我们要

    2024年02月14日
    浏览(53)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包