简体   繁体   English

如何为 Flutter 中的主窗口小部件内的窗口小部件设置背景颜色?

[英]How to set a background color for a widget inside a main widget in Flutter?

I have a Container widget in which all my codes are in for the particular page.我有一个 Container 小部件,其中我的所有代码都在特定页面中。 It has a background image and inside the container, there is a Center widget.它有一个背景图像,在容器内,有一个 Center 小部件。 I want to set the background color of the center widget to white but while doing so, the entire screen's background is changing to white.我想将中心小部件的背景颜色设置为白色,但这样做时,整个屏幕的背景都变为白色。 How can I achieve that please?请问我怎样才能做到这一点?

return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage('Assets/images/loginbg2.jpg'),
          fit: BoxFit.cover
        ),
      ),
    child: Scaffold(
      backgroundColor: Colors.transparent, //code for background image



body: Center(
        child:Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Align(
              alignment: Alignment.topLeft,
              child: Padding(
                padding: EdgeInsets.fromLTRB(40, 0, 0, 0),
                child: Text(
                  "Welcome back!",
                  textAlign: TextAlign.left,
                  style: TextStyle(
                    fontSize: 30.0,
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                  ),
                ),
              )
            ),
            SizedBox(
              height: 20.0,
            ), //code for center widget

This is probably because the Column widget inside your Center widget is taking up the entire screen.这可能是因为 Center 小部件中的 Column 小部件占据了整个屏幕。

A Column widget (unless constrained) is taking up all the space it can get. Column 小部件(除非受到限制)正在占用它可以获得的所有空间。

Try solving your issue by removing the Column widget for now and only use the container inside the Center widget and define it's height and width.现在尝试通过删除 Column 小部件来解决您的问题,并且只使用 Center 小部件内的容器并定义它的高度和宽度。 See if the background you want is coming back in view.查看您想要的背景是否重新出现在视图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM