我正在try 创建一个应用程序栏,它在展开时看起来像下图

enter image description here

当折叠时,我只需要文本的初始列在应用程序栏中可用,因此图像应该淡出,文本列应该设置动画并成为应用程序栏标题

我曾try 使用SliverAppBar和自定义滚动视图,但灵活空间的标题看起来不太好,而且我不知道如何使图像单独消失,并在折叠时仅显示文本.

到目前为止,这是扩展状态

enter image description here

当我试图折叠它时,就会发生这种情况

enter image description here

这是相同的代码

CustomScrollView(
    slivers: [
      SliverAppBar(
          pinned: true,
          leading: GestureDetector(
            onTap: () => Get.back(),
            child: const Icon(
              Icons.arrow_back,
              color: Colors.black,
            ),
          ),
          flexibleSpace: FlexibleSpaceBar(
            title: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16.0),
              child: Row(
                children: [
                  const Expanded(
                    child: Text.rich(
                      TextSpan(
                          text: "Buffet Deal\n",
                          style:
                              TextStyle(color: Colors.black, fontSize: 14),
                          children: [
                            TextSpan(
                              text: "Flash Deal",
                              style: TextStyle(
                                  color: Colors.black, fontSize: 10),
                            ),
                          ]),
                    ),
                  ),
                  if (_model.imgUrl != null)
                    CachedNetworkImage(
                      imageUrl: _model.imgUrl!,
                    ),
                  const SizedBox(
                    width: 18,
                  )
                ],
              ),
            ),
          ),
          expandedHeight: Get.height * 0.2,
          backgroundColor: const Color(0xFFFFF4F4)),
      SliverToBoxAdapter(
        child: Container()
      )
    ],
  ),

谢谢你的帮助.提前感谢

推荐答案

要使滑动应用条的灵活空间图像在扩展空间塌陷时消失,只需使用FlexibleSpaceBar.background属性.

要解决重叠的文本,只需将FlexibleSpaceBar环绕在LayoutBuilder周围,以获得它是否展开,并调整文本位置.

查看下面的结果(还有live demo on DartPad个):

Screenshot

import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final expandedHeight = MediaQuery.of(context).size.height * 0.2;
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverAppBar(
            pinned: true,
            leading: GestureDetector(
              onTap: () => {},
              child: const Icon(
                Icons.arrow_back,
                color: Colors.black,
              ),
            ),
            flexibleSpace: LayoutBuilder(builder: (context, constraints) {
              final fraction =
                  max(0, constraints.biggest.height - kToolbarHeight) /
                      (expandedHeight - kToolbarHeight);
              return FlexibleSpaceBar(
                title: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16.0),
                  child: Row(
                    children: [
                      Expanded(
                        child: Padding(
                          padding: EdgeInsets.only(left: 24 * (1 - fraction)),
                          child: const Text.rich(
                            TextSpan(
                                text: "Buffet Deal\n",
                                style: TextStyle(
                                    color: Colors.black, fontSize: 14),
                                children: [
                                  TextSpan(
                                    text: "Flash Deal",
                                    style: TextStyle(
                                        color: Colors.black, fontSize: 10),
                                  ),
                                ]),
                          ),
                        ),
                      ),
                      const SizedBox(width: 18)
                    ],
                  ),
                ),
                background: Align(
                  alignment: Alignment.centerRight,
                  child: Image.network(
                    "https://source.unsplash.com/random/400x225?sig=1&licors",
                  ),
                ),
              );
            }),
            expandedHeight: MediaQuery.of(context).size.height * 0.2,
            backgroundColor: const Color(0xFFFFF4F4),
          ),
          const SliverToBoxAdapter(child: SizedBox(height: 1000))
        ],
      ),
    );
  }
}

Flutter相关问答推荐

如何在应用栏中将列置于中心

Flutter 中的面向对象模式

如何处理Flutter Riverpod异步通知器中的状态和数据库

就像Flutter 中的头文件一样?

我有相关的下拉菜单Flutter Windows应用程序的问题

Ffmpeg_kit_fltter:^6.0.3版权问题

在flutter中实现类似3D的可滚动堆叠分页列表

运行调试flutter应用程序时出错:B/BL超出范围(最大+/-128MB)到'';

Flutter Xcode 15 错误(Xcode):DT_TOOLCHAIN_DIR 无法用于判断 LIBRARY_SEARCH_PATHS

如何在flutter中将所有单词的第一个字母大写

如何正确地将 PageView 设置为 AppBar 的标题?

如何使用 riverpod_annotation 创建非自动处置提供程序?

Flutter:为什么我的 listTile colored颜色 被容器 colored颜色 覆盖了?

将 String 与 List 元素进行比较时出现问题

如何从 Flutter Slider Widget 中移除发光效果?

如何在 SingleChildScrollView 小部件中管理自定义小部件状态

如何使按钮被选中?

带图像封面的 ElevatedButton

Flutter 小部件中的视图和逻辑分离

如何在Flutter 中将列表的模型类型转换为数组?