native_image_cropper_android 0.3.0 copy "native_image_cropper_android: ^0.3.0" to clipboard
native_image_cropper_android: ^0.3.0 copied to clipboard

PlatformAndroid

This is the Android implementation of the native_image_cropper plugin.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:native_image_cropper_android/native_image_cropper_android.dart';
import 'package:native_image_cropper_android_example/button.dart';
import 'package:native_image_cropper_android_example/dropdown.dart';
import 'package:native_image_cropper_android_example/result.dart';
import 'package:native_image_cropper_android_example/themes.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  static const String image = 'sail-boat.png';

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _nativeImageCropperAndroidPlugin = NativeImageCropperAndroid();
  ImageFormat _format = ImageFormat.jpg;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: CustomThemes.theme(context),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Native Image Cropper Android Example'),
        ),
        body: FutureBuilder<Uint8List>(
          // ignore: discarded_futures, build method cannot be marked async.
          future: _getBytes(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final bytes = snapshot.data;
              if (bytes != null) {
                return Column(
                  children: [
                    Image(
                      image: MemoryImage(bytes),
                    ),
                    const SizedBox(
                      height: 20,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        ImageFormatDropdown(
                          onChanged: (value) => _format = value,
                        ),
                        RoundedIconButton(
                          onTap: () => unawaited(
                            _crop(
                              context: context,
                              bytes: bytes,
                              method: _nativeImageCropperAndroidPlugin.cropRect,
                            ),
                          ),
                          icon: const Icon(Icons.crop),
                        ),
                        RoundedIconButton(
                          shape: BoxShape.circle,
                          onTap: () => unawaited(
                            _crop(
                              context: context,
                              bytes: bytes,
                              method: _nativeImageCropperAndroidPlugin.cropOval,
                            ),
                          ),
                          icon: const Icon(Icons.crop),
                        ),
                      ],
                    ),
                  ],
                );
              }
            }
            return const Center(
              child: CircularProgressIndicator(),
            );
          },
        ),
      ),
    );
  }

  Future<void> _crop({
    required BuildContext context,
    required Uint8List bytes,
    required Future<Uint8List> Function({
      required Uint8List bytes,
      required int x,
      required int y,
      required int width,
      required int height,
      required ImageFormat format,
    }) method,
  }) async {
    final croppedBytes = await method(
      bytes: bytes,
      x: 1200,
      y: 900,
      width: 600,
      height: 600,
      format: _format,
    );

    if (context.mounted) {
      return Navigator.push<void>(
        context,
        MaterialPageRoute<ResultPage>(
          builder: (context) => ResultPage(
            bytes: croppedBytes,
            format: _format,
          ),
        ),
      );
    }
  }

  Future<Uint8List> _getBytes() async {
    final byteData = await rootBundle.load('assets/${MyApp.image}');
    return byteData.buffer.asUint8List();
  }
}
2
likes
140
pub points
45%
popularity

Publisher

verified publishercosee.biz

This is the Android implementation of the native_image_cropper plugin.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter, native_image_cropper_platform_interface

More

Packages that depend on native_image_cropper_android