image_crop 0.1.2
image_crop: ^0.1.2 copied to clipboard
A flutter plugin to crop image on iOS and Android. It processes image files off main thread natively. The plugin provides a Crop widget to display image cropping to a user.
example/lib/main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_crop/image_crop.dart';
// Hidden import to let `flutter packages pub publish --dry-run` complete without errors
// FIXME: uncomment to try out example code
// import 'package:image_picker/image_picker.dart';
void main() {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarBrightness: Brightness.dark,
statusBarIconBrightness: Brightness.light,
systemNavigationBarIconBrightness: Brightness.light,
));
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
final cropKey = GlobalKey<CropState>();
File _file;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: SafeArea(
child: Container(
color: Colors.black,
padding: const EdgeInsets.symmetric(vertical: 40.0, horizontal: 20.0),
child: _file == null ? _buildOpeningImage() : _buildCroppingImage(),
),
),
);
}
Widget _buildOpeningImage() {
return Center(child: _buildOpenImage());
}
Widget _buildCroppingImage() {
return Column(
children: <Widget>[
Expanded(
child: Crop.file(
_file,
key: cropKey,
),
),
Container(
padding: const EdgeInsets.only(top: 20.0),
alignment: AlignmentDirectional.center,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
FlatButton(
child: Text(
'Crop Image',
style: Theme.of(context)
.textTheme
.button
.copyWith(color: Colors.white),
),
onPressed: () => _cropImage(),
),
_buildOpenImage(),
],
),
)
],
);
}
Widget _buildOpenImage() {
return FlatButton(
child: Text(
'Open Image',
style: Theme.of(context).textTheme.button.copyWith(color: Colors.white),
),
onPressed: () => _openImage(),
);
}
Future<void> _openImage() async {
final file = await ImagePicker.pickImage(source: ImageSource.gallery);
final sample = await ImageCrop.sampleImage(file: file, preferredSize: 2000);
setState(() {
_file = sample;
});
}
Future<void> _cropImage() async {
final scale = cropKey.currentState.scale;
final area = cropKey.currentState.area;
if (area == null) {
// cannot crop, widget is not setup
return;
}
final file =
await ImageCrop.cropImage(file: _file, area: area, scale: scale);
debugPrint('$file');
}
}