Flutter导航过渡动画插件hero_here的使用
Flutter导航过渡动画插件hero_here的使用
hero_here
是一个 Flutter 库,它使开发者能够在同一屏幕内实现英雄过渡动画,而无需进行导航。
目的
hero_here
设计用于帮助开发者在单个屏幕上创建无缝且视觉上吸引人的过渡效果。它借鉴了 Flutter 的 AnimatedSwitcher
和 Hero
机制,并提供了方便的方式来实现这些过渡效果,而无需进行导航。
特点
- 允许在同一屏幕内进行英雄过渡。
- 提供类似于
AnimatedSwitcher
和Hero
的小部件:HeroHereSwitcher
和HeroHere
。 - 可自定义飞行动画。
开始使用
在 pubspec.yaml
文件中添加以下依赖:
dependencies:
hero_here: ^1.0.0
然后运行:
flutter pub get
使用方法
这是一个基本示例,展示如何使用 hero_here
:
import 'package:flutter/material.dart';
import 'package:hero_here/hero_here.dart';
const kHeroTag = 'hero';
void main() => runApp(
MaterialApp(
title: 'HeroHere Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(useMaterial3: true, brightness: Brightness.light),
darkTheme: ThemeData(useMaterial3: true, brightness: Brightness.dark),
home: const HeroHereExample(),
),
);
enum HeroType { red, green }
class HeroHereExample extends StatefulWidget {
const HeroHereExample({super.key});
[@override](/user/override)
State<HeroHereExample> createState() => _HeroHereExampleState();
}
class _HeroHereExampleState extends State<HeroHereExample> {
HeroType _curHeroType = HeroType.red;
HeroType get curHeroType => _curHeroType;
set curHeroType(HeroType value) {
if (_curHeroType == value) return;
setState(() => _curHeroType = value);
}
[@override](/user/override)
Widget build(BuildContext context) => Scaffold(
body: HeroHereSwitcher(
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
SizedBox(
width: 100,
height: 100,
child: curHeroType == HeroType.red
? HeroHere(
key: const ValueKey(HeroType.red),
tag: kHeroTag,
flightShuttleBuilder: _flightShuttleBuilder,
child: Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(75),
),
),
)
: null,
),
SizedBox(
width: 150,
height: 150,
child: curHeroType == HeroType.green
? HeroHere(
key: const ValueKey(HeroType.green),
tag: kHeroTag,
flightShuttleBuilder: _flightShuttleBuilder,
child: Container(
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(75),
),
),
)
: null,
),
],
),
),
),
bottomNavigationBar: BottomAppBar(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () => curHeroType = HeroType.red,
icon: Icon(curHeroType == HeroType.red
? Icons.circle
: Icons.radio_button_off),
color: curHeroType == HeroType.red
? Colors.red
: Theme.of(context).colorScheme.secondary,
),
],
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () => curHeroType = HeroType.green,
icon: Icon(curHeroType == HeroType.green
? Icons.circle
: Icons.radio_button_off),
color: curHeroType == HeroType.green
? Colors.green
: Theme.of(context).colorScheme.secondary,
),
],
),
],
),
),
);
Widget _flightShuttleBuilder(
BuildContext flightContext,
Animation<double> animation,
HeroHere fromHero,
HeroHere toHero,
) =>
Stack(
fit: StackFit.expand,
children: [
toHero.child,
FadeTransition(
opacity: ReverseAnimation(animation),
child: fromHero.child,
),
],
);
}
更多关于Flutter导航过渡动画插件hero_here的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter导航过渡动画插件hero_here的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter中使用hero_here
插件来实现导航过渡动画的示例代码。hero_here
是一个Flutter插件,它扩展了Hero
动画,使得在不同页面之间的导航过渡更加灵活和多样化。
首先,确保你已经在pubspec.yaml
文件中添加了hero_here
依赖:
dependencies:
flutter:
sdk: flutter
hero_here: ^x.y.z # 请替换为最新版本号
然后运行flutter pub get
来安装依赖。
接下来,是一个完整的示例,展示如何使用hero_here
插件在两个页面之间实现导航过渡动画。
主页面(Main Page)
import 'package:flutter/material.dart';
import 'package:hero_here/hero_here.dart';
import 'second_page.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'HeroHere Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MainPage(),
);
}
}
class MainPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Main Page'),
),
body: Center(
child: HeroHere(
tag: 'hero-tag',
builder: (context) => Material(
color: Colors.blue,
shape: CircleBorder(),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondPage()),
);
},
child: Icon(
Icons.arrow_forward,
color: Colors.white,
size: 50,
),
),
),
),
),
);
}
}
第二页面(Second Page)
import 'package:flutter/material.dart';
import 'package:hero_here/hero_here.dart';
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: HeroHere(
tag: 'hero-tag',
builder: (context) => Material(
color: Colors.red,
shape: CircleBorder(),
child: Icon(
Icons.arrow_back,
color: Colors.white,
size: 50,
),
),
),
),
);
}
}
解释
- 依赖添加:确保在
pubspec.yaml
文件中添加了hero_here
依赖。 - MainPage:在主页面的中心位置,我们创建了一个
HeroHere
组件,并赋予它一个唯一的tag
值(在这个例子中是'hero-tag'
)。点击图标时,会导航到SecondPage
。 - SecondPage:在第二页面,我们也创建了一个
HeroHere
组件,并赋予它相同的tag
值('hero-tag'
)。这样,Flutter就知道这两个HeroHere
组件是相关的,并会在导航时应用过渡动画。
运行代码
运行这个Flutter应用,你应该会看到,当从MainPage
导航到SecondPage
时,图标会以平滑的过渡动画从一个页面移动到另一个页面。
希望这个示例能帮助你理解如何在Flutter中使用hero_here
插件来实现导航过渡动画。如果有任何问题或需要进一步的帮助,请随时提问!