颤振布局容器边距

M. *_*erg 12 layout containers margin dart flutter

我的Flutter布局有问题.

我有一个简单的容器,右边和左边的边距为20.0.在这个容器内,我有另一个容器.

但是这个容器只适合左侧的父容器.我不知道为什么会这样.

这是我的代码:

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.white,
      body: new Container(
        margin: new EdgeInsets.symmetric(horizontal: 20.0),
        child: new Container(

        )
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

问题的截图

小智 35

你可以使用左右值:)

@override
      Widget build(BuildContext context) {
        return new Scaffold(
          backgroundColor: Colors.white,
          body: new Container(
           margin: const EdgeInsets.only(left: 20.0, right: 20.0),
            child: new Container(

            )
          ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

  • 也可以使用“ margin:EdgeInsets.fromLTRB(20.0,0.0,20.0,0.0)”。 (2认同)
  • @Cold_Class `const` 是一个关键字,指示构造函数是 const 构造函数,即它只能采用编译时值,而不是像某些变量那样在运行时计算的值。 (2认同)

vin*_*dav 21

您可以尝试:到任意一条边的边距

new Container(
    margin: const EdgeInsets.only(left: 20.0, right: 20.0),
    child: new Container()
)
Run Code Online (Sandbox Code Playgroud)

您可以尝试:到任何所有边缘的边距

new Container(
    margin: const EdgeInsets.all(20.0),
    child: new Container()
)
Run Code Online (Sandbox Code Playgroud)

如果您需要当前系统填充或小部件上下文中的视图插入,请考虑使用 [MediaQuery.of] 获取这些值,而不是使用来自 [dart:ui.window] 的值,以便您收到更改通知。

new Container(
    margin: EdgeInsets.fromWindowPadding(padding, devicePixelRatio),
    child: new Container()
)
Run Code Online (Sandbox Code Playgroud)


小智 6

Container(
  margin: EdgeInsets.all(10) ,
  alignment: Alignment.bottomCenter,
  decoration: BoxDecoration(
    gradient: LinearGradient(
      begin: Alignment.topCenter,
      end: Alignment.bottomCenter,
      colors: <Color>[
        Colors.black.withAlpha(0),
        Colors.black12,
        Colors.black45
      ],
    ),
  ),
  child: Text(
    "Foreground Text",
    style: TextStyle(color: Colors.white, fontSize: 20.0),
  ),
),
Run Code Online (Sandbox Code Playgroud)