Flutter Hero

作者: 三只仓鼠 | 来源:发表于2020-03-06 17:20 被阅读0次

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),
              ),
            )
          ],
        ),
      ),
    );
  }
}

相关文章

  • flutter之Hero动画

    Hero指的是可以在路由(页面)之间“飞行”的widget。使用Flutter的Hero widget创建hero...

  • Flutter – Radial Hero Animation

    Flutter – Radial Hero Animation[https://www.geeksforgeeks...

  • Flutter Hero

    Hero指的是可以在路由(页面)之间“飞行”的widget,简单来说Hero动画就是在路由切换时,有一个共享的wi...

  • flutter九宫格看图

    flutter九宫格图片 利用GridView实现布局,用Hero动画进行展示

  • Flutter-Hero动画

    详细介绍 你会学到什么: Hero指的是可以在路由(页面)之间“飞行”的widget。 使用Flutter的Her...

  • Flutter Hero动画案例

    Hero 指的是可以在路由(页面)之间“飞行”的 widget,简单来说 Hero 动画就是在路由切换时,有一个共...

  • Flutter 动画篇——Hero

    Hero 动画使用方法 我们先来看一下效果rt: Hero动画实现了元素共享功能,简单来说: Hero动画就是在路...

  • Flutter Hero/FadeTransition动画

    图片列表 --> 图片详情的 Hero动画 图片列表页 图片详情页

  • flutter 开发问题汇总

    Flutter踩坑整理 1、Hero动画 Text 出现黄色条纹 以及红色字体的问题 字体变化需要设置好TextS...

  • Flutter(三十七)Hero动画

    特点 页面A有个圆形头像,页面B也有个圆形头像,头像图片是同一个。从A跳转到B时,一般都是硬性跳转,两个头像之间没...

网友评论

    本文标题:Flutter Hero

    本文链接:https://www.haomeiwen.com/subject/twjwrhtx.html