Zinbo
Zinbo

Reputation: 57

How can I make an image bigger than its container?

I have a grid of images like this:
grid list

However the pngs have quite a lot of whitespace around them, so I want to scale the image up so that the white space is cut out of the container.

How can I achieve this?

What I currently have:

body: new Container(
          child: GridView.count(
              crossAxisCount: 4,
          children: <Widget>[
          new Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                    image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                  fit: BoxFit.cover
                )
              )
          ),
            new Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                        fit: BoxFit.cover
                    )
                )
            ),
            new Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                        fit: BoxFit.cover
                    )
                )
            ),
            new Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                        fit: BoxFit.cover
                    )
                )
            ),
            new Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                        fit: BoxFit.cover
                    )
                )
            ),
            new Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                        fit: BoxFit.cover
                    )
                )
            )
          ]),

         )

Upvotes: 1

Views: 223

Answers (2)

chunhunghan
chunhunghan

Reputation: 54447

You can copy paste run full code below
You can use Transform.scale, with the example use scale 3.0, you can see image size is over original ConstrainedBox

Transform.scale(
                scale: 3.0,
                child: new Container(
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                            fit: BoxFit.cover
                        )
                    )
                ),
              ),

working demo

enter image description here

full code

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        child: GridView.count(
            crossAxisCount: 4,
            children: <Widget>[
              Transform.scale(
                scale: 3.0,
                child: new Container(
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                            fit: BoxFit.cover
                        )
                    )
                ),
              ),
              Transform.scale(
                scale: 2.0,
                child: new Container(
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                            fit: BoxFit.cover
                        )
                    )
                ),
              ),
              Transform.scale(
                scale: 1.5,
                child: new Container(
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                            fit: BoxFit.cover
                        )
                    )
                ),
              ),
              Transform.scale(
                scale: 1.0,
                child: new Container(
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                            fit: BoxFit.cover
                        )
                    )
                ),
              ),
              new Container(
                  decoration: BoxDecoration(
                      image: DecorationImage(
                          image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                          fit: BoxFit.cover
                      )
                  )
              ),
              new Container(
                  decoration: BoxDecoration(
                      image: DecorationImage(
                          image: NetworkImage("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"),
                          fit: BoxFit.cover
                      )
                  )
              )
            ]),

      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Upvotes: 1

yurianxdev
yurianxdev

Reputation: 51

You can use OverflowBox, don't forget set fit: cover to your Image constructor.

Container(
  child: OverflowBox(
    minWidth: 0.0, 
    minHeight: 0.0, 
    maxWidth: double.infinity, 
    child: Image.asset(
      your_asset, 
      fit: BoxFit.cover,),),
),

Upvotes: 0

Related Questions