flutter - 如何使我的应用程序栏充当 Material 搜索小部件,同时保持灵活的应用程序栏

标签 flutter flutter-layout

我希望我的应用栏在向下滚动或用户搜索某些内容时充当固定应用栏。

SliverAppBar(
  title: new TextField(
    style: Theme.of(context).primaryTextTheme.title,
    decoration: InputDecoration(
      hintText: '검색',
    ),
  ),
),

searching


但是当它向上滚动并且用户没有搜索时,我想将其绘制为灵活的应用程序栏。

flexibleSpace: new FlexibleSpaceBar(
  centerTitle: false,
  title: new TextField(
    style: Theme.of(context).primaryTextTheme.title,
    decoration: InputDecoration(
      hintText: '검색',
    ),
  ),
  background: Stack(
    fit: StackFit.expand,
    children: <Widget>[
      SizedBox(
        height: 256.0,
        child: Container(
          child: Padding(
            padding: const EdgeInsets.only(top: 24.0),
            child: Column(
              children: <Widget>[
                Align(
                  alignment: Alignment.topLeft,
                  child: FlutterLogo(
                    size: 64.0,
                  ),
                ),
                Padding(padding: const EdgeInsets.only(bottom: 24.0)),
                ListTile(
                  title: Text('Some Text'),
                ),
              ],
            ),
          ),
        ),
      ),
      // This gradient ensures that the toolbar icons are distinct
      // against the background image.
    ],
  ),
),

scrolled-up

当使用第二种方法向上滚动时,搜索字段会转换为右上角的一点。

最佳答案

将标题内容移动到另一个SliverList中即可实现该效果。

从SliverAppBar中删除flexibleSpace,并将flexibleSpace.background的内容移动到SliverList之前SliverAppBar。

示例:

@override
Widget build(BuildContext context) {
  return new CustomScrollView(
    slivers: <Widget>[
      new SliverList(
          delegate: new SliverChildListDelegate(<Widget>[
        Align(
          alignment: Alignment.topLeft,
          child: FlutterLogo(size: 64.0),
        ),
        ListTile(
          title: Text('Some Text'),
        ),
        ListTile(),
      ])),
      new SliverAppBar(
        backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        elevation: 0.0,
        automaticallyImplyLeading: false,
        pinned: true,
        floating: false,
        title: new TextField(
          focusNode: _searchFocusNode,
          style: Theme.of(context).primaryTextTheme.title,
          decoration: InputDecoration(
            border: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
            suffixIcon: Icon(Icons.search),
            hintText: '검색',
          ),
        ),
      ),
      new SliverList(
        delegate: new SliverChildListDelegate(List.generate(
            100,
            (i) => ListTile(
                  title: Text('Scroll'),
                )).toList()),
      ),
    ],
  );
}

关于flutter - 如何使我的应用程序栏充当 Material 搜索小部件,同时保持灵活的应用程序栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52563949/

相关文章:

flutter - 底部溢出 112 像素的 RenderFlex

Flutter Appbar标题灰色背景显示

flutter - Flutter 中可以有不同的 Button 样式吗?

flutter - 如何在 flutter 中仅使一行2个元素中的一个元素居中

Flutter,如何将值从一个函数传递到不同页面上的另一个函数

flutter - 不为空时在 TextField 中显示图标

flutter - 为什么我们不能在具有 bloc 属性的 Consumer 内部使用 BlocProvider.of(blocContext) ?什么是解决方案?

react-native - 我可以在 flutter 中获得与 React Native 中的 <TouchableOpacity/> 相同的效果吗?

Flutter 错误 : Type 'File' not found. 这是怎么回事?

flutter - 失败 : Build failed with an exception. flutter.gradle' 行:904