【问题标题】:Flutter Hero animation does not use theme colors?Flutter Hero 动画不使用主题色?
【发布时间】:2018-12-17 12:32:16
【问题描述】:

当在 MaterialApp 的应用栏中创建一个简单的英雄动画时,英雄动画似乎没有使用主题颜色,除非在图标本身中明确指定了颜色。 有人可以解释为什么未明确设置图标颜色时图标的颜色在飞行过程中会发生变化吗?英雄无权访问主题数据吗?还是使用其他颜色集?

例子:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: Hero(
          tag: "mytag",
          child: Material(
            color: Colors.transparent,
            child: IconButton(
              icon: Icon(
                Icons.menu,
                // uncomment below line and the flying icon is white as expected...
                // color: Theme.of(context).primaryIconTheme.color
              ),
              onPressed: () {
                Navigator.of(context).push(
                  PageRouteBuilder(
                    pageBuilder: 
                      (context, animation, secondaryAnimation) => SecondPage()
                  )
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: <Widget> [
          Hero(
            tag: "mytag",
            child: Material(
              color: Colors.transparent,
                child: IconButton(
                icon: Icon(
                  Icons.menu,
                  // uncomment below line and the reverse flying icon is white as expected...
                  // color: Theme.of(context).primaryIconTheme.color
                ),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ),
          ),
        ]
      ),
    );
  }
}

【问题讨论】:

    标签: flutter flutter-animation


    【解决方案1】:

    这是因为Hero 的飞行动画是OverlayEntry 上的MaterialApp。 因此,虽然使用的小部件是相同的(图标),但它的位置却不同。

    问题是,在您的情况下,IconTheme.of(context) 根据该位置返回不同的值:

    • 作为AppBar 的子代,IconTheme 被覆盖以处理 primaryColor 作为背景
    • 在其他任何地方,它都使用MaterialApp 指定的默认主题。

    所以在动画过程中,使用的IconTheme是不同的,导致这样的问题。


    一个潜在的解决方案是修复该值以确保使用的IconTheme 始终相同:

    AppBar(
      leading: Builder(
        builder: (context) {
          return Hero(
            child: IconTheme(
              data: Theme.of(context).primaryIconTheme,
              child: Icon(Icons.whatshot),
            ),
          );
        },
      ),
    );
    

    【讨论】:

    • 啊,明白了:所以在应用程序栏中使用primaryIconTheme,而在飞行过程中iconTheme,这就是颜色不同的原因。谢谢
    • 将按钮包裹在Theme 小部件中时,图标实际上一直是黑色的。用data: Theme.of(context).primaryIconTheme 将它包裹在IconTheme 中确实可以解决问题。我会坚持下去,因为如果我选择更改大小或不透明度。
    猜你喜欢
    • 2019-04-04
    • 2019-12-16
    • 2021-10-07
    • 2019-06-25
    • 2020-10-24
    • 2020-03-02
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    相关资源
    最近更新 更多