【问题标题】:Flutter: Flutter: How to make finest this Slivers efectFlutter:Flutter:如何使这种 Slivers 效果最好
【发布时间】:2020-11-23 19:38:22
【问题描述】:

首先对不起我的英语不好,我会尽力解释自己

我从列表中创建了一个项目,效果如下:

当我滚动以填充下部字段时,图像大小将减小到最小高度,扁平按钮字体大小和扁平按钮不透明度也一样。

问题是:如何让效果更平滑,并且按钮始终与图像保持相同的距离?

这是代码:

SliverPersistentHeader makeHeader(bool pinned) {
    return SliverPersistentHeader(
      pinned: pinned,
      floating: true,
      delegate: _SliverAppBarDelegate(
        minHeight: 60.0,
        maxHeight: 200.0,
      ),
    );
  }

_SliverAppBarDelegate:

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  final double minHeight;
  final double maxHeight;
  final bool hideButtonWhenExpanded;

  _SliverAppBarDelegate(
      {@required this.minHeight,
      @required this.maxHeight,
      this.hideButtonWhenExpanded = true});

  @override
  double get minExtent => minHeight;
  @override
  double get maxExtent => math.max(maxHeight, minHeight);
  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    final appBarSize = maxHeight - shrinkOffset;
    final proportion = 2 - (maxHeight / appBarSize);
    final photoToButton = 160 * proportion;
    final percent = proportion < 0 || proportion > 1 ? 0.0 : proportion;
    return new SizedBox.expand(
      child: Container(
        color: Colors.white,
        child: Stack(
          alignment: Alignment.topCenter,
          children: <Widget>[
            Positioned(
              top: 10.0,
              child: CircleAvatar(
                minRadius: 20.0,
                maxRadius: 75.0 * proportion > 20 ? 75.0 * proportion : 20.0,
                backgroundImage: NetworkImage(
                    'https://t3.ftcdn.net/jpg/02/33/46/24/240_F_233462402_Fx1yke4ng4GA8TJikJZoiATrkncvW6Ib.jpg'),
              ),
            ),
            Positioned(
              left: 0.0,
              right: 0.0,
              top: photoToButton,
              child: Opacity(
                opacity: percent,
                child: FlatButton(
                  onPressed: () {},
                  child: Text(
                    'Add Photo',
                    style: TextStyle(
                        color: Colors.blue, fontSize: 14.0 * proportion),
                  ),
                ),
              ),
            ),
            Positioned(
              left: 0.0,
              right: 0.0,
              top: appBarSize - 1.0 > 59.0 ? appBarSize - 1 : 59.0,
              child: const Divider(
                height: 1,
                thickness: 0.5,
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
            minHeight !=
                oldDelegate
                    .minHeight
        ;
  }
}

我会感谢所有可能的帮助

【问题讨论】:

    标签: flutter flutter-sliver sliverappbar


    【解决方案1】:

    您可以在_SliverAppBarDelegate 中使用FlexFlexible

      @override
      Widget build(
          BuildContext context, double shrinkOffset, bool overlapsContent) {
        final appBarSize = maxHeight - shrinkOffset;
        final proportion = 2 - (maxHeight / appBarSize);
        final photoToButton = 160 * proportion;
        final percent = proportion < 0 || proportion > 1 ? 0.0 : proportion;
        return Flex(
          direction: Axis.vertical,
          children: <Widget>[
            Flexible(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Flexible(
                    flex: 8,
                    child: CircleAvatar(
                      minRadius: 20.0,
                      maxRadius: 75.0 * proportion > 20 ? 75.0 * proportion : 20.0,
                      backgroundImage: NetworkImage(
                          'https://t3.ftcdn.net/jpg/02/33/46/24/240_F_233462402_Fx1yke4ng4GA8TJikJZoiATrkncvW6Ib.jpg'),
                    ),
                  ),
                  Flexible(
                    flex: 2,
                    child: Opacity(
                      opacity: percent,
                      child: FlatButton(
                        onPressed: () {},
                        child: Text(
                          'Add Photo',
                          style: TextStyle(
                              color: Colors.blue, fontSize: 14.0 * proportion),
                        ),
                      ),
                    ),
                  ),
                  const Divider(
                    height: 1,
                    thickness: 0.5,
                  ),
                ],
              ),
            ),
          ],
        );
      }
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      相关资源
      最近更新 更多