【问题标题】:How to implement a SliverAppBar with a collapsable search bar如何使用可折叠搜索栏实现 SliverAppBar
【发布时间】:2019-09-08 05:25:33
【问题描述】:

This is what I'm trying to do,它是 iOS 上非常常见的 Widget。这是我的代码:

return Scaffold(
  backgroundColor: Colors.white,
  body: CustomScrollView(
    slivers: <Widget>[
      SliverAppBar(
        automaticallyImplyLeading: false,
        pinned: true,
        titleSpacing: 0,
        backgroundColor: Colors.white,
        elevation: 1.0,
        title: Container(
          width: double.infinity,
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              CupertinoButton(
                padding: EdgeInsets.all(0),
                onPressed: () {},
                child: AutoSizeText(
                  'Ordenar',
                  style: TextStyle(
                    color: Color(0xff16161F),
                    fontFamily: 'Brutal',
                    fontSize: 17.0,
                  ),
                ),
              ),
              AutoSizeText(
                'Equipos 14',
                style: TextStyle(
                  color: Color(0xff16161F),
                  fontFamily: 'Archia',
                  fontSize: 22.0,
                ),
              ),
              CupertinoButton(
                padding: EdgeInsets.all(0),
                onPressed: () {},
                child: AutoSizeText(
                  'Editar',
                  style: TextStyle(
                    color: Color(0xff16161F),
                    fontFamily: 'Brutal',
                    fontSize: 17.0,
                  ),
                ),
              ),
            ],
          ),
        ),
        centerTitle: true,
      ),
      SliverFillRemaining(
        child: Center(
          child: CupertinoButton(
            onPressed: () {
              setState(() {
                (isBottom) ? isBottom = false : isBottom = true;
              });
            },
            child: Text('YESSS'),
          ),
        ),
      ),
    ],
  ),
  bottomNavigationBar: (isBottom) ? _toolBar() : _tabBar(),
);

我尝试将CupertinoTextField() 添加到bottom 属性,但它进入了我的Row() 并搞砸了一切。有没有人这样做,或者知道如何实现它?

谢谢。

【问题讨论】:

    标签: flutter flutter-layout flutter-sliver


    【解决方案1】:

    我设法解决了。 SliverAppBar 有一个名为flexibleSpace 的属性。在这里放置一个FlexibleSpaceBar,其中包含一个background 属性。现在,不要上当,这不仅限于纯色或图像。它可以采用您想要的任何小部件。就我而言,我想添加一个搜索栏。并且因为这个属性将填充整个expandedHeight 属性,所以您需要添加一个小的空白区域,这样您的自定义小部件就不会被绘制在您的 SliverAppBar 上。这是相关的代码:

    flexibleSpace: FlexibleSpaceBar(
              background: Column(
                children: <Widget>[
                  SizedBox(height: 90.0),
                  Padding(
                    padding: const EdgeInsets.fromLTRB(16.0, 6.0, 16.0, 16.0),
                    child: Container(
                      height: 36.0,
                      width: double.infinity,
                      child: CupertinoTextField(
                        keyboardType: TextInputType.text,
                        placeholder: 'Filtrar por nombre o nombre corto',
                        placeholderStyle: TextStyle(
                          color: Color(0xffC4C6CC),
                          fontSize: 14.0,
                          fontFamily: 'Brutal',
                        ),
                        prefix: Padding(
                          padding:
                              const EdgeInsets.fromLTRB(9.0, 6.0, 9.0, 6.0),
                          child: Icon(
                            Icons.search,
                            color: Color(0xffC4C6CC),
                          ),
                        ),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(8.0),
                          color: Color(0xffF0F1F5),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
    

    这里是the result。滚动向上时搜索栏将消失。希望这可以帮助任何想要这样做的人。

    【讨论】:

    • 这会导致小屏幕手机出现一些时间错误!如何解决这个问题?或给出动态高度而不是固定高度
    • 在 Pixel 2 等小屏幕手机的模拟器上尝试相同的操作,您将面临溢出错误
    • 我认为而不是背景.. 应该使用标题.. 带有背景的小部件永远不会出现在 android 模拟器的屏幕上..
    猜你喜欢
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多