Hero指的是可以在路由(页面)之间“飞行”的widget,简单来说Hero动画就是在路由切换时,有一个共享的widget可以在新旧路由间切换。由于共享的widget在新旧路由页面上的位置、外观可能有所差异,所以在路由切换时会从旧路由逐渐过渡到新路由中的指定位置,这样就会产生一个Hero动画。
1.gif
导入图片资源
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Avengers'),
);
}
}
class MySuperHero {
final String img;
final String title;
final String body;
MySuperHero(this.img, this.title, this.body);
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<MySuperHero> items = new List<MySuperHero>();
_MyHomePageState() {
items.add(new MySuperHero("assets/iron_man.png", "Iron Man",
"Genius. Billionaire. Playboy. Philanthropist. Tony Stark's confidence is only matched by his high-flying abilities as the hero called Iron Man."));
items.add(new MySuperHero("assets/captain_america.png", "Captain America",
"Recipient of the Super-Soldier serum, World War II hero Steve Rogers fights for American ideals as one of the world’s mightiest heroes and the leader of the Avengers."));
items.add(new MySuperHero("assets/thor.png", "Thor",
"The son of Odin uses his mighty abilities as the God of Thunder to protect his home Asgard and planet Earth alike."));
items.add(new MySuperHero("assets/hulk.png", "Hulk",
"Dr. Bruce Banner lives a life caught between the soft-spoken scientist he’s always been and the uncontrollable green monster powered by his rage."));
items.add(new MySuperHero("assets/black_widow.png", "Black Widow",
"Despite super spy Natasha Romanoff’s checkered past, she’s become one of S.H.I.E.L.D.’s most deadly assassins and a frequent member of the Avengers."));
}
Widget SuperHeroCell(BuildContext context, int index) {
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyDetailPage(items[index])));
},
child: Card(
margin: EdgeInsets.all(8),
elevation: 4.0,
child: Container(
padding: EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Hero(
tag: items[index],//这里的tag值必须和跳转页面的hero组件tag值一样
child: Image.asset(items[index].img),
),
SizedBox(
width: 16,
),
Text(
items[index].title,
style:
TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
],
),
Icon(Icons.navigate_next, color: Colors.black38),
],
),
)),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Stack(
children: <Widget>[
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) => SuperHeroCell(context, index),
)
],
),
),
);
}
}
class MyDetailPage extends StatefulWidget {
MySuperHero _superHero;
MyDetailPage(MySuperHero superHero) {
_superHero = superHero;
}
@override
_MyDetailPageState createState() => _MyDetailPageState(_superHero);
}
class _MyDetailPageState extends State<MyDetailPage> {
MySuperHero superHero;
_MyDetailPageState(MySuperHero superHero) {
this.superHero = superHero;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(superHero.title),
centerTitle: true,
),
body: Center(
child: Column(
children: <Widget>[
SizedBox(height: 50),
Hero(
transitionOnUserGestures: true,
tag: superHero,
child: Transform.scale(
scale: 2.0,
child: Image.asset(superHero.img),
),
),
SizedBox(height: 30),
Card(
elevation: 8,
margin: EdgeInsets.all(16),
child: Container(
padding: EdgeInsets.all(16),
child: Text(superHero.body),
),
)
],
),
),
);
}
}
网友评论