flutter - 收缩 SliverAppBar 图像动画就像 Flutter 中的标题文本

标签 flutter dart animation flutter-sliver sliverappbar

我正在尝试复制带有图像的 SliverAppBar 自然出现的文本收缩效果,以便 sliverhead 的背景图像收缩为 sliverAppBar 的应用栏中的前导图标。

我尝试使用 AnimatedPostioned flutter 小部件以及滚动 Controller ,该 Controller 在页面滚动时切换 isSkrink bool 值的状态。

下面是 AnimatedPosition 小部件:

AnimatedPositioned(
  width: isShrink ? 10.0 : 200.0,
  height: isShrink ? 10.0 : 200.0,
  duration: Duration(seconds: 2),
  curve: Curves.linear,
  child: Stack(
    children: [
      Container(
        child: Image.asset(
            'assets/images/user-avatar.png'),
      ),
    ],
  ),
),

下面是 SliverAppBar,堆栈中有 AnimatedPositioned,SliverAppBar 的前导参数中有用户图标

SliverAppBar(
  leading: isShrink
      ? Padding(
          padding: EdgeInsets.all(10),
          child: CircleAvatar(
            backgroundImage:
                AssetImage('assets/images/user-avatar.png'),
            backgroundColor: Colors.white,
            radius: 3,
          ),
        )
      : Container(),
  floating: true,
  pinned: true,
  flexibleSpace: FlexibleSpaceBar(
    title: Row(
      children: <Widget>[
        Text(
          "Username",
          style: bodyText1Style(
            context,
            color: isShrink ? Colors.black : Colors.white,
            fontSize: isShrink ? 18 : 15,
            fontWeight: FontWeight.w600,
          ),
        ),
      ],
    ),
    background: Stack(
      children: <Widget>[
        AnimatedPositioned(
          width: isShrink ? 10.0 : 200.0,
          height: isShrink ? 10.0 : 200.0,
          duration: Duration(seconds: 2),
          curve: Curves.linear,
          child: Stack(
            children: [
              Container(
                child: Image.asset(
                    'assets/images/user-avatar.png'),
              ),
            ],
          ),
        ),
      ],
    ),
  ),
  expandedHeight: size.height * 0.35,
);

这是上面代码的结果:

enter image description here

最佳答案

经过长时间的研究,我能够使用 https://stackoverflow.com/a/59323713/3636615 来解决这个问题作为引用:

Scaffold(
  body: CustomScrollView(
    slivers: <Widget>[
      TransitionAppBar(
        // extent: 250,
        extent: 300,
        avatar: Container(
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(
                      'assets/images/user-avatar.png'), // NetworkImage(user.imageUrl),
                  fit: BoxFit.cover)),
        ),
        title: "Emmanuel Olu-Flourish",
      ),
      SliverList(
          delegate: SliverChildBuilderDelegate((context, index) {
        return Container(
            child: ListTile(
              title: Text("${index}a"),
            ));
      }, childCount: 25))

    ],
  ),
);

在此处检查 TransitionAppBar 实现: https://gist.github.com/Oluflourish/2f0789bd2e8ee576a2d6364d709c1c14

下面是结果演示:

enter image description here

关于flutter - 收缩 SliverAppBar 图像动画就像 Flutter 中的标题文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65129870/

相关文章:

flutter - 即使我的文本字段不为空,错误消息也不会出现

dart - Flutter BLoC 模式 - 如何在流事件后导航到另一个屏幕?

html - 为什么基于关键帧的动画最终会变慢?

javascript - Liquid Layout, JS 解决方案

javascript - 使用jquery淡出背景颜色

dart - 在 flutter 中使用自定义脚手架复制 GlobalKey

user-interface - 如何创建独特的TextBox设计

dart - 将 BLoC 的接收器与另一个 BLoC 连接

android - 有没有办法在 Android 上的 Flutter 应用程序中拦截 'Back' keydown?

javascript - 在 Flutter Webview 中运行一些 Javascript 文件,例如 index.js