缩小 SliverAppBar 图像动画,如 Flutter 中的标题文本

Flo*_*ish 2 animation dart flutter flutter-sliver sliverappbar

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

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

下面是 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'),
      ),
    ],
  ),
),
Run Code Online (Sandbox Code Playgroud)

下面是 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,
);
Run Code Online (Sandbox Code Playgroud)

这是上面代码的结果:

在此输入图像描述

Flo*_*ish 6

经过长时间的研究,我能够使用/sf/answers/4152659941/作为参考:

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))

    ],
  ),
);
Run Code Online (Sandbox Code Playgroud)

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

下面是结果演示:

在此输入图像描述