avatarSamra Khan

Summarize

Mastering Color Pickers in Flutter: From Basic to Advanced Solutions

Creating a beautiful color picker in Flutter can be an engaging and rewarding task. Flutter provides various widgets and packages that can be leveraged to implement a feature-rich color picker. In this article, we will explore different approaches to designing and implementing a color picker, covering both basic and advanced solutions.

1. Basic Color Picker with ColorPicker Package:

Flutter offers a package named color_picker that simplifies the process of creating a basic color picker. To get started, add the package to your pubspec.yaml file:

dependencies:
  color_picker: ^0.6.0

Now, let’s create a simple color picker:

import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ColorPickerScreen(),
    );
  }
}

class ColorPickerScreen extends StatefulWidget {
  @override
  _ColorPickerScreenState createState() => _ColorPickerScreenState();
}

class _ColorPickerScreenState extends State<ColorPickerScreen> {
  Color selectedColor = Colors.blue;

  void changeColor(Color color) {
    setState(() => selectedColor = color);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Basic Color Picker'),
      ),
      body: Center(
        child: ColorPicker(
          pickerColor: selectedColor,
          onColorChanged: changeColor,
          showLabel: true,
          pickerAreaHeightPercent: 0.8,
        ),
      ),
    );
  }
}

This basic example provides a simple color picker with a color-changing callback.

2. Customized Color Picker:

For a more customized experience, you can create your color picker using Flutter’s built-in widgets. Let’s implement a slider-based color picker:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ColorPickerScreen(),
    );
  }
}

class ColorPickerScreen extends StatefulWidget {
  @override
  _ColorPickerScreenState createState() => _ColorPickerScreenState();
}

class _ColorPickerScreenState extends State<ColorPickerScreen> {
  double red = 0.0;
  double green = 0.0;
  double blue = 0.0;

  void updateColor() {
    // Use red, green, and blue values to update the color
    Color selectedColor = Color.fromRGBO(red.toInt(), green.toInt(), blue.toInt(), 1.0);
    // Perform actions with the selected color
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Customized Color Picker'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Red'),
            Slider(
              value: red,
              min: 0.0,
              max: 255.0,
              onChanged: (value) {
                setState(() => red = value);
                updateColor();
              },
            ),
            Text('Green'),
            Slider(
              value: green,
              min: 0.0,
              max: 255.0,
              onChanged: (value) {
                setState(() => green = value);
                updateColor();
              },
            ),
            Text('Blue'),
            Slider(
              value: blue,
              min: 0.0,
              max: 255.0,
              onChanged: (value) {
                setState(() => blue = value);
                updateColor();
              },
            ),
          ],
        ),
      ),
    );
  }
}

In this example, sliders for red, green, and blue values are used to create a customized color picker.

3. Advanced Color Picker with flutter_colorpicker Package:

The flutter_colorpicker package also offers advanced features like opacity control and custom color palettes. To use this package:

dependencies:
  flutter_colorpicker: ^0.4.0

Now, create an advanced color picker:

import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ColorPickerScreen(),
    );
  }
}

class ColorPickerScreen extends StatefulWidget {
  @override
  _ColorPickerScreenState createState() => _ColorPickerScreenState();
}

class _ColorPickerScreenState extends State<ColorPickerScreen> {
  Color selectedColor = Colors.blue;

  void changeColor(Color color) {
    setState(() => selectedColor = color);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Advanced Color Picker'),
      ),
      body: Center(
        child: ColorPicker(
          pickerColor: selectedColor,
          onColorChanged: changeColor,
          showLabel: true,
          pickerAreaHeightPercent: 0.8,
          enableAlpha: true,
        ),
      ),
    );
  }
}

This advanced color picker includes an opacity control slider (enableAlpha), providing a more comprehensive color selection experience.

In conclusion, Flutter offers various solutions for implementing color pickers, ranging from basic to advanced. The choice depends on your project requirements and design preferences. Feel free to customize and combine features from different examples to create a beautiful and functional color picker for your Flutter app.

If you enjoyed this article, consider trying out the AI service I recommend. It provides the same performance and functions to ChatGPT Plus(GPT-4) but more cost-effective, at just $6/month (Special offer for $1/month). Click here to try ZAI.chat.

Color Picker
Flutter
Advance Color Picker
Flutter App Development
Flutter Color
Recommended from ReadMedium