Want to crop an image in Flutter? 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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'), ) ), ), ), ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.