flutter_remove_bg 0.0.1 copy "flutter_remove_bg: ^0.0.1" to clipboard
flutter_remove_bg: ^0.0.1 copied to clipboard

A Flutter package that removes background from images using remove.bg API. Simple to use with support for both local files and image URLs.

example/lib/main.dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_remove_bg/flutter_remove_bg.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Remove.bg Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _removeBg =
      RemoveBg(apiKey: '5cV3rMuZdohj5bfdbJokk4Zf'); // Replace with your API key
  File? _originalImage;
  File? _processedImage;
  bool _isProcessing = false;

  Future<void> _pickAndProcessImage() async {
    try {
      final imagePicker = ImagePicker();
      final pickedFile =
          await imagePicker.pickImage(source: ImageSource.gallery);

      if (pickedFile == null) return;

      setState(() {
        _originalImage = File(pickedFile.path);
        _processedImage = null;
        _isProcessing = true;
      });

      // Process the image
      final processedImageBytes =
          await _removeBg.removeBackground(pickedFile.path);

      // Save the processed image
      final tempDir = await getTemporaryDirectory();
      final processedImageFile = File('${tempDir.path}/processed_image.png');
      await processedImageFile.writeAsBytes(processedImageBytes);

      setState(() {
        _processedImage = processedImageFile;
        _isProcessing = false;
      });
    } catch (e) {
      setState(() {
        _isProcessing = false;
      });
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Error: $e')),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Remove.bg Example'),
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              if (_originalImage != null) ...[
                const Text('Original Image:'),
                Image.file(
                  _originalImage!,
                  height: 200,
                ),
                const SizedBox(height: 20),
              ],
              if (_isProcessing)
                const CircularProgressIndicator()
              else if (_processedImage != null) ...[
                const Text('Processed Image:'),
                Image.file(
                  _processedImage!,
                  height: 200,
                ),
              ],
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _isProcessing ? null : _pickAndProcessImage,
        tooltip: 'Pick Image',
        child: const Icon(Icons.add_photo_alternate),
      ),
    );
  }
}
1
likes
0
points
140
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package that removes background from images using remove.bg API. Simple to use with support for both local files and image URLs.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, http, path

More

Packages that depend on flutter_remove_bg