关于flutter:在-Flutter-中实现文字动画

微信群 ducafecat

b 站 https://space.bilibili.com/40…

原文

https://medium.com/flutterdev…

参考

  • https://pub.dev/packages/anim…

注释

动画冀望在更新您的应用程序的整体客户机体验从视觉剖析,静止,和自定义动画的微小的一部分,你真的能够设想!.就像协调到应用程序中的一些不同的货色一样,动画应该是有帮忙的,而不是基本上是一个失常的简单格局。

在 Flutter,动画是间接做到的,而且很多乖僻的货色能够用比原生 Android 更少的致力来欠缺。

在本帖中,咱们将摸索 Flutter 文本动画。咱们还将实现一个演示程序的文本动画,并显示一个沉着和漂亮的文本动画收集应用的动画工具包在您的 Flutter 应用程序。

https://pub.dev/packages/anim…

简介

一个 Flutter 小工具包,蕴含一些很酷的和平凡的内容动画分类。咱们将制作不凡的和优良的内容动画利用动画 animated_text_kit 工具包包。

属性

以下是 AnimatedTextKit 的一些属性:

  • animatedTexts: 动画文本: 此属性用于列出[ AnimatedText ] ,以便随后在动画中显示
  • isRepeatingAnimation: 反复动画: 此属性用于设置动画是否应该通过将其值更改为 false 来反复。默认状况下,它被设置为 true
  • totalRepeatCount: 累计反复计数: 此属性用于设置动画应反复的次数。默认状况下,设置为 3
  • repeatForever: 此属性用于设置动画是否会永远反复。如果你想永远反复,还须要将动画设置为 true
  • onFinished: 此属性用于将 onFinished [ VoidCallback ]增加到动画小部件。只有当[ isrepetinganimation ]设置为 false 时,此办法才会运行
  • onTap: 此属性用于将 onTap [ VoidCallback ]增加到动画小部件
  • stopPauseOnTap: 此属性用于暂停,是否须要点击删除残余的暂停工夫?.默认状况下,它被设置为 false

装置

  • 第一步: 增加依赖项

将依赖项增加到 pubspec.yaml 文件。

animated_text_kit: ^4.2.1
  • 第二步: 导入
import 'package:animated_text_kit/animated_text_kit.dart';
  • 第三步: 在应用程序的根目录中运行 flutter 软件包。
flutter packages get

如何实现 dart 文件中的代码

你须要别离在你的代码中实现它:

在 lib 文件夹中创立一个名为 home_page_screen.dart 的新 dart 文件。

咱们将在主页屏幕上创立九个不同的按钮,当用户点击按钮时,动画将工作。所有按钮都有不同的动画成果。咱们将在上面深刻探讨这个问题。当咱们运行应用程序时,咱们应该失去屏幕的输入,就像上面的屏幕截图一样。

旋转动画文字

在注释中,咱们将增加一个列小部件。在这个小部件中,增加一个具备高度和宽度的 Container。其子属性,增加一个 _rotate() 小部件。

Center(
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.center,
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      Container(
        decoration: BoxDecoration(color: Colors.red),
        height: 300.0,
        width: 350.0,
        child: Center(
          child: _rotate(),
        ),
      ),
    ],
  ),
)

_rotate() 小部件中。咱们将返回 Row 小部件。在外部,增加文本和 defaultextstyle()。它是子属性,咱们将增加 AnimatedTextKit()小部件。在外面,咱们将增加 repeatForever 是实在的,isRepeatingAnimation 也是实在的,并增加 animatedtext。在 animatedtext 中,咱们将增加三个 RotateAnimatedText()。用户还能够增加持续时间,旋转。

Widget _rotate(){
  return Row(
    mainAxisSize: MainAxisSize.min,
    children: <Widget>[
      const SizedBox(width: 10.0, height: 100.0),
      const Text(
        'Flutter',
        style: TextStyle(fontSize: 40.0),
      ),
      const SizedBox(width: 15.0, height: 100.0),
      DefaultTextStyle(
        style: const TextStyle(
          fontSize: 35.0,
        ),
        child: AnimatedTextKit(
            repeatForever: true,
            isRepeatingAnimation: true,
            animatedTexts: [
          RotateAnimatedText('AWESOME'),
          RotateAnimatedText('Text'),
          RotateAnimatedText('Animation'),
        ]),
      ),
    ],
  );
}

当咱们运行应用程序,咱们应该失去屏幕的输入像上面的屏幕视频。

打字机动画文字

在注释中,咱们将增加与下面雷同的办法。然而在子属性中的更改,增加一个 _typer 小部件。

Widget _typer(){
  return SizedBox(
    width: 250.0,
    child: DefaultTextStyle(
      style: const TextStyle(
        fontSize: 30.0,
        fontFamily: 'popin',
      ),
      child: AnimatedTextKit(
        isRepeatingAnimation: true,
          animatedTexts: [
            TyperAnimatedText('When you talk, you are only repeating'
                ,speed: Duration(milliseconds: 100)),
            TyperAnimatedText('something you know.But if you listen,'
                ,speed: Duration(milliseconds: 100)),
            TyperAnimatedText(' you may learn something new.'
                ,speed: Duration(milliseconds: 100)),
            TyperAnimatedText('– Dalai Lama'
                ,speed: Duration(milliseconds: 100)),
          ]
    ),
  ),
  );
}

在这个小部件中,咱们将返回 SizedBox()。在外部,咱们将增加 DefaultTextStyle()并增加 AnimatedTextKit()小部件。在这个小部件中,咱们将增加 animatedtext。在外部,咱们将增加四个带有速度持续时间的 TyperAnimatedText()。当咱们运行应用程序,咱们应该失去屏幕的输入像上面的屏幕视频。

淡出动画文本

在注释中,咱们将增加与下面雷同的办法。然而如果扭转子属性,增加一个 _fade 小部件。

Widget _fade(){
  return SizedBox(
    child: DefaultTextStyle(
      style: const TextStyle(
        fontSize: 32.0,
        fontWeight: FontWeight.bold,
      ),
      child: Center(
        child: AnimatedTextKit(
          repeatForever: true,
          animatedTexts: [
            FadeAnimatedText('THE HARDER!!',
                duration: Duration(seconds: 3),fadeOutBegin: 0.9,fadeInEnd: 0.7),
            FadeAnimatedText('YOU WORK!!',
                duration: Duration(seconds: 3),fadeOutBegin: 0.9,fadeInEnd: 0.7),
            FadeAnimatedText('THE LUCKIER!!!',
                duration: Duration(seconds: 3),fadeOutBegin: 0.9,fadeInEnd: 0.7),
            FadeAnimatedText('YOU GET!!!!',
                duration: Duration(seconds: 3),fadeOutBegin: 0.9,fadeInEnd: 0.7),
          ],
        ),
      ),
    ),
  );

}

在这个小部件中,咱们将返回 SizedBox()。在外部,咱们将增加 DefaultTextStyle()并增加 AnimatedTextKit()小部件。在这个小部件中,咱们将增加 animatedtext。在外部,咱们将增加 4 个 FadeAnimatedText() ,其中包含速度持续时间、 fadeOutBegin 和 fadeInEnd。比 fadeInEnd 要好。当咱们运行应用程序,咱们应该失去屏幕的输入像上面的屏幕视频。

缩放动画文字

在注释中,咱们将增加与下面雷同的办法。然而在子属性中的更改,增加一个 _scale 小部件。

Widget _scale(){
  return SizedBox(
    child: DefaultTextStyle(
      style: const TextStyle(
        fontSize: 50.0,
        fontFamily: 'SF',
      ),
      child: Center(
        child: AnimatedTextKit(
          repeatForever: true,
          animatedTexts: [
            ScaleAnimatedText('Eat',scalingFactor: 0.2),
            ScaleAnimatedText('Code',scalingFactor: 0.2),
            ScaleAnimatedText('Sleep',scalingFactor: 0.2),
            ScaleAnimatedText('Repeat',scalingFactor: 0.2),
          ],
        ),
      ),
    ),
  );
}

在这个小部件中,咱们将返回 SizedBox()。在外部,咱们将增加 DefaultTextStyle()并增加 AnimatedTextKit()小部件。在这个小部件中,咱们将增加 animatedtext。在外部,咱们将增加四个带有 scalingFactor 的 ScaleAnimatedText()。scalingFactor 设置了动画文本的缩放因子。当咱们运行应用程序,咱们应该失去屏幕的输入像上面的屏幕视频。

TextLiquidFill 动画

在注释中,咱们将增加与下面雷同的办法。然而如果更改子属性,则增加一个 _textLiquidFillAnimation 小部件。

Widget _textLiquidFillAnimation(){
  return SizedBox(
    child: Center(
      child: TextLiquidFill(
        text: 'Flutter Devs',
        waveDuration: Duration(seconds: 5),
        waveColor: Colors.blue,
        boxBackgroundColor: Colors.green,
        textStyle: TextStyle(
          fontSize: 50.0,
          fontWeight: FontWeight.bold,
        ),
      ),
    ),
  );
}

在这个小部件中,咱们将返回 SizedBox()。在外部,咱们将增加 TextLiquidFill()小部件。在这个小部件中,咱们将增加文本、 waveDuration、 waveColor 和 boxBackgroundColor。当咱们运行应用程序,咱们应该失去屏幕的输入像上面的屏幕视频。

动画文字

在注释中,咱们将增加与下面雷同的办法。然而如果扭转子属性,增加一个 _wave 小部件。

Widget _wavy(){
  return DefaultTextStyle(
    style: const TextStyle(
      fontSize: 25.0,
    ),
    child: AnimatedTextKit(
      animatedTexts: [
        WavyAnimatedText("Flutter is Google's UI toolkit,",
            speed: Duration(milliseconds: 200)),
        WavyAnimatedText('for building beautiful Apps',
            speed: Duration(milliseconds: 200)),
      ],
      isRepeatingAnimation: true,
      repeatForever: true,
    ),
  );
}

在这个小部件中,咱们将返回 DefaultTextStyle()。在外部,咱们将增加 AnimatedTextKit()小部件。在这个小部件中,咱们将增加 animatedtext。在外部,咱们将增加两个 WavyAnimatedText()和文本的速度持续时间。当咱们运行应用程序,咱们应该失去屏幕的输入像上面的屏幕视频。

闪动动画文字

在注释中,咱们将增加与下面雷同的办法。然而在子属性中的更改,增加一个 _flicker 小部件。

Widget _flicker(){
  return SizedBox(
    width: 250.0,
    child: DefaultTextStyle(
      style: const TextStyle(
        fontSize: 30,
      ),
      child: AnimatedTextKit(
        repeatForever: true,
        animatedTexts: [
          FlickerAnimatedText('FlutterDevs specializes in creating,',
              speed: Duration(milliseconds: 1000),entryEnd: 0.7),
          FlickerAnimatedText('cost-effective and',
              speed: Duration(milliseconds: 1000),entryEnd: 0.7),
          FlickerAnimatedText("efficient applications!",
              speed: Duration(milliseconds: 1000),entryEnd: 0.7),
        ],
      ),
    ),
  );
}

在这个小部件中,咱们将返回 SizedBox()。在外部,咱们将增加 DefaultTextStyle()并增加 AnimatedTextKit()小部件。在这个小部件中,咱们将增加 animatedtext。在外部,咱们将增加四个具备 entryEnd 和速度的 FlickerAnimatedText()。entryEnd 被标记为文本闪动输出距离的完结。当咱们运行应用程序,咱们应该失去屏幕的输入像上面的屏幕视频。

彩色动画文本

在注释中,咱们将增加与下面雷同的办法。然而在子属性中的更改,增加一个 _colorize 的小部件。

Widget _colorize(){
  return SizedBox(
    child: Center(
      child: AnimatedTextKit(
        animatedTexts: [
          ColorizeAnimatedText(
            'Mobile Developer',
            textStyle: colorizeTextStyle,
            colors: colorizeColors,
          ),
          ColorizeAnimatedText(
            'Software Testing',
            textStyle: colorizeTextStyle,
            colors: colorizeColors,
          ),
          ColorizeAnimatedText(
            'Software Engineer',
            textStyle: colorizeTextStyle,
            colors: colorizeColors,
          ),
        ],
        isRepeatingAnimation: true,
        repeatForever: true,
      ),
    ),
  );
}

在这个小部件中,咱们将返回 SizedBox()。在外部,咱们将增加 AnimatedTextKit()小部件。在这个小部件中,咱们将增加 animatedtext。在外部,咱们将增加三个带有 textStyle 和色彩的 colorizeanmatedtext()。

List<MaterialColor> colorizeColors = [
  Colors.red,
  Colors.yellow,
  Colors.purple,
  Colors.blue,
];

static const colorizeTextStyle = TextStyle(
  fontSize: 40.0,
  fontFamily: 'SF',
);

用户能够依据文本扭转色彩。当咱们运行应用程序,咱们应该失去屏幕的输入像上面的屏幕视频

打字机动画文字

在注释中,咱们将增加与下面雷同的办法。然而如果更改子属性,则增加 _typeWriter 小部件。

Widget _typeWriter(){
  return SizedBox(
    child: DefaultTextStyle(
      style: const TextStyle(
        fontSize: 30.0,
      ),
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Center(
          child: AnimatedTextKit(
            repeatForever: true,
            animatedTexts: [
              TypewriterAnimatedText('FlutterDevs specializes in creating cost-effective',
                  curve: Curves.easeIn,speed: Duration(milliseconds: 80)),
              TypewriterAnimatedText('and efficient applications with our perfectly crafted,',
                  curve: Curves.easeIn,speed: Duration(milliseconds: 80)),
              TypewriterAnimatedText('creative and leading-edge flutter app development solutions',
                  curve: Curves.easeIn,speed: Duration(milliseconds: 80)),
              TypewriterAnimatedText('for customers all around the globe.',
                  curve: Curves.easeIn,speed: Duration(milliseconds: 80)),
            ],
          ),
        ),
      ),
    ),
  );
}

在这个小部件中,咱们将返回 SizedBox()。在外部,咱们将增加 DefaultTextStyle()并增加 AnimatedTextKit()小部件。在这个小部件中,咱们将增加 animatedtext。外部,咱们将增加四个打字机动画文本()与曲线和速度。当咱们运行应用程序,咱们应该失去屏幕的输入像上面的屏幕视频。

所有代码

import 'package:flutter/material.dart';
import 'package:flutter_animation_text/colorize_animation_text.dart';
import 'package:flutter_animation_text/fade_animation_text.dart';
import 'package:flutter_animation_text/flicker_animation_text.dart';
import 'package:flutter_animation_text/rotate_animation_text.dart';
import 'package:flutter_animation_text/scale_animation_text.dart';
import 'package:flutter_animation_text/text_liquid_fill_animation.dart';
import 'package:flutter_animation_text/typer_animation_text.dart';
import 'package:flutter_animation_text/typewriter_animated_text.dart';
import 'package:flutter_animation_text/wavy_animation_text.dart';


class HomePageScreen extends StatefulWidget {
  @override
  _HomePageScreenState createState() => _HomePageScreenState();
}

class _HomePageScreenState extends State<HomePageScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xffFFFFFF),
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: Text('Flutter Animations Text Demo'),
        automaticallyImplyLeading: false,
        centerTitle: true,
      ),
      body: Center(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[

                // ignore: deprecated_member_use
                RaisedButton(
                  child: Text('Rotate Animation Text',style: TextStyle(color: Colors.black),),
                  color: Colors.tealAccent,
                  onPressed:() {
                    Navigator.of(context).push(
                        MaterialPageRoute(builder:(context) => RotateAnimationText()));
                  },
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
                  padding: EdgeInsets.all(13),
                ),
                SizedBox(height: 8,),
                // ignore: deprecated_member_use
                RaisedButton(
                  child: Text('Typer Animation Text',style: TextStyle(color: Colors.black),),
                  color: Colors.tealAccent,
                  onPressed:() {
                    Navigator.of(context).push(
                        MaterialPageRoute(builder:(context) => TyperAnimationText()));
                  },
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
                  padding: EdgeInsets.all(13),

                ),
                SizedBox(height: 8,),
                // ignore: deprecated_member_use
                RaisedButton(
                  child: Text('Fade Animation Text',style: TextStyle(color: Colors.black),),
                  color: Colors.tealAccent,
                  onPressed:() {
                    Navigator.of(context).push(
                        MaterialPageRoute(builder:(context) => FadeAnimationText()));
                  },
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
                  padding: EdgeInsets.all(13),

                ),
                SizedBox(height: 8,),
                // ignore: deprecated_member_use
                RaisedButton(
                  child: Text('Scale Animation Text',style: TextStyle(color: Colors.black),),
                  color: Colors.tealAccent,
                  onPressed:() {
                    Navigator.of(context).push(
                        MaterialPageRoute(builder:(context) => ScaleAnimationText()));
                  },
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
                  padding: EdgeInsets.all(13),

                ),
                SizedBox(height: 8,),
                // ignore: deprecated_member_use
                RaisedButton(
                  child: Text('TextLiquidFill Animation',style: TextStyle(color: Colors.black),),
                  color: Colors.tealAccent,
                  onPressed:() {
                    Navigator.of(context).push(
                        MaterialPageRoute(builder:(context) => TextLiquidFillAnimation()));
                  },
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
                  padding: EdgeInsets.all(13),

                ),
                SizedBox(height: 8,),
                // ignore: deprecated_member_use
                RaisedButton(
                  child: Text('Wavy Animation Text',style: TextStyle(color: Colors.black),),
                  color: Colors.tealAccent,
                  onPressed:() {
                    Navigator.of(context).push(
                        MaterialPageRoute(builder:(context) => WavyAnimationText()));
                  },
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
                  padding: EdgeInsets.all(13),

                ),
                SizedBox(height: 8,),
                // ignore: deprecated_member_use
                RaisedButton(
                  child: Text('Flicker Animation Text',style: TextStyle(color: Colors.black),),
                  color: Colors.tealAccent,
                  onPressed:() {
                    Navigator.of(context).push(
                        MaterialPageRoute(builder:(context) => FlickerAnimationText()));
                  },
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
                  padding: EdgeInsets.all(13),

                ),
                SizedBox(height: 8,),
                // ignore: deprecated_member_use
                RaisedButton(
                  child: Text('Colorize Animation Text',style: TextStyle(color: Colors.black),),
                  color: Colors.tealAccent,
                  onPressed:() {
                    Navigator.of(context).push(
                        MaterialPageRoute(builder:(context) => ColorizeAnimationText()));
                  },
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
                  padding: EdgeInsets.all(13),

                ),
                SizedBox(height: 8,),

                // ignore: deprecated_member_use
                RaisedButton(
                  child: Text('Typewriter Animation Text',style: TextStyle(color: Colors.black),),
                  color: Colors.tealAccent,
                  onPressed:() {
                    Navigator.of(context).push(
                        MaterialPageRoute(builder:(context) => TypewriterAnimationText()));
                  },
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
                  padding: EdgeInsets.all(13),

                ),

              ],
            ),
          )
      ), //center
    );
  }
}

总结

在这篇文章中,我曾经简略地解释了文本动画的根本构造,您能够依据本人的抉择批改这个代码。这是一个小的介绍文本动画用户交互从我这边,它的工作应用扑动。


© 猫哥

https://ducafecat.tech/

https://github.com/ducafecat

往期

开源

GetX Quick Start

https://github.com/ducafecat/…

新闻客户端

https://github.com/ducafecat/…

strapi 手册译文

https://getstrapi.cn

微信探讨群 ducafecat

系列汇合

译文

https://ducafecat.tech/catego…

开源我的项目

https://ducafecat.tech/catego…

Dart 编程语言根底

https://space.bilibili.com/40…

Flutter 零根底入门

https://space.bilibili.com/40…

Flutter 实战从零开始 新闻客户端

https://space.bilibili.com/40…

Flutter 组件开发

https://space.bilibili.com/40…

Flutter Bloc

https://space.bilibili.com/40…

Flutter Getx4

https://space.bilibili.com/40…

Docker Yapi

https://space.bilibili.com/40…

【腾讯云】轻量 2核2G4M,首年65元

阿里云限时活动-云数据库 RDS MySQL  1核2G配置 1.88/月 速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据