Flutter Hero创建重复的文本

时间:2019-01-18 23:51:41

标签: mobile dart flutter

我正在尝试对英雄动画进行编码,以在屏幕之间切换。我使用一种颤动动画将卡片上的动画制作到下一页,并使用另一种颤动动画将文本从卡片上移动到下一页上。不幸的是,在我在下面的代码中放慢了的动画中,有一个重复的文本项。

下面是我用来测试点击卡片将其扩展到新页面的代码。我一辈子都想不通如何防止文本在动画中重复。罪魁祸首是页面上有一个英雄(动画化页面之间的过渡),页面内有一个英雄。

Card (page-hero)
---> CardText (text-hero)

and

Page (page-hero)
---> PageText (text-hero)

注意:下面的代码是我从另一个问题中采用的,用于检验该理论:How to expand a card on tap in flutter?

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart' show timeDilation;

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) {
    timeDilation = 10.0;
    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 Container(child: new Hero(
                  tag: palette[index].keys.first,
                  child: new GestureDetector(
                    onTap: () {
                      Navigator
                          .of(context)
                          .push(new ColorPageRoute(palette[index]));
                    },
                    child: new Card(
                      // height: 64.0,
                      // width: double.infinity,
                      color: new Color(palette[index].values.first),
                      child: new Align(
                        alignment: Alignment.topRight,
                        child: new Hero(
                          tag: 'text-${palette[index].keys.first}',
                          child: SizedBox(
                            width: 250,
                            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: Container( child: new Hero(
        tag: color.keys.first,
        child: new Container(
          color: new Color(color.values.first),
          child: new Align(
            // alignment: Alignment.topCenter,
            child: new Hero(
              tag: 'text-${color.keys.first}',
              child: new SizedBox(
                width: 200,
                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,
                ), fullscreenDialog: true);

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

duplicate text gif

预期的动画是一点点文本,而不是您在上面的代码中看到的重复文本。我该如何做,以免得到重复的文本?谢谢!

1 个答案:

答案 0 :(得分:0)

在英雄中不包含英雄,这已解决

body: Container( child: new Hero(
        tag: color.keys.first,
        child: new Container(
          color: new Color(color.values.first),
          child: new Align(
             alignment: Alignment.topCenter,
            child: Hero( // remove this hero
              tag: 'text-${color.keys.first}',
              child: new SizedBox(
                  width: 200,
                  child: new Text(
                    color.keys.first,
                    style:
                    Theme.of(context).textTheme.title.copyWith(color: Colors.white),
                  )),
            ),
          ),
        ),