Flutter SDK Set Background image

后端 未结 10 1626
礼貌的吻别
礼貌的吻别 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 21:11

    We can use Container and mark its height as infinity

    body: Container(
          height: double.infinity,
          width: double.infinity,
          child: FittedBox(
            fit: BoxFit.cover,
            child: Image.network(
              'https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg',
            ),
          ),
        ));
    

    Output:

    0 讨论(0)
  • 2020-11-30 21:14

    enter link description here

    Use FractionallySizedBox widget to set a background image (full screen) for your flutter(along with scaffold and safearea) app.

    0 讨论(0)
  • 2020-11-30 21:17

    I'm not sure I understand your question, but if you want the image to fill the entire screen you can use a DecorationImage with a fit of BoxFit.cover.

    class BaseLayout extends StatelessWidget{
      @override
      Widget build(BuildContext context){
        return Scaffold(
          body: Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage("assets/images/bulb.jpg"),
                fit: BoxFit.cover,
              ),
            ),
            child: null /* add child content here */,
          ),
        );
      }
    }
    

    For your second question, here is a link to the documentation on how to embed resolution-dependent asset images into your app.

    0 讨论(0)
  • 2020-11-30 21:18

    You can use Stack to make the image stretch to the full screen.

    Stack(
            children: <Widget>
            [
              Positioned.fill(  //
                child: Image(
                  image: AssetImage('assets/placeholder.png'),
                  fit : BoxFit.fill,
               ),
              ), 
              ...... // other children widgets of Stack
              ..........
              .............
             ]
     );
    

    Note: Optionally if are using a Scaffold, you can put the Stack inside the Scaffold with or without AppBar according to your needs.

    0 讨论(0)
提交回复
热议问题