Flutter SDK Set Background image

后端 未结 10 1824
礼貌的吻别
礼貌的吻别 2020-11-30 20:38

I am trying to set a background image for the home page. I am getting the image place from start of the screen and filling the width but not the height. Am I missing someth

10条回答
  •  情书的邮戳
    2020-11-30 20:53

    If you use a Container as the body of the Scaffold, its size will be accordingly the size of its child, and usually that is not what you want when you try to add a background image to your app.

    Looking at this other question, @collin-jackson was also suggesting to use Stack instead of Container as the body of the Scaffold and it definitely does what you want to achieve.

    This is how my code looks like

    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        body: new Stack(
          children: [
            new Container(
              decoration: new BoxDecoration(
                image: new DecorationImage(image: new AssetImage("images/background.jpg"), fit: BoxFit.cover,),
              ),
            ),
            new Center(
              child: new Text("Hello background"),
            )
          ],
        )
      );
    }
    

提交回复
热议问题