• Flutter 获取本地图片并剪切


    安装依赖

    dependencies:
      ...
      image_picker:
      image_cropper
    

    androidappsrcmainAndroidManifest.xml

    将UCropActivity添加到AndroidManifest.xml中

     <activity android:name="com.yalantis.ucrop.UCropActivity" android:screenOrientation="portrait" android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
    

    androidgradle.properties

    添加AndroidX兼容

    org.gradle.jvmargs=-Xmx1536M
    android.enableJetifier=true
    android.useAndroidX=true
    

    libmain.dart

    import 'package:flutter/material.dart';
    import 'dart:async';
    import 'dart:io';
    
    import 'package:image_cropper/image_cropper.dart';
    import 'package:image_picker/image_picker.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'ImageCropper',
          home: MyHomePage(
            title: 'ImageCropper',
          ),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      final String title;
    
      MyHomePage({this.title});
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    enum AppState {
      free,
      picked,
      cropped,
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      AppState state;
      File imageFile;
    
      @override
      void initState() {
        super.initState();
        state = AppState.free;
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: imageFile != null ? Image.file(imageFile) : Container(),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              if (state == AppState.free)
                _pickImage();
              else if (state == AppState.picked)
                _cropImage();
              else if (state == AppState.cropped) _clearImage();
            },
            child: _buildButtonIcon(),
          ),
        );
      }
    
      Widget _buildButtonIcon() {
        if (state == AppState.free)
          return Icon(Icons.add);
        else if (state == AppState.picked)
          return Icon(Icons.crop);
        else if (state == AppState.cropped)
          return Icon(Icons.clear);
        else
          return Container();
      }
    
      Future<Null> _pickImage() async {
        imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
        if (imageFile != null) {
          setState(() {
            state = AppState.picked;
          });
        }
      }
    
      Future<Null> _cropImage() async {
        File croppedFile = await ImageCropper.cropImage(
          sourcePath: imageFile.path,
          toolbarTitle: 'Cropper',
          toolbarColor: Colors.blue,
          toolbarWidgetColor: Colors.white,
        );
        if (croppedFile != null) {
          imageFile = croppedFile;
          setState(() {
            state = AppState.cropped;
          });
        }
      }
    
      Future<Null> _saveImage() async {}
    
      void _clearImage() {
        imageFile = null;
        setState(() {
          state = AppState.free;
        });
      }
    }
    

    更多

  • 相关阅读:
    percona-toolkit 之 【pt-heartbeat】说明
    sql-索引的作用(超详细)
    SqlServer2005 海量数据 数据表分区解决难题
    怎样玩转千万级别的数据(表分区)
    SQL数据缓存依赖总结
    关于缓存中Cookie,Session,Cache的使用
    Anti-Forgery Request Recipes For ASP.NET MVC And AJAX
    jquery 父、子页面之间页面元素的获取,方法的调用
    win7系统部分软件显示乱码怎么办
    转:mvc 当中 [ValidateAntiForgeryToken] 的作用
  • 原文地址:https://www.cnblogs.com/ajanuw/p/11298775.html
Copyright © 2020-2023  润新知