带有动画定位图像的Flutter动画SliverAppBar

jucafojl  于 2023-01-21  发布在  Flutter
关注(0)|答案(2)|浏览(167)

我尝试用我的appBar复制这个动画:

我知道我可以使用SliverAppBar并简单地动画化textSize,但是我如何实现图像的逻辑呢?它向右移动并稍微缩小。
这是我为text准备的:

SliverAppBar(
    expandedHeight: 200,
    flexibleSpace: FlexibleSpaceBar(
      title: Text('Test', textScaleFactor: 1),
    ),
    pinned: true,
  ),

你知道我该怎么解决吗?

brc7rcf0

brc7rcf01#

你玩SliverPersistentHeaderDelegate

class AppSliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    final t = shrinkOffset / maxExtent;
    return Stack(
      children: [
        Align(
          alignment: Alignment(0, .7), //perhaps it should also use lerp
          child: Text(
            "Title",
            style: TextStyle(fontSize: ui.lerpDouble(34, 14, t)),
          ),
        ),
        Align(
          alignment:
              Alignment.lerp(Alignment.topCenter, Alignment.topRight, t)!,
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Icon(Icons.settings),
          ),
        )
      ],
    );
  }

  @override
  double get maxExtent => 200;

  @override
  double get minExtent => kToolbarHeight;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}

并用于

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        body: CustomScrollView(
          slivers: [
            SliverPersistentHeader(
              pinned: true,
              delegate: AppSliverPersistentHeaderDelegate(),
            ),
            SliverToBoxAdapter(
              child: Container(
                height: 12222,
                color: Colors.red,
              ),
            )
          ],
        ),
      ),
    );
  }
}
bkhjykvo

bkhjykvo2#

你可以尝试使用AnimatedPositioned类,其中flutter已经提供.检查此链接
https://api.flutter.dev/flutter/widgets/AnimatedPositioned-class.html
您可以使用它并根据特定操作更改位置和大小。

相关问题