我该如何从SliverPersistentHeader中删除高程?

问题描述 投票:0回答:1

希望这是一个简单的问题,我在CustomScrollView中有SliverPersistentHeader,看起来SliverPersistentHeader具有某种阴影/高程。是否可以删除它(我在红色框中勾勒出阴影)?

请参见下图:enter image description here

此基本主体是一个带有_makeHeader调用的SliverPersistentHeader的支架:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Consts.coMainBackground,
      body: CustomScrollView(
        slivers: <Widget>[
          _sliverAppBar(),
          _makeHeader(),
          BlocBuilder<AllPersonsBloc, AllPersonsState>(
            builder: (context, state) {
              if (state is AllPersonsLoading) {
                return _buildLoading();
              } else if (state is AllPersonsLoaded) {
                return _sliverList(context, state.persons);
              } else if (state is AllPersonsError) {
                return _buildErrorMessage(state.message);
              } else {
                return _buildErrorMessage('Unknown error!');
              }
            },
          ),
        ],
      ),
    );
  }

制作标题功能:

  Widget _makeHeader() {
    return SliverPersistentHeader(
      pinned: true,
      delegate: _SliverAppBarDelegate(
        minHeight: 130.0,
        maxHeight: 130.0,
        child: Container(
          color: Consts.coForestGreenBackground,
          child: Container(
            decoration: BoxDecoration(
              color: Consts.coMainBackground,
              borderRadius: BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15))
            ),
            child: _cardHeader('People', Icons.search, 'Name')),
          )
      ),
    );
  }

最后是Delegate函数:

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SliverAppBarDelegate({
    @required this.minHeight,
    @required this.maxHeight,
    @required this.child,
  });
  final double minHeight;
  final double maxHeight;
  final Widget child;

  @override
  double get minExtent => minHeight;
  @override
  double get maxExtent => math.max(maxHeight, minHeight);

  @override
  Widget build(
      BuildContext context, 
      double shrinkOffset, 
      bool overlapsContent) 
  {
    return new SizedBox.expand(child: child);
  }
  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
        minHeight != oldDelegate.minHeight ||
        child != oldDelegate.child;
  }
}

和卡头(实际上不是卡)

 Widget _cardHeader(String titleText, IconData inputIcon, String hint) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: standardEdgePadding),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(titleText, style: Consts.cardHeaderStyle,),
          Container(
            margin: EdgeInsets.only(top: 20),
            height: 40,
            decoration: BoxDecoration(
              color: Consts.inputGreen,
              borderRadius: BorderRadius.all(Radius.circular(10))
            ),
            child: Row(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 10),
                  child: Icon(
                    inputIcon,
                    color: Consts.inputGreenText,
                    size: Consts.cardInputIconSize,
                  ),
                ),
                Flexible(child: TextField(
                  decoration: new InputDecoration.collapsed(
                    hintText: hint,
                    hintStyle: Consts.cardInputStyle,
                  ),
                ),)
              ],
            ),
          )
        ],
      ),
    );
  }
flutter dart flutter-layout
1个回答
0
投票

SliverPersistentHeader本身没有阴影,因此elevation效果不是来自您的SliverPersistentHeader。您的代码段中没有明确显示,但是我可以看到您的小部件树中有一个_cardHeader('People', Icons.search, 'Name')方法。我怀疑它在此方法返回的小部件树中包含Card小部件。

the Card widget Flutter documentation中所示,Card具有默认的非零elevation值,这可能在您的情况下蒙上阴影。查看小部件树中是否有任何Card小部件,并将其elevation参数设置为零。

© www.soinside.com 2019 - 2024. All rights reserved.