dart - Flutter:如何在英雄动画期间更改子元素的大小?

标签 dart flutter

我有一个 Hero,它有一个子元素,我想在英雄动画中更改大小(在本例中,我希望更改其纵横比)。

这是我的(简化的)构建方法:

Widget build(BuildContext build) {
  final aspect = expanded ? 1.3 : 1.0;
  return new Hero(
    child: Column(
      children: <Widget>[
      new AspectRatio(
        aspect: aspect,
        child: // Stuff that creates a header
      ),
      new Container(
        child: // General descriptions
      )
    ],
  );
}

现在,纵横比在两个值之间跳跃。我的问题是,我能否将方面与英雄动画一起制作动画,并让它从 1.3 慢慢切换到 1.0 并返回?

有关完整示例(它还显示了英雄动画期间奇怪的溢出问题),请参阅此要点: https://gist.github.com/fuzzybinary/74196bccecc6dd070b35474c4c9223d7

最佳答案

因为我现在在家里,所以我现在无法试用您的代码。但是我有一个工作中的英雄动画示例。试试这个。

// main.dart class
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart' show timeDilation;
import 'package:flutter_test7/photo_hero.dart';
import 'package:flutter_test7/second_page.dart';

class HeroAnimation extends StatelessWidget {
  Widget build(BuildContext context) {
    timeDilation = 2.5; // 1.0 means normal animation speed.

    return new Scaffold(
      appBar: new AppBar(
        title: const Text('Basic Hero Animation'),
      ),
      body: new Center(
        child: new PhotoHero(
          photo: 'images/flippers-alpha.png',
          width: 300.0,
          onTap: () {
            Navigator.of(context).pushNamed('/second_page');
          },
        ),
      ),
    );
  }
}

void main() {
  runApp(
    new MaterialApp(
      home: new HeroAnimation(),
      routes: <String, WidgetBuilder>{
        '/second_page': (context) => new SecondPage()
      },
    ),
  );
}

// photo_hero.dart class
import 'package:flutter/material.dart';

class PhotoHero extends StatelessWidget {
  const PhotoHero({Key key, this.photo, this.onTap, this.width})
      : super(key: key);

  final String photo;
  final VoidCallback onTap;
  final double width;

  Widget build(BuildContext context) {
    return new SizedBox(
      width: width,
      child: new Hero(
        tag: photo,
        child: new Material(
          color: Colors.transparent,
          child: new InkWell(
            onTap: onTap,
            child: new Image.asset(
              photo,
              fit: BoxFit.contain,
            ),
          ),
        ),
      ),
    );
  }
}

// second_page.dart class
import 'package:flutter/material.dart';
import 'package:flutter_test7/photo_hero.dart';

class SecondPage extends StatefulWidget {
  @override
  _SecondPageState createState() => new _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: const Text('Flippers Page'),
      ),
      body: new Container(
        color: Colors.lightBlueAccent,
        padding: const EdgeInsets.all(16.0),
        alignment: Alignment.topLeft,
        child: new PhotoHero(
          photo: 'images/flippers-alpha.png',
          width: 100.0,
          onTap: () {
            Navigator.of(context).pop();
          },
        ),
      ),
    );
  }
}

希望这对您有所帮助。

关于dart - Flutter:如何在英雄动画期间更改子元素的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50500994/

相关文章:

flutter - RenderFlex 在底部溢出了 729 像素。在 SingleChildScrollView

dart - Flutter ReorderableListView 示例

android - flutter Android错误: ensureInitializationComplete must be called after startInitialization

Flutter Layout Row/Column - 共享宽度,扩展高度

android - Flutter Scaffold 允许子系统在导航栏后面

android - Flutter - 如何使用无代码设置连接到 Facebook 的 SDK 应用程序事件

flutter - 在一个小部件子列表中多次使用全局键

class - 有人在Dart做类树吗?

dart - 检测小部件何时在 Flutter 中更改位置

android - Flutter无法混淆和去除符号