广告
返回顶部
首页 > 资讯 > 精选 >怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画
  • 407
分享到

怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画

2023-07-05 21:07:15 407人浏览 薄情痞子
摘要

这篇文章主要介绍“怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画”,在日常操作中,相信很多人在怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画问题

这篇文章主要介绍“怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画”,在日常操作中,相信很多人在怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    基本使用

    StrikeThroughText(    text: "1. Task Item StrikeThroughText",    textStyle: const TextStyle(        fontSize: 18,    ),    inactiveTextColor: Colors.red,    textColor: Colors.blue,    strikethrough: isCheck,    onChange: (value) {        setState(() {            isCheck = value;        });    },)

    实现

    1、布局

    首先完成 widget 的布局和样式,这里采用了 Stack 布局,首先添加文字和文字样式,在文字的中间放置一个横线作为中划线。 大致布局如下:

     Stack(      children: [        Text(          "Task Item",          maxLines: 1,          softWrap: false,          style: TextStyle(            fontSize: 18,          ),        ),        Positioned(          top: 0,          bottom: 0,          left: 0,          right: 0,          child: CustomPaint(              painter: StrikeThroughTextPainter(                  ...,              ),          ),        ),      ],    );

    2、绘制中划线

    绘制中划线,首先需要知道要绘制多长。这里可以使用 TextPainter 来测绘文字的宽高,这里写成一个通用的方法,传入 Text 的text和textStyle,返回文字的宽高:

    class TextSizeBox {  final double width;  final double height;  TextSizeBox({required this.width, required this.height});  factory TextSizeBox.fromText(String text, {TextStyle? textStyle}) {    final TextPainter textPainter = TextPainter(      text: TextSpan(text: text, style: textStyle),      maxLines: 1,      textDirection: TextDirection.ltr,    )..layout(minWidth: 0, maxWidth: double.infinity);    return TextSizeBox(width: textPainter.width, height: textPainter.height);  }}

    知道了文字的宽就等于知道绘制文字的中划线宽度了。

    StrikeThroughTextPainter(    width: TextSizeBox.fromText(widget.text,  textStyle: widget.textStyle).width,    height: 2.0,    color: Colors.grey,)
    class StrikeThroughTextPainter extends CustomPainter {  final double width;  final double height;  final Color color;  StrikeThroughTextPainter(      {required this.width, required this.height, required this.color});  @override  void paint(canvas canvas, Size size) {    final paint = Paint()      ..color = color      ..strokeWidth = height      ..strokeCap = StrokeCap.round;    if (width > 0) {      canvas.drawLine(          Offset(0, size.height / 2),          Offset(width > size.width ? size.width : width, size.height / 2),          paint);    }  }  @override  bool shouldRepaint(StrikeThroughTextPainter oldDelegate) {    return width != oldDelegate.width || height != oldDelegate.height;  }}

    3、动画

    首先是左右移动动画,先创建一个 AnimationController ,在创建一个Tween<Offset>来控制左右移动的偏移量

     _offsetController = AnimationController(      vsync: this,      duration: const Duration(milliseconds: 100),    );_offsetAnimation = Tween<Offset>(      begin: const Offset(0.0, 0.0),      end: const Offset(0.2, 0.0),    ).animate(CurvedAnimation(      parent: _offsetController,      curve: Curves.easeInOut,    ));

    使用 SlideTransition 来控制左右平移偏移量

    SlideTransition(position: _offsetAnimation,child: Stack(......),)

    因为颜色变化和划中划线是同步进行的,所以只需要创建一个AnimationController来控制颜色和进度的动画

    _animationController = AnimationController(      vsync: this,      duration: const Duration(milliseconds: 300),      value: 1,    );_animation = Tween(begin: 0.0, end: 1.0).animate(_animationController);_animationColor = ColorTween(            begin: Colors.black87,            end: Colors.grey)        .animate(_animationController);

    接下来就是在需要动画的 widget 上放上动画就可以了.

    完整代码

    import 'package:flutter/material.dart';class StrikeThroughText extends StatefulWidget {  final String text;  final TextStyle textStyle;  final bool strikethrough;  final Color? textColor;  final Color? inactiveTextColor;  final ValueChanged? onChange;  const StrikeThroughText({    Key? key,    required this.text,    required this.textStyle,    this.strikethrough = false,    this.textColor,    this.inactiveTextColor,    this.onChange,  }) : super(key: key);  @override  StrikeThroughTextState createState() => StrikeThroughTextState();}class StrikeThroughTextState extends State<StrikeThroughText>    with TickerProviderStateMixin {  late AnimationController _animationController;  late Animation<double> _animation;  late Animation _animationColor;  late AnimationController _offsetController;  late Animation<Offset> _offsetAnimation;  @override  void initState() {    super.initState();    _animationController = AnimationController(      vsync: this,      duration: const Duration(milliseconds: 300),      value: widget.strikethrough ? 1 : 0,    );    _animation = Tween(begin: 0.0, end: 1.0).animate(_animationController);    _animationColor = ColorTween(            begin: widget.textColor ?? Colors.black87,            end: widget.inactiveTextColor ?? Colors.grey)        .animate(_animationController);    _offsetController = AnimationController(      vsync: this,      duration: const Duration(milliseconds: 100),    );    _offsetAnimation = Tween<Offset>(      begin: const Offset(0.0, 0.0),      end: const Offset(0.2, 0.0),    ).animate(CurvedAnimation(      parent: _offsetController,      curve: Curves.easeInOut,    ));  }  @override  void didUpdateWidget(covariant StrikeThroughText oldWidget) {    super.didUpdateWidget(oldWidget);    if (oldWidget.strikethrough != widget.strikethrough) {      if (widget.strikethrough) {        startAnimation();      } else {        reset();      }    }  }  @override  void dispose() {    _animationController.dispose();    _offsetController.dispose();    super.dispose();  }  @override  Widget build(BuildContext context) {    return GestureDetector(      onTap: () {        if (widget.strikethrough) {          widget.onChange?.call(false);        } else {          widget.onChange?.call(true);        }      },      child: SlideTransition(        position: _offsetAnimation,        child: Stack(          children: [            AnimatedBuilder(                animation: _animationController,                builder: (context, child) {                  return Text(                    widget.text,                    maxLines: 1,                    softWrap: false,                    style: widget.textStyle.copyWith(                      color: _animationColor.value,                      overflow: TextOverflow.clip,                    ),                  );                }),            // AnimatedDefaultTextStyle(            //   style: widget.textStyle..copyWith(color: _animationColor.value),            //   duration: const Duration(milliseconds: 500),            //   child: Text(widget.text),            // ),            AnimatedBuilder(              animation: _animation,              builder: (context, child) {                return Positioned(                  left: 0,                  right: 0,                  top: 0,                  bottom: 0,                  child: CustomPaint(                    painter: StrikeThroughTextPainter(                      width: TextSizeBox.fromText(widget.text,                                  textStyle: widget.textStyle)                              .width *                          _animation.value,                      height: 2.0,                      color: widget.inactiveTextColor ?? Colors.grey,                    ),                  ),                );              },            ),          ],        ),      ),    );  }  void startAnimation() async {    _animationController.reset();    await _offsetController.forward();    await _offsetController.reverse();    _animationController.forward();  }  void reset() {    _animationController.reset();  }}class StrikeThroughTextPainter extends CustomPainter {  final double width;  final double height;  final Color color;  StrikeThroughTextPainter(      {required this.width, required this.height, required this.color});  @override  void paint(Canvas canvas, Size size) {    final paint = Paint()      ..color = color      ..strokeWidth = height      ..strokeCap = StrokeCap.round;    if (width > 0) {      canvas.drawLine(          Offset(0, size.height / 2),          Offset(width > size.width ? size.width : width, size.height / 2),          paint);    }  }  @override  bool shouldRepaint(StrikeThroughTextPainter oldDelegate) {    return width != oldDelegate.width || height != oldDelegate.height;  }}class TextSizeBox {  final double width;  final double height;  TextSizeBox({required this.width, required this.height});  factory TextSizeBox.fromText(String text, {TextStyle? textStyle}) {    final TextPainter textPainter = TextPainter(      text: TextSpan(text: text, style: textStyle),      maxLines: 1,      textDirection: TextDirection.ltr,    )..layout(minWidth: 0, maxWidth: double.infinity);    return TextSizeBox(width: textPainter.width, height: textPainter.height);  }}

    到此,关于“怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

    --结束END--

    本文标题: 怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画

    本文链接: https://www.lsjlt.com/news/355494.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    • 怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画
      这篇文章主要介绍“怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画”,在日常操作中,相信很多人在怎么使用Flutter StrikeThroughTextAnimation实现文字中划线动画问题...
      99+
      2023-07-05
    • Flutter 文字中划线动画StrikeThroughTextAnimation
      目录概述效果预览基本使用实现1、布局2、绘制中划线3、动画完整代码概述 接上文 CheckBoxAnimation 动画,在加上文字的动画,刚好可以做一个组合的列表动画。文字部分动...
      99+
      2023-05-14
      Flutter 文字中划线动画 Flutter StrikeThroughTextAnimation
    • div css怎么实现文字中划线
      这篇文章主要介绍了div css怎么实现文字中划线的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇div css怎么实现文字中划线文章都会有所收获,下面我们一起来看看吧。代码&l...
      99+
      2022-10-19
    • Flutter中怎么使用AnimatedOpacity实现图片渐现动画
      今天小编给大家分享一下Flutter中怎么使用AnimatedOpacity实现图片渐现动画的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起...
      99+
      2023-06-29
    • Flutter中怎么使用AnimatedSwitcher实现场景切换动画
      这篇文章主要介绍“Flutter中怎么使用AnimatedSwitcher实现场景切换动画”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Flutter中怎么使用AnimatedSwitcher实现场...
      99+
      2023-06-29
    • HTML5中怎么用Canvas实现文字动画特效
      本篇内容介绍了“HTML5中怎么用Canvas实现文字动画特效”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成...
      99+
      2022-10-19
    • Android中怎么利用Xfermode实现动态文字加载动画
      这篇文章将为大家详细讲解有关Android中怎么利用Xfermode实现动态文字加载动画,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。第一步:我们要熟悉一下这个图16个图形结果,其实现在有1...
      99+
      2023-05-30
      android
    • 怎么使用纯CSS代码实现文字断开的动画效果
      这篇文章将为大家详细讲解有关怎么使用纯CSS代码实现文字断开的动画效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。   代码解读   定义dom,只有一个元素,元素...
      99+
      2022-10-19
    • 使用css3怎么实现一个文字扫光渐变动画效果
      使用css3怎么实现一个文字扫光渐变动画效果?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。利用css3这个属性(背景剪裁):background-clip: b...
      99+
      2023-06-08
    • 怎么使用CSS实现图片帧动画与曲线运动
      这篇文章将为大家详细讲解有关怎么使用CSS实现图片帧动画与曲线运动,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。css是什么意思css是一种用来表现HTML或XML等文件样式的计算机语言,主要是用来设计网...
      99+
      2023-06-08
    • 怎么在Android中利用TextView实现一个数字滚动动画
      怎么在Android中利用TextView实现一个数字滚动动画?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。NumberRollingView是一个自定义的自带数字滚动动画的T...
      99+
      2023-05-31
      android textview 动动
    • 怎么使用SpeechSynthesis实现文字自动播报
      这篇文章主要介绍“怎么使用SpeechSynthesis实现文字自动播报”,在日常操作中,相信很多人在怎么使用SpeechSynthesis实现文字自动播报问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么使...
      99+
      2023-07-05
    • CSS和D3怎么实现用文字组成的心形动画效果
      小编给大家分享一下CSS和D3怎么实现用文字组成的心形动画效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!   代码解读 ...
      99+
      2022-10-19
    • 怎么使用css3实现一个类在线直播的队列动画
      这篇文章给大家分享的是有关怎么使用css3实现一个类在线直播的队列动画的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。之前在群里有个朋友问了这样一个问题, 就是如何在 小程序 中实现类似 直播平台 的用户上线时的 ...
      99+
      2023-06-08
    • 使用javascript怎么实现一个文字滚动特效
      这篇文章将为大家详细讲解有关使用javascript怎么实现一个文字滚动特效,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。JavaScript是什么JavaScript是一种直译式的脚本语言...
      99+
      2023-06-14
    • 怎么使用css实现文字循环滚动效果
      今天小编给大家分享一下怎么使用css实现文字循环滚动效果的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。首先创建一个html文...
      99+
      2023-07-04
    • Android中怎么使用ListView实现滚轮动画效果
      今天就跟大家聊聊有关Android中怎么使用ListView实现滚轮动画效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。   private ...
      99+
      2023-05-31
      android listview
    • 怎么在css3中使用less实现一个星空动画
      这期内容当中小编将会给大家带来有关怎么在css3中使用less实现一个星空动画,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。首先html文件结构很简单,如下:<div>  ...
      99+
      2023-06-08
    • 怎么在Html5页面中使用JSON实现一个动画
      今天就跟大家聊聊有关怎么在Html5页面中使用JSON实现一个动画,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1.demo.html里面有很多内联的东西,使用时堆积在页面内不好看仔...
      99+
      2023-06-09
    • 怎么在Html5中使用Canvas实现动画碰撞检测功能
      本篇文章为大家展示了怎么在Html5中使用Canvas实现动画碰撞检测功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。html有什么特点1、简易性:超级文本标记语言版本升级采用超集方式,从而更加灵...
      99+
      2023-06-09
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作