pe_sdk_flutter 0.0.3
pe_sdk_flutter: ^0.0.3 copied to clipboard
Photo Editor Plugin
example/lib/main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pe_sdk_flutter/export_result.dart';
import 'package:pe_sdk_flutter/pe_sdk_flutter.dart';
const _licenceToken = SET YOUR LICENCE TOKEN;
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _peSdkFlutterPlugin = PeSdkFlutter();
String _errorMessage = '';
String _startMessage = 'Please start Photo Editor';
String? _imagePath;
Future<void> _startPhotoEditorFromGallery() async {
try {
final exportResult =
await _peSdkFlutterPlugin.openGalleryScreen(_licenceToken);
_handleExportResult(exportResult);
} on PlatformException catch (e) {
_handlePlatformException(e);
}
}
Future<void> _startPhotoEditorWithImagePath() async {
final ImagePicker picker = ImagePicker();
final imageFile = await picker.pickImage(source: ImageSource.gallery);
final sourceImageFile = imageFile?.path;
if (sourceImageFile == null) {
debugPrint('Error: Cannot start photo editor: please pick an image file');
return;
}
try {
final exportResult = await _peSdkFlutterPlugin.openEditorScreen(
_licenceToken, sourceImageFile);
_handleExportResult(exportResult);
} on PlatformException catch (e) {
_handlePlatformException(e);
}
}
void _handleExportResult(ExportResult? result) {
if (result == null) {
debugPrint(
'No export result! The user has closed photo editor before export');
return;
}
debugPrint('Exported photo file = ${result.photoSource}');
setState(() {
_imagePath = result.photoSource;
});
}
void _handlePlatformException(PlatformException exception) {
debugPrint("Error: code = ${exception.code}, message = $_errorMessage");
setState(() {
_errorMessage = exception.message ?? 'unknown error';
_startMessage = 'See the logs or try again';
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin Photo Editor example'),
),
body: Center(
child: Padding(
padding: EdgeInsets.all(25.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Visibility(
visible: _errorMessage.isNotEmpty,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: Text(
_errorMessage,
textAlign: TextAlign.center,
style:
const TextStyle(fontSize: 17.0, color: Colors.red),
))),
),
Expanded(
flex: 2,
child: _imagePath != null
? Image.memory(
File(_imagePath!).readAsBytesSync(),
)
: Center(
child: Text(
_startMessage,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 17.0),
),
)),
Expanded(
flex: 1,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.all(10.0),
child: MaterialButton(
color: Colors.blue,
textColor: Colors.white,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: const EdgeInsets.all(12.0),
splashColor: Colors.blueAccent,
minWidth: 240,
onPressed: () =>
_startPhotoEditorFromGallery(),
child:
Text("Start Photo Editor From Gallery"))),
Padding(
padding: EdgeInsets.all(10.0),
child: MaterialButton(
color: Colors.blue,
textColor: Colors.white,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: const EdgeInsets.all(12.0),
splashColor: Colors.blueAccent,
minWidth: 240,
onPressed: () =>
_startPhotoEditorWithImagePath(),
child:
Text("Start Photo Editor From Editor"))),
],
),
)
)
],
),
),
),
),
);
}
}