How do I stretch an image to fit the whole background (100% height x 100% width) in Flutter?

后端 未结 12 1073
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 00:10

I have an image that doesn\'t match the aspect ratio of my device\'s screen. I want to stretch the image so that it fully fills the screen, and I don\'t want to crop any par

相关标签:
12条回答
  • 2020-12-01 00:21

    None of the above answers worked for me. And since there is no accepted answer, I found the following extended my image from horizontal edge to horizontal edge:

    Container ( width: MediaQuery
                        .of(context)
                        .size
                        .width,
                    child: 
                      Image.network(my_image_name, fit: BoxFit.fitWidth )
                  )
    
    0 讨论(0)
  • 2020-12-01 00:24

    This worked for me

    class _SplashScreenState extends State<SplashScreen> {
      @override
      Widget build(BuildContext context) {
        return Container(
          child: FittedBox(
            child: Image.asset("images/my_image.png"),
            fit: BoxFit.fill,
          ),);
      }
    }
    
    0 讨论(0)
  • 2020-12-01 00:26

    Your Question contains the first step, but you need width and height. you can get the width and height of the screen. Here is a small edit

    //gets the screen width and height
    double Width = MediaQuery.of(context).size.width;
    double Height = MediaQuery.of(context).size.height;
    
    Widget background = new Image.asset(
      asset.background,
      fit: BoxFit.fill,
      width: Width,
      height: Height,
    );
    
    return new Stack(
      children: <Widget>[
        background,
        foreground,
      ],
    );
    

    You can also use Width and Height to size other objects based on screen size.

    ex: width: Height/2, height: Height/2 //using height for both keeps aspect ratio

    0 讨论(0)
  • 2020-12-01 00:29

    Inside your Stack, you should wrap your background widget in a Positioned.fill.

    return new Stack(
      children: <Widget>[
        new Positioned.fill(
          child: background,
        ),
        foreground,
      ],
    );
    
    0 讨论(0)
  • 2020-12-01 00:38

    Try setting contentPadding

    ListTile(
      contentPadding: EdgeInsets.all(0.0),
      ...
    )
    
    0 讨论(0)
  • 2020-12-01 00:40

    For me, using Image(fit: BoxFit.fill ...) worked when in a bounded container.

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