crop_image 1.0.3 crop_image: ^1.0.3 copied to clipboard
An image cropper widget. Supports mobile, web and desktop.
import 'package:crop_image/crop_image.dart';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Crop Image Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(title: 'Crop Image Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({Key? key, required this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final controller = CropController(
aspectRatio: 1,
defaultCrop: const Rect.fromLTRB(0.1, 0.1, 0.9, 0.9),
);
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(6.0),
child: CropImage(
controller: controller,
image: Image.asset('assets/08272011229.jpg'),
),
),
),
bottomNavigationBar: _buildButtons(),
);
Widget _buildButtons() => Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
IconButton(
icon: const Icon(Icons.close),
onPressed: () {
controller.aspectRatio = 1.0;
controller.crop = const Rect.fromLTRB(0.1, 0.1, 0.9, 0.9);
},
),
IconButton(
icon: const Icon(Icons.aspect_ratio),
onPressed: _aspectRatios,
),
TextButton(
onPressed: _finished,
child: const Text('Done'),
),
],
);
Future<void> _aspectRatios() async {
final value = await showDialog<double>(
context: context,
builder: (context) {
return SimpleDialog(
title: const Text('Select aspect ratio'),
children: [
SimpleDialogOption(
onPressed: () => Navigator.pop(context, 1.0),
child: const Text('square'),
),
SimpleDialogOption(
onPressed: () => Navigator.pop(context, 2.0),
child: const Text('2:1'),
),
SimpleDialogOption(
onPressed: () => Navigator.pop(context, 4.0 / 3.0),
child: const Text('4:3'),
),
SimpleDialogOption(
onPressed: () => Navigator.pop(context, 16.0 / 9.0),
child: const Text('16:9'),
),
],
);
},
);
if (value != null) {
controller.aspectRatio = value;
controller.crop = const Rect.fromLTRB(0.1, 0.1, 0.9, 0.9);
}
}
Future<void> _finished() async {
final image = await controller.croppedImage();
await showDialog<bool>(
context: context,
builder: (context) {
return SimpleDialog(
contentPadding: const EdgeInsets.all(6.0),
titlePadding: const EdgeInsets.all(8.0),
title: const Text('Cropped image'),
children: [
Text('relative: ${controller.crop}'),
Text('pixels: ${controller.cropSize}'),
const SizedBox(height: 5),
image,
TextButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('OK'),
),
],
);
},
);
}
}