【问题标题】:In Flutter, How to make SliverAppBar respect the top safe area on Floating State when it is not primary在 Flutter 中,当 SliverAppBar 不是主要的时,如何使 SliverAppBar 尊重浮动状态的顶部安全区域
【发布时间】:2021-04-03 09:33:24
【问题描述】:

我有一个 SliverAppBar,看起来这是我想要的正常状态:

但是当向下滚动时,应用栏不尊重其浮动状态的顶部安全区域:

这是我的构建方法代码

      return Scaffold(
      body: CustomScrollView(
        controller: _scrollController,
        slivers: <Widget>[
          SliverSafeArea(
            bottom: false,
            sliver: SliverPadding(
              padding: const EdgeInsets.symmetric(horizontal: 5),
              sliver: SliverAppBar(
                primary: false,
                centerTitle: true,
                actions: actions,
                floating: true,
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
                title: const Text('title'),
              ),
            ),
          ),
          SliverGrid(
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 4,
            ),
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return Container(
                  margin: const EdgeInsets.all(20),
                  color: Colors.amber,
                );
              },
              childCount: 130,
            ),
          ),
        ],
      ),
    );

【问题讨论】:

    标签: flutter sliverappbar


    【解决方案1】:

    我认为您需要一个自定义应用栏。

    类似的东西

    class FloatingAppBar extends StatelessWidget {
      const FloatingAppBar(
        this.title, {
        this.actions = const <Widget>[],
        Key? key,
        this.leading = const BackButton(),
        this.height = 48,
      }) : super(key: key);
    
      final String? title;
      final List<Widget> actions;
      final Widget leading;
      final double height;
    
      @override
      Widget build(BuildContext context) {
        //final Color bgColor = isDark(context) ? Colors.grey.shade800 : Colors.white;
        return Center(
          child: Container(
            height: height,
            width: MediaQuery.of(context).size.width - 20,
            constraints: const BoxConstraints(maxWidth: 600),
            decoration: BoxDecoration(
              color: isDark(context) ? Colors.grey.shade800 : Colors.white,
              borderRadius: BorderRadius.circular(8),
              boxShadow: <BoxShadow>[
                BoxShadow(
                  color: isDark(context) ? Colors.black54 : Colors.grey.shade500,
                  blurRadius: 1,
                  spreadRadius: 0.1,
                  offset: const Offset(0, 0.7),
                ),
              ],
            ),
            padding: const EdgeInsets.all(0),
            margin: const EdgeInsets.only(top: 5),
            child: Row(
              children: <Widget>[
                leading,
                Expanded(
                  child: Center(
                    child: Text(
                      title ?? '',
                      textDirection: getTextDirection(title ?? ''),
                      style: const TextStyle(
                        fontWeight: FontWeight.bold,
                        //color: Colors.black87,
                      ),
                    ),
                  ),
                ),
                if (actions.isEmpty)
                  const IconButton(
                    padding: EdgeInsets.all(0),
                    iconSize: 20,
                    icon: Icon(iconArrowLeft, color: Colors.transparent),
                    onPressed: null,
                  ),
                //
                ...actions
              ],
            ),
          ),
        );
      }
    }
    
    
    

    【讨论】:

    • 感谢您的回答,但我想使用默认的 AppBar + 您的代码在滚动时没有实现 AppBar 的浮动方面!
    猜你喜欢
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2020-03-16
    • 2021-01-22
    相关资源
    最近更新 更多