dart - Flutter Positioned Transitioned to below other Stack child - 如何获得高度?

标签 dart flutter

我正在执行类似于 Codelabs flutter example here 的背景样式向下滑动.我希望前景部分停在背景内容的底部。

我知道在布局之后才能直接获取高度。由于应用程序从顶部的前景部分开始,我想我可以在构建后获取高度并将其存储在状态中,但我不确定如何执行此操作。

这就是我现在所拥有的,只是使用不考虑背景内容高度的预定义“悬垂”:

Widget _buildForeground(BuildContext context, BoxConstraints constraints) {
    Animation<RelativeRect> rectAnimation = new RelativeRectTween(
      begin: new RelativeRect.fromLTRB(0.0, constraints.maxHeight - widget.overhang, 0.0, 0.0),
      end: new RelativeRect.fromLTRB(0.0, 0.0, 0.0, 0.0),
    ).animate(_controller);
    return new PositionedTransition(
      rect: rectAnimation,
      child: new Material(
        shape: RoundedRectangleBorder(borderRadius: new BorderRadius.only(topLeft: new Radius.circular(15.0), topRight: new Radius.circular(15.0))),
        elevation: 16.0,
        child: widget.foreground,
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    return new LayoutBuilder(
      builder: (context, constraints) => new Stack(
        children: <Widget>[
          new Container(color: Theme.of(context).primaryColor,),
          widget.background,
          _buildForeground(context, constraints),
        ],
      ),
    );
  }

最佳答案

感谢 Remi,我使用 GlobalKey 想出了这个解决方案

void _toggleForeground() {
  setState(() => top = backgroundKey?.currentContext?.size?.height);
  _controller.fling(velocity: _isBackgroundVisible ? -widget.toggleVelocity : widget.toggleVelocity);
}

Widget _buildForeground() {
  if (top == null) top = 500.0;
  Animation<RelativeRect> rectAnimation = new RelativeRectTween(
    begin: new RelativeRect.fromLTRB(0.0, top, 0.0, 0.0),
    end: new RelativeRect.fromLTRB(0.0, 0.0, 0.0, 0.0),
  ).animate(_controller);
  return new PositionedTransition(
    rect: rectAnimation,
    child: new Material(
      shape: RoundedRectangleBorder(borderRadius: new BorderRadius.only(topLeft: new Radius.circular(15.0), topRight: new Radius.circular(15.0))),
      elevation: 16.0,
      child: widget.foreground,
    )
  );
}

@override
Widget build(BuildContext context) {
  return new Stack(
      children: <Widget>[
        new Container(color: Theme.of(context).primaryColor),
        new Column(
          key: backgroundKey,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[widget.background],
        ),
        _buildForeground(),
    ],
  );
}

关于dart - Flutter Positioned Transitioned to below other Stack child - 如何获得高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50305780/

相关文章:

firebase - flutter firebase 使用 future 从数据库获取字符串

android - Flutter - 如何滚动到 ListView 的底部?

flutter - 如何在 Flutter 中将图像 url 转换为 XFile?

dart - 如何更改 InkResponse 小部件中的 highlightShape 大小

pointers - Dart编程中的链接列表,如何在Dart中处理指针或如何在堆上存储变量?

flutter - 我如何阅读从最后一个项目到第一个项目的 list

dart - 如何使 Dart/ Aqueduct 永久运行

flutter - 如何将带有主题 [FLUTTER] 的 AppBar 的标题居中

flutter - 如何使用itemBuilder选择最后点击的ListTile小部件?

dart - Navigator.push() 的类型无法签名到 onPressed