Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 使容器小部件垂直填充父对象_Flutter - Fatal编程技术网

Flutter 使容器小部件垂直填充父对象

Flutter 使容器小部件垂直填充父对象,flutter,Flutter,TL;DR需要容器来填充垂直空间,以便它可以充当ontap侦听器。我尝试过大多数解决方案,但似乎都不管用 所以我想做的是让我的容器填满垂直的空间,同时保持固定的宽度。其思想是使用手势ontap侦听器使容器透明。如果有人对不同的解决方案有更好的想法,请随时提出建议 Widget build(BuildContext context) { return new GestureDetector( onHorizontalDragUpdate: _move, onHorizontalDragEn

TL;DR需要容器来填充垂直空间,以便它可以充当ontap侦听器。我尝试过大多数解决方案,但似乎都不管用

所以我想做的是让我的容器填满垂直的空间,同时保持固定的宽度。其思想是使用手势ontap侦听器使容器透明。如果有人对不同的解决方案有更好的想法,请随时提出建议

Widget build(BuildContext context) {
return new GestureDetector(
  onHorizontalDragUpdate: _move,
  onHorizontalDragEnd: _handleDragEnd,
  child: new Stack(
    children: <Widget>[
      new Positioned.fill(           
        child: new Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: <Widget>[
            new Container(
              child: new IconButton(                          
                padding: new EdgeInsets.only(top: 16.0, bottom: 16.0, left: 24.0, right: 24.0),
                icon: new Icon(Icons.warning),
                color: Colors.black12,
                onPressed: () {},
              )
            ),
          ],
        ),
      ),
      new SlideTransition(
        position: new Tween<Offset>(
          begin:  Offset(0.0, 0.0),
          end: const Offset(-0.6, 0.0),
        ).animate(_animation),
        child: new Card(
          child: new Row(
            children: <Widget>[
              new Container(
                width: 20.0,
                height: 20.0,
                color: Colors.amber,
              ),
              new Expanded(
                child: new Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[

                    _getListTile(),
                    _ifStoplineIsToBeShown()
                  ],
                ),
              )
            ],
          )
        ),
      ),
    ],
  )
);
小部件构建(构建上下文){
返回新的手势检测器(
水平排水更新:_移动,
在水平方向上:_handleDragEnd,
子:新堆栈(
儿童:[
新定位。填充(
孩子:新的一排(
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
新容器(
孩子:新的图标按钮(
填充:仅限新边集(顶部:16.0,底部:16.0,左侧:24.0,右侧:24.0),
图标:新图标(图标。警告),
颜色:颜色。黑色,
按下:(){},
)
),
],
),
),
新幻灯片转换(
职位:新吐温(
开始:偏移量(0.0,0.0),
结束:常数偏移(-0.6,0.0),
).制作动画(_动画),
孩子:新卡(
孩子:新的一排(
儿童:[
新容器(
宽度:20.0,
身高:20.0,
颜色:颜色。琥珀色,
),
新扩展(
子:新列(
mainAxisSize:mainAxisSize.min,
儿童:[
_getListTile(),
_ifStoplineIsToBeShown()
],
),
)
],
)
),
),
],
)
);
}

考虑到我已经尝试了很多不同的东西,但似乎没有任何效果,我很确定我错过了一些东西

我还上传了一幅带有调试图的图像


另外,我知道我已将高度设置为固定值,但这是显示容器的唯一方法。

诀窍是将
IntrinsicHeight
小部件和
crossAxisAlignment:crossAxisAlignment.stretch

这将强制行的子行垂直展开,但行将占用尽可能少的垂直空间

Card(
  child: IntrinsicHeight(
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        Container(
          width: 20.0,
          color: Colors.amber,
        ),
        // Expanded(...)
      ],
    ),
  )
)
卡(
孩子:内在的(
孩子:排(
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
容器(
宽度:20.0,
颜色:颜色。琥珀色,
),
//扩展(…)
],
),
)
)

要将容器拉伸到父容器的全高,请使用属性
约束:BoxConstraints.expand()
in-container小部件。容器占据独立于子窗口小部件的完整空间

Container(
  color: Colors.green,
  child: Text("Flutter"),
  constraints: BoxConstraints.expand(),
)

关于容器的更多信息,请参考链接,从2020年1月起,最简单的方法是使用扩展的小部件

Expanded(flex: 1,
         child: Container(..),
            ),

我建议使用扩展小部件(这允许我们避免使用IntrinsicHeight小部件),将其与容器的对齐属性结合起来,从而使其正常工作,即使容器不是屏幕上唯一的容器

Expanded(
  child: Container(
    alignment: Alignment.center,
    child: Text('Your text', textAlign: TextAlign.center))),
这样还可以避免潜在的应用程序崩溃,这种情况经常发生在您意外地将小部件树的某些部分水平和垂直扩展到无穷大时(这就是为什么在许多情况下您无法使用BoxConstraints小部件)


您可以在此处阅读更多关于在颤振中传递约束的问题-必须阅读:

如果您想要一个容器来填充所有可用空间,您可以简单地使用:

width: double.infinity,
height: double.infinity
说明:

在flatter中,子小部件不能超过其父小部件施加的“布局约束”。在布局阶段,颤振引擎使用约束解算器将“越界”值自动更正为其父约束允许的值

例如,如果您有一个50x50的容器,并且对于其子容器,您传入另一个300x300的容器,则内部容器将自动更正为“不超过其父容器”,因此为50x50。因此,使用足够大的值将始终确保“填充父对象”

事实上,即使是
BoxConstraints.expand()
也在内部利用了相同的思想。如果打开
expand()
的源代码,您将看到:

  /// Creates box constraints that expand to fill another box constraints.
  ///
  /// If width or height is given, the constraints will require exactly the
  /// given value in the given dimension.
  const BoxConstraints.expand({
    double width,
    double height,
  }) : minWidth = width ?? double.infinity,
       maxWidth = width ?? double.infinity,
       minHeight = height ?? double.infinity,
       maxHeight = height ?? double.infinity;

因此,如果您完全确定要填充所有空格,您可以直观地输入一个大于父项(或大于整个屏幕)的数字,如
double.infinity

有许多答案建议使用两种方法

  • 约束:BoxConstraints.expand()
  • 高度:双无限
  • 但这两个答案都会给你一个错误,比如

    BoxConstraints强制无限高

    我们可以通过计算屏幕的高度来避免这些问题

  • 应用程序栏
  • 顶栏空间(位于上面的应用程序栏上)
  • 剩余屏幕
  • 1。获取MediaQuery

     final mediaQuery = MediaQuery.of(context);
    
    2。声明AppBar小部件,并在Scaffold App Bar中使用相同的App Bar实例

    final PreferredSizeWidget appBar = AppBar(
          title: Text('Home'),
        );
    
    3。使用计算高度

          Container(
                  width: mediaQuery.size.width,
                  height: (mediaQuery.size.height -
                      appBar.preferredSize.height -
                      mediaQuery.padding.top),
                  color: Colors.red,
                ),
    
    输出:


    将容器的高度或宽度设置为double.maxFinite

    Container(
       height: double.maxFinite,
        width: 100,)
    

    您可以使小部件具有容器小部件的完整大小,然后将容器的高度和/或宽度设置为double.maxFinite。这将使容器具有高度和/或宽度,或者其父窗口小部件未被调用?您的“固定宽度”在哪里?是否应该提到IntrinsicHeight对性能的影响?我不这么认为。这里的警告主要是警告您在
    ConstraintBox
    为enoug时不要使用它