Flutter动画效果插件animated_appear的使用
Flutter动画效果插件animated_appear的使用
动画效果插件介绍
animated_appear
是一个用于在Flutter中实现带有动画效果的出现组件的插件。它受到了Whatssub网站的启发,并可以在浏览器中运行示例代码。
使用方法
animated_appear
插件提供了以下选项:
child
: 需要添加动画效果的子组件。delay
: 动画延迟时间。duration
: 动画持续时间。curve
: 动画曲线。slideBeginOffset
: SlideTransition的开始偏移量。slideEndOffset
: SlideTransition的结束偏移量。
安装插件
dependencies:
animated_appear: ^x.x.x
示例代码
下面是一个完整的示例代码,展示了如何使用animated_appear
插件创建带有动画效果的聊天消息组件。
import 'package:animated_appear/animated_appear.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final Color primaryColor = Color(0xFF0068FF);
final Color lightColor = Color(0xFFF0F0F5);
final double fontSize = 11;
final BorderRadius chatMessageBorderRadius = BorderRadius.circular(12);
final Border chatDefaultBorder =
Border.all(color: Colors.grey, width: 0.8, style: BorderStyle.solid);
final int baseDelayMilliseconds = 2000;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: CustomScrollView(
physics: BouncingScrollPhysics(),
slivers: [
SliverToBoxAdapter(
child: AnimatedAppear(
delay: Duration(milliseconds: baseDelayMilliseconds ~/ 2),
duration: Duration(milliseconds: 400),
child: AppHeader(),
),
),
SliverList(
delegate: SliverChildListDelegate.fixed([
AnimatedAppear(
delay: Duration(milliseconds: baseDelayMilliseconds),
duration: Duration(milliseconds: 400),
child: ChatMessage(
text: "[WEB 发信] 区域服务已更新",
border: chatDefaultBorder,
borderRadius: chatMessageBorderRadius,
),
),
AnimatedAppear(
delay: Duration(milliseconds: baseDelayMilliseconds + 1000),
duration: Duration(milliseconds: 250),
child: ChatMessage(
text: "??? 什么情况",
mainAxisAlignment: MainAxisAlignment.end,
backgroundColor: lightColor,
borderRadius: chatMessageBorderRadius,
),
),
AnimatedAppear(
delay: Duration(milliseconds: baseDelayMilliseconds + 1800),
duration: Duration(milliseconds: 250),
child: ChatMessage(
text: "哈... 对了...",
mainAxisAlignment: MainAxisAlignment.end,
backgroundColor: lightColor,
borderRadius: chatMessageBorderRadius,
),
),
AnimatedAppear(
delay: Duration(milliseconds: baseDelayMilliseconds + 2500),
duration: Duration(milliseconds: 250),
child: ChatMessage(
text: "这样的体验大家都有吧?",
border: chatDefaultBorder,
borderRadius: chatMessageBorderRadius,
),
),
AnimatedAppear(
delay: Duration(milliseconds: baseDelayMilliseconds + 3500),
duration: Duration(milliseconds: 250),
child: ChatMessage(
text: "🤗 我们,在Watsap轻松接收通知并取消订阅",
textColor: primaryColor,
backgroundColor: primaryColor.withOpacity(0.1),
borderRadius: chatMessageBorderRadius,
),
),
]),
),
SliverFillRemaining(
fillOverscroll: false,
hasScrollBody: false,
child: AnimatedAppear(
delay: Duration(milliseconds: baseDelayMilliseconds + 5000),
duration: Duration(milliseconds: 250),
slideBeginOffset: const Offset(0, 0),
child: Container(
padding: const EdgeInsets.symmetric(vertical: 16),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
RichText(
textAlign: TextAlign.center,
text: TextSpan(
children: [
TextSpan(
text: '\'同意并开始\'按此按钮即表示您接受 ',
style: TextStyle(color: Colors.grey, fontSize: 12),
),
TextSpan(
text: 'Watsap 必需的服务条款',
style: TextStyle(
color: Colors.grey,
decoration: TextDecoration.underline,
fontSize: 12),
),
TextSpan(
text: '及\n',
style: TextStyle(color: Colors.grey, fontSize: 12),
),
TextSpan(
text: '个人信息使用政策',
style: TextStyle(
color: Colors.grey,
decoration: TextDecoration.underline,
fontSize: 12),
),
TextSpan(
text: '上所述内容。",
style: TextStyle(color: Colors.grey, fontSize: 12),
),
],
),
),
SizedBox(height: 16),
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: Size(0, 48),
elevation: 0,
primary: primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
onPressed: () {},
child: Text('同意并开始'),
),
),
],
),
),
),
)
],
),
);
}
}
class AppHeader extends StatelessWidget {
AppHeader({Key? key}) : super(key: key);
final Color primaryColor = Color(0xFF0068FF);
[@override](/user/override)
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(top: 40),
padding: const EdgeInsets.symmetric(vertical: 32, horizontal: 16),
child: RichText(
text: TextSpan(children: [
TextSpan(
text: '我',
style: TextStyle(
color: Colors.black,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
TextSpan(
text: '什么样的服务',
style: TextStyle(
color: primaryColor,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
TextSpan(
text: '\n',
style: TextStyle(
color: Colors.black,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
TextSpan(
text: '什么时候订阅',
style: TextStyle(
color: primaryColor,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
TextSpan(
text: '了呢?',
style: TextStyle(
color: Colors.black,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
]),
),
);
}
}
class ChatMessage extends StatelessWidget {
const ChatMessage({
Key? key,
required this.text,
this.textColor = Colors.black,
this.backgroundColor,
this.alignment,
this.mainAxisAlignment = MainAxisAlignment.start,
this.border,
this.borderRadius,
}) : super(key: key);
final String text;
final Color? textColor;
final Color? backgroundColor;
final Alignment? alignment;
final MainAxisAlignment mainAxisAlignment;
final Border? border;
final BorderRadius? borderRadius;
[@override](/user/override)
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: mainAxisAlignment,
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
margin: const EdgeInsets.only(bottom: 8),
decoration: BoxDecoration(
border: border,
borderRadius: borderRadius,
color: backgroundColor,
),
child: Text(
text,
style: TextStyle(color: textColor),
),
)
],
),
);
}
}
更多关于Flutter动画效果插件animated_appear的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter动画效果插件animated_appear的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,animated_appear
是一个用于 Flutter 的动画效果插件,它允许你在小部件(Widget)出现时添加动画效果。以下是一个简单的代码示例,展示了如何在 Flutter 应用中使用 animated_appear
插件。
首先,确保你已经在你的 pubspec.yaml
文件中添加了 animated_appear
依赖:
dependencies:
flutter:
sdk: flutter
animated_appear: ^x.y.z # 请替换为最新版本号
然后运行 flutter pub get
来获取依赖。
接下来,你可以在你的 Flutter 应用中使用 AnimatedAppear
小部件。以下是一个完整的示例:
import 'package:flutter/material.dart';
import 'package:animated_appear/animated_appear.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('AnimatedAppear Example'),
),
body: Center(
child: AnimatedAppearExample(),
),
),
);
}
}
class AnimatedAppearExample extends StatefulWidget {
@override
_AnimatedAppearExampleState createState() => _AnimatedAppearExampleState();
}
class _AnimatedAppearExampleState extends State<AnimatedAppearExample> with SingleTickerProviderStateMixin {
bool isVisible = false;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () {
setState(() {
isVisible = !isVisible;
});
},
child: Text('Toggle Visibility'),
),
SizedBox(height: 20),
AnimatedAppear(
visible: isVisible,
animation: SlideAnimation(
slideFrom: SlideDirection.bottom,
curve: Curves.easeInOutQuad,
duration: Duration(milliseconds: 500),
),
child: Container(
width: 200,
height: 200,
color: Colors.blue,
child: Center(
child: Text(
'Hello, AnimatedAppear!',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
),
),
],
);
}
}
在这个示例中:
- 我们定义了一个
AnimatedAppearExample
小部件,它包含一个按钮和一个AnimatedAppear
小部件。 AnimatedAppear
小部件的visible
属性控制其子小部件的可见性。- 当按钮被点击时,
setState
方法会被调用,isVisible
的值会切换,从而触发AnimatedAppear
小部件的显示或隐藏动画。 animation
属性用于定义动画效果,这里我们使用了SlideAnimation
,并且指定了动画从底部滑入、动画曲线、以及动画持续时间。
你可以根据需要调整动画类型(如 FadeAnimation
、ScaleAnimation
等)和动画参数,以实现不同的动画效果。