How do I crop an image in Flutter?

后端 未结 5 1102
长发绾君心
长发绾君心 2020-12-03 03:09

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

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

How can I do this in Flutter?

相关标签:
5条回答
  • 2020-12-03 03:54

    Take a look to brendan-duncan/image, it's platform-independent library to manipulate images in Dart.

    You can use the function:

    Image copyCrop(Image src, int x, int y, int w, int h);
    
    0 讨论(0)
  • 2020-12-03 03:55

    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.

    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'),
                  )
                ),
              ),
            ),
          ),
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-03 03:57
    import 'dart:io';
    import 'package:flutter/material.dart';
    import 'package:image_picker/image_picker.dart';
    import 'package:image_cropper/image_cropper.dart';
    
    class MyPage extends StatefulWidget {
      @override
      _MyPageState createState() => _MyPageState();
    }
    
    class _MyPageState extends State<MyPage> {
      /// Variables
      File imageFile;
    
      /// Widget
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              backgroundColor: Color(0XFF307777),
              title: Text("Image Cropper"),
            ),
            body: Container(
                child: imageFile == null
                    ? Container(
                        alignment: Alignment.center,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            RaisedButton(
                              color: Color(0XFF307777),
                              onPressed: () {
                                _getFromGallery();
                              },
                              child: Text(
                                "PICK FROM GALLERY",
                                style: TextStyle(color: Colors.white),
                              ),
                            ),
                          ],
                        ),
                      )
                    : Container(
                        child: Image.file(
                          imageFile,
                          fit: BoxFit.cover,
                        ),
                      )));
      }
    
      /// Get from gallery
      _getFromGallery() async {
        PickedFile pickedFile = await ImagePicker().getImage(
          source: ImageSource.gallery,
          maxWidth: 1800,
          maxHeight: 1800,
        );
        _cropImage(pickedFile.path);
      }
    
      /// Crop Image
      _cropImage(filePath) async {
        File croppedImage = await ImageCropper.cropImage(
          sourcePath: filePath,
          maxWidth: 1080,
          maxHeight: 1080,
        );
        if (croppedImage != null) {
          imageFile = croppedImage;
          setState(() {});
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-03 03:59

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

    new Image.asset(
      stringToImageLocation,
      fit: BoxFit.cover,
    )
    
    0 讨论(0)
  • 2020-12-03 03:59

    There is a new package called ImageCropper. I would recommend everyone to use this instead as it has many features and makes everything easier. It allows you to crop the image to any or specified aspect ratio you want and can even compress the image. Here is the link to the package: https://pub.dev/packages/image_cropper

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