Flutter动画背景效果插件animated_hover_background的使用
Flutter动画背景效果插件animated_hover_background的使用
Hey Folks,
很高兴与大家分享我的第一个开源贡献!通过我们的动画小部件让您的仪表板更加生动。我们的动画背景由经验丰富的设计师和动画师创建,确保每个动画既视觉上令人印象深刻又技术上稳健。
特性
开始使用
在您的 pubspec.yaml
文件中添加依赖:
dependencies:
animated_hover_background: any
使用方法
在您的应用中使用 AnimatedHoverBackground
小部件来包裹需要动画背景的组件。例如:
AnimatedHoverBackground(YourWidget());
完整示例
以下是一个完整的示例代码,展示了如何在 Flutter 应用中使用 AnimatedHoverBackground
插件。
import 'dart:ui';
import 'package:animated_hover_background/animated_hover_background.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
// 自定义页面
class Home_Dash_Screen extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Center(
child: Text("主页"),
);
}
}
// 自定义日历页面
class CustomCalender extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Center(
child: Text("日历"),
);
}
}
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// 这个小部件是您的应用的根节点。
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomeScreen());
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
[@override](/user/override)
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String pageName = 'home';
[@override](/user/override)
Widget build(BuildContext context) {
return AnimatedHoverBackground(
Scaffold(
backgroundColor: Colors.transparent,
body: pageName == "home" ? Home_Dash_Screen() : CustomCalender(),
bottomNavigationBar: Stack(
children: [
Container(
height: 50,
width: MediaQuery.of(context).size.width,
margin: const EdgeInsets.only(bottom: 20, right: 12, left: 12),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.white24,
border: Border.all(color: Colors.grey.shade200)),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
GestureDetector(
onTap: () {
pageName = 'home';
setState(() {});
},
child: const Icon(Icons.dashboard_customize_outlined)),
GestureDetector(
onTap: () {
pageName = 'calender';
setState(() {});
},
child: const Icon(Icons.calendar_month)),
],
),
),
],
),
),
);
}
}
更多关于Flutter动画背景效果插件animated_hover_background的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter动画背景效果插件animated_hover_background的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter中使用animated_hover_background
插件来实现动画背景效果的代码示例。这个插件允许你创建一个当用户悬停(或触摸)在组件上时,背景会有动画效果的组件。
首先,确保你已经在pubspec.yaml
文件中添加了animated_hover_background
依赖:
dependencies:
flutter:
sdk: flutter
animated_hover_background: ^x.y.z # 替换为最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,在你的Dart文件中使用AnimatedHoverBackground
组件。以下是一个完整的示例:
import 'package:flutter/material.dart';
import 'package:animated_hover_background/animated_hover_background.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Animated Hover Background Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Animated Hover Background Demo'),
),
body: Center(
child: AnimatedHoverBackground(
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(15),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3), // changes position of shadow
),
],
),
child: Center(
child: Text(
'Hover Over Me',
style: TextStyle(fontSize: 24, color: Colors.black),
),
),
),
curve: Curves.easeInOutQuad,
duration: Duration(milliseconds: 500),
hoverColor: Colors.blue.withOpacity(0.4),
hoverScale: 1.05,
padding: EdgeInsets.all(10),
),
),
),
);
}
}
在这个示例中,我们创建了一个AnimatedHoverBackground
组件,它包裹了一个简单的Container
。Container
有一个白色的背景,一些阴影,以及居中的文本。AnimatedHoverBackground
组件的属性配置如下:
curve
: 动画的曲线,这里使用的是Curves.easeInOutQuad
。duration
: 动画的持续时间,这里是500毫秒。hoverColor
: 当用户悬停在组件上时,背景会变为这个颜色(这里是半透明的蓝色)。hoverScale
: 当用户悬停在组件上时,组件会放大到这个比例(这里是原大小的1.05倍)。padding
: 动画效果作用的内边距。
你可以根据需要调整这些属性来实现不同的动画效果。希望这个示例能帮助你理解如何使用animated_hover_background
插件!