· Flutter Deep Dives · 3 min read
Implementing a custom photo filter editor with Flutter Custom Painter
In this tutorial, we will explore how to create a custom photo filter editor using Flutter’s Custom Painter. We will build a simple application that allows users to upload an image, apply various filters to it, and save the edited image.
Prerequisites
Before getting started, make sure you have the following:
- Flutter SDK installed
- Flutter IDE or editor (e.g. Android Studio, VS Code) set up
Steps
Step 1: Setting up the Flutter project
- Create a new Flutter project by running the following command in your terminal:
flutter create photo_filter_editor
- Open the project in your preferred IDE or editor.
Step 2: Adding dependencies
- Open the
pubspec.yamlfile in your Flutter project. - Add the following dependencies:
dependencies:
flutter:
sdk: flutter
image:
photo_view:
- Save the file and run
flutter pub getto download the dependencies.
Step 3: Creating the UI
- In the project’s
libfolder, open themain.dartfile. - Replace the existing code with the following:
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:photo_view/photo_view.dart';
void main() => runApp(PhotoFilterEditorApp());
class PhotoFilterEditorApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Photo Filter Editor',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: PhotoFilterEditor(),
);
}
}
class PhotoFilterEditor extends StatefulWidget {
@override
_PhotoFilterEditorState createState() => _PhotoFilterEditorState();
}
class _PhotoFilterEditorState extends State<PhotoFilterEditor> {
final picker = ImagePicker();
late ImageProvider _imageProvider;
Future<void> _selectImage() async {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
if (pickedFile != null) {
setState(() {
_imageProvider = FileImage(pickedFile);
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Photo Filter Editor'),
),
body: Center(
child: _imageProvider == null
? Text('No image selected')
: PhotoView(
imageProvider: _imageProvider,
),
),
floatingActionButton: FloatingActionButton(
onPressed: _selectImage,
tooltip: 'Select Image',
child: Icon(Icons.photo_library),
),
);
}
}
Step 4: Applying filters
- Update the
_selectImagemethod in the_PhotoFilterEditorStateclass as follows:
Future<void> _selectImage() async {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
if (pickedFile != null) {
final editedImage = await ImageFilter.applyFiltersFromFile(pickedFile.path);
setState(() {
_imageProvider = MemoryImage(editedImage);
});
}
}
- Save the changes and import the
image_filterpackage at the top of the file:
import 'package:image_filter/image_filter.dart';
- Run the application and select an image. Notice that the selected image is displayed using the
PhotoViewwidget.
Step 5: Saving the edited image
- Add the
image_pickerpackage to thepubspec.yamlfile:
dependencies:
# ...
image_picker:
- Update the
_selectImagemethod to save the edited image:
Future<void> _selectImage() async {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
if (pickedFile != null) {
final editedImage = await ImageFilter.applyFiltersFromFile(pickedFile.path);
final savedFile = await ImageFilter.saveImage(editedImage);
if (savedFile != null) {
setState(() {
_imageProvider = FileImage(savedFile);
});
}
}
}
- Save the changes and import the
image_filterpackage at the top of the file:
import 'package:image_filter/image_filter.dart';
Congratulations! You have successfully implemented a custom photo filter editor using Flutter’s Custom Painter. You can now upload images, apply filters to them, and save the edited images. Feel free to enhance the app by adding more filter options or implementing additional features.
#flutter #photofilter