animation - 在 Flutter 中将 ListView 项目动画化为全屏

标签 animation flutter

我想让我的列表项执行this animation ( mp4 ) 点击时。我尝试使用 AnimatedCrossFade 但它要求它的两个 child 处于同一级别,例如详细 View 与 ListView 交叉淡入淡出,而不是被点击的项目。事实上,Hero 动画似乎是唯一可以跨小部件制作动画的动画。

我在使用英雄时遇到问题。它应该包装列表项吗? Widget 子树在 Hero 源/目标中是否显着不同是否重要?另外,Hero 动画能否与 LocalHistoryRoutes 或 staggered animations 一起使用? ?

编辑

现在看起来我需要做的是使用 Overlay,困难的部分是我需要将所选项目添加到屏幕上被点击的相同位置的叠加层,那么动画部分就很简单了。可能在这里使用的是目标/追随者模式,例如CompositedTransformTarget

最佳答案

您可以只使用 Hero 小部件来制作这种动画。这是我的例子:

enter image description here

及源代码:

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new FirstPage(title: 'Color Palette'),
    );
  }
}

class FirstPage extends StatefulWidget {
  FirstPage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _FirstPageState createState() => new _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  final palette = [
    {'#E53935': 0xFFE53935},
    {'#D81B60': 0xFFD81B60},
    {'#8E24AA': 0xFF8E24AA},
    {'#5E35B1': 0xFF5E35B1},
    {'#3949AB': 0xFF3949AB},
    {'#1E88E5': 0xFF1E88E5},
    {'#039BE5': 0xFF039BE5},
    {'#00ACC1': 0xFF00ACC1},
    {'#00897B': 0xFF00897B},
    {'#43A047': 0xFF43A047},
    {'#7CB342': 0xFF7CB342},
    {'#C0CA33': 0xFFC0CA33},
  ];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Container(
        child: new ListView.builder(
            itemCount: palette.length,
            itemBuilder: (context, index) => new Hero(
                  tag: palette[index].keys.first,
                  child: new GestureDetector(
                    onTap: () {
                      Navigator
                          .of(context)
                          .push(new ColorPageRoute(palette[index]));
                    },
                    child: new Container(
                      height: 64.0,
                      width: double.infinity,
                      color: new Color(palette[index].values.first),
                      child: new Center(
                        child: new Hero(
                          tag: 'text-${palette[index].keys.first}',
                          child: new Text(
                            palette[index].keys.first,
                            style: Theme.of(context).textTheme.title.copyWith(
                                  color: Colors.white,
                                ),
                          ),
                        ),
                      ),
                    ),
                  ),
                )),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  final Map<String, int> color;

  SecondPage({this.color});

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Color'),
      ),
      body: new Hero(
        tag: color.keys.first,
        child: new Container(
          color: new Color(color.values.first),
          child: new Center(
            child: new Hero(
              tag: 'text-${color.keys.first}',
              child: new Text(
                color.keys.first,
                style:
                    Theme.of(context).textTheme.title.copyWith(color: Colors.white),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class ColorPageRoute extends MaterialPageRoute {
  ColorPageRoute(Map<String, int> color)
      : super(
            builder: (context) => new SecondPage(
                  color: color,
                ));

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    return FadeTransition(opacity: animation, child: child);
  }
}

关于animation - 在 Flutter 中将 ListView 项目动画化为全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50457078/

相关文章:

javascript - Css3动态添加的元素没有得到动画

html - SVG 剪贴蒙版未按预期工作

iphone - 设置了animationDidStop但没有被调用(iPhone应用程序编码)

Flutter 过渡导出

flutter - 当在Stream上使用Distinct()时似乎无法过滤出相同的结果

android - flutter中实现自定义底部导航栏

android - Flutter - Android 在 Release模式下不请求许可 - 自动拒绝

ios - 使用 Swift 在 UIScrollView 上动画 zoomScale

flutter - 如何在Flutter中重新访问时保存页面状态

javascript - Svg 动画对象跟随路径