Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I crop an image in Flutter?

Tags:

flutter

dart

Let's say I have a rectangular, portrait image:

enter image description here

I'd like to crop it, such that it's rendered like this:

enter image description here

How can I do this in Flutter?

(I don't need to resize the image.)

(Image from https://flic.kr/p/nwXTDb)

like image 280
Seth Ladd Avatar asked Jun 21 '17 02:06

Seth Ladd


People also ask

How do I select and crop an image in Flutter?

The image_picker already can crop the image. You pass in the specified width and height for the image you want and the plugin actually crops the original image. _imageFile = ImagePicker. pickImage(source: source, maxWidth: 200.0, maxHeight: 300.0);


2 Answers

I would probably use a BoxDecoration with a DecorationImage. You can use the alignment and fit properties to determine how your image is cropped. You can use an AspectRatio widget if you don't want to hard code a height on the Container.

screenshot

import 'package:flutter/material.dart';  void main() {   runApp(new MaterialApp(     home: new MyHomePage(),   )); }  class MyHomePage extends StatelessWidget {    Widget build(BuildContext context) {     return new Scaffold(       appBar: new AppBar(         title: new Text("Image Crop Example"),       ),       body: new Center(         child: new AspectRatio(           aspectRatio: 487 / 451,           child: new Container(             decoration: new BoxDecoration(               image: new DecorationImage(                 fit: BoxFit.fitWidth,                 alignment: FractionalOffset.topCenter,                 image: new NetworkImage('https://i.stack.imgur.com/lkd0a.png'),               )             ),           ),         ),       ),     );   } } 
like image 179
Collin Jackson Avatar answered Sep 22 '22 09:09

Collin Jackson


You can also directly use the Image class with BoxFit and do something like:

new Image.asset(   stringToImageLocation,   fit: BoxFit.cover, ) 
like image 39
Mary Avatar answered Sep 24 '22 09:09

Mary