Search code examples
flutterscaffold

Flutter : Fill the image over the body


I am working on a flutter project. I have loaded an image and I want it to fill up the body. After reading the documentation I came up with this:

void main() => runApp(MaterialApp(
  home : Scaffold(
      appBar: AppBar(
        title: Center(
           child: Text("I am rich"),
        ),
        backgroundColor: Colors.amberAccent,
      ),
      backgroundColor: Colors.purple,
      body: Image(
        fit: BoxFit.cover, 
        image: NetworkImage('https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQWqFc0Y7fRegMPpHoqNmD-hdba-3Zk_ttQQw&usqp=CAU'),
      ),
    )
  )
);

But it does not seem to work. Please help


Solution

  • You can copy paste run full code below
    You can directly set width and height to window.physicalSize.width and window.physicalSize.height
    And you can use BoxFit.fill, see effect below

    Image(
        fit: BoxFit.fill,
        width: window.physicalSize.width,
        height: window.physicalSize.height,
    

    working demo

    enter image description here

    full code

    import 'dart:ui';
    
    import 'package:flutter/material.dart';
    
    void main() => runApp(MaterialApp(
            home: Scaffold(
          appBar: AppBar(
            title: Center(
              child: Text("I am rich"),
            ),
            backgroundColor: Colors.amberAccent,
          ),
          backgroundColor: Colors.purple,
          body: Image(
            fit: BoxFit.fill,
            width: window.physicalSize.width,
            height: window.physicalSize.height,
            image: NetworkImage(
                'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQWqFc0Y7fRegMPpHoqNmD-hdba-3Zk_ttQQw&usqp=CAU'),
          ),
        )));