Applying Stunning Photo Filters with Flutter

May 15, 2024 | Programming

In the world of mobile app development, providing visually captivating content is essential to engage users. The Photo Filters package for Flutter allows developers to apply delightful filters to images on both iOS and Android platforms. Whether you’re looking for preset filters or want to create your own, this package has got you covered. Let’s dive into how to install and use this powerful tool!

Installation

To begin, you need to add the required dependencies to your Flutter project. Open your pubspec.yaml file and add the following lines:

dependencies:
  photofilters: ^version_number
  image: ^version_number

Replace version_number with the latest version available. Now you are ready to start filtering images!

Platform Configuration

Good news! No additional configuration is needed for both iOS and Android. The plugin works seamlessly out of the box, making it easier for you to focus on building your application.

Example Usage

Let’s set up a simple Flutter app that allows users to select an image from their gallery and apply filters. Think of creating this functionality as preparing a perfect smoothie. You gather the ingredients (image), blend them with different flavors (filters), and voila! Here’s the code:

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:photofilters/photofilters.dart';
import 'package:image/image.dart' as imageLib;
import 'package:image_picker/image_picker.dart';

void main() => runApp(new MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State {
  String fileName;
  List filters = presetFiltersList;
  File imageFile;

  Future getImage(BuildContext context) async {
    imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
    fileName = basename(imageFile.path);
    var image = imageLib.decodeImage(imageFile.readAsBytesSync());
    image = imageLib.copyResize(image, width: 600);
    
    Map imagefile = await Navigator.push(
      context,
      new MaterialPageRoute(
        builder: (context) => new PhotoFilterSelector(
          title: Text("Photo Filter Example"),
          image: image,
          filters: presetFiltersList,
          filename: fileName,
          loader: Center(child: CircularProgressIndicator()),
          fit: BoxFit.contain,
        ),
      ),
    );
    if (imagefile != null && imagefile.containsKey(image_filtered)) {
      setState(() {
        imageFile = imagefile[image_filtered];
      });
    }
    print(imageFile.path);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Photo Filter Example"),
      ),
      body: Center(
        child: new Container(
          child: imageFile == null
              ? Center(child: new Text("No image selected."))
              : Image.file(imageFile),
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: () => getImage(context),
        tooltip: "Pick Image",
        child: new Icon(Icons.add_a_photo),
      ),
    );
  }
}

In the code above, we start by importing necessary packages. Then we set up a simple app that allows users to pick an image from their gallery and select a filter to apply to it. Each stage of our process mirrors blending distinct ingredients to create something delightful!

Troubleshooting

  • If you encounter any errors while installing dependencies, double-check your pubspec.yaml for typos or incorrect versions.
  • If the app crashes when selecting an image, ensure your app has permission to access the photo library.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Sample Images of Filters

Here are some delightful examples of filters you can apply:

  • No Filter
  • Addictive Blue
  • Addictive Red
  • Aden

With the Photo Filters package, not only can you enhance user engagement with visually appealing images, but you can also tap into your creativity by customizing filters!

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox