Flutter动画文本插件animated_text的使用
Flutter动画文本插件animated_text的使用
Animated_Text
Animated_Text 帮助你通过复用相似字母来实现文字之间的动画效果。
安装
1. 添加依赖
在项目的 pubspec.yaml
文件中添加以下依赖:
dependencies:
animated_text: ^1.0.2
2. 获取依赖
可以通过命令行安装依赖:
使用 pub
:
$ pub get
使用 Flutter
:
$ flutter packages get
3. 导入库
在 Dart 文件中导入 animated_text
库:
import 'package:animated_text/animated_text.dart';
使用方法
AnimatedText
/** 你的父级小部件 */
child: AnimatedText(
alignment: Alignment.center, // 设置对齐方式
speed: Duration(milliseconds: 1000), // 动画速度
controller: AnimatedTextController.loop, // 控制器,默认为播放状态
displayTime: Duration(milliseconds: 1000), // 文本显示时间
wordList: ['animations.', 'are.', 'easier.', 'now.'], // 动画的单词列表
textStyle: TextStyle( // 设置文本样式
color: Colors.black,
fontSize: 55,
fontWeight: FontWeight.w700,
),
onAnimate: (index) { // 每次动画开始时调用
print("Animating index:" + index.toString());
},
onFinished: () { // 动画完成时调用
print("Animation finished");
},
),
Widget Options
key | description |
---|---|
controller | 控制动画状态,枚举类型 <AnimatedTextController {play, pause, stop, restart, loop}> ,默认值为 AnimatedTextController.play |
onAnimate | 动画开始时调用,传递当前动画索引 |
onFinished | 动画结束时调用,仅当非循环动画时触发 |
repeatCount | 动画重复次数,默认值为 5 |
textStyle | 如果未设置,则使用上下文的默认文本样式 |
displayTime | 动画之间文本显示的时间 |
示例代码
以下是完整的示例代码:
import 'package:animated_text/animated_text.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool play = true;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Animated Text'),
),
body: Container(
color: Colors.white,
child: Column(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: 300,
child: AnimatedText(
alignment: Alignment.center, // 文字居中对齐
speed: Duration(milliseconds: 1000), // 动画速度
controller: AnimatedTextController.loop, // 循环动画
displayTime: Duration(milliseconds: 1000), // 显示时间
wordList: ['animations.', 'are.', 'easier.', 'now.'], // 动画的单词列表
textStyle: TextStyle( // 设置文本样式
color: Colors.black,
fontSize: 55,
fontWeight: FontWeight.w700,
),
onAnimate: (index) { // 动画开始时打印索引
print("Animating index:" + index.toString());
},
onFinished: () { // 动画完成时打印消息
print("Animation finished");
},
),
),
],
),
),
);
}
}
1 回复
更多关于Flutter动画文本插件animated_text的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
animated_text
是一个用于在 Flutter 应用中创建各种文本动画效果的插件。它提供了多种动画类型,如逐字显示、淡入淡出、打字机效果等。以下是如何使用 animated_text
插件的详细步骤。
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 animated_text
插件的依赖:
dependencies:
flutter:
sdk: flutter
animated_text: ^1.0.0
然后运行 flutter pub get
来获取依赖。
2. 导入包
在你的 Dart 文件中导入 animated_text
包:
import 'package:animated_text/animated_text.dart';
3. 使用 AnimatedText
组件
AnimatedText
组件是 animated_text
插件的核心组件。你可以通过设置不同的参数来创建各种文本动画效果。
基本用法
以下是一个简单的例子,展示如何使用 AnimatedText
组件:
import 'package:flutter/material.dart';
import 'package:animated_text/animated_text.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Animated Text Example'),
),
body: Center(
child: AnimatedText(
text: 'Hello, Flutter!',
textStyle: TextStyle(fontSize: 32.0, fontWeight: FontWeight.bold),
duration: Duration(seconds: 2),
animationType: AnimatedTextType.typer,
),
),
),
);
}
}
参数说明
text
: 要显示的文本。textStyle
: 文本的样式。duration
: 动画的持续时间。animationType
: 动画类型,如AnimatedTextType.typer
(打字机效果)、AnimatedTextType.fade
(淡入淡出效果)等。
其他动画类型
animated_text
插件支持多种动画类型,以下是一些常见的动画类型:
AnimatedTextType.typer
: 打字机效果,逐字显示文本。AnimatedTextType.fade
: 淡入淡出效果,文本逐渐显示和消失。AnimatedTextType.scale
: 缩放效果,文本从小变大或从大变小。AnimatedTextType.rotate
: 旋转效果,文本旋转显示。
你可以通过设置 animationType
参数来选择不同的动画类型。
4. 自定义动画
你还可以通过 AnimatedText
的 controller
参数来控制动画的播放、暂停、停止等操作。
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _controller = AnimatedTextController();
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Animated Text Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AnimatedText(
text: 'Hello, Flutter!',
textStyle: TextStyle(fontSize: 32.0, fontWeight: FontWeight.bold),
duration: Duration(seconds: 2),
animationType: AnimatedTextType.typer,
controller: _controller,
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
_controller.play();
},
child: Text('Play'),
),
ElevatedButton(
onPressed: () {
_controller.pause();
},
child: Text('Pause'),
),
ElevatedButton(
onPressed: () {
_controller.stop();
},
child: Text('Stop'),
),
],
),
),
),
);
}
}