palette_generator_master 1.1.0 copy "palette_generator_master: ^1.1.0" to clipboard
palette_generator_master: ^1.1.0 copied to clipboard

A powerful Flutter package for extracting prominent colors from images with advanced features and accessibility compliance.

example/lib/main.dart

// Copyright 2024 Palette Generator Master Example. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'package:example/professional_example.dart';
import 'package:flutter/material.dart';
import 'package:palette_generator_master/palette_generator_master.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Palette Generator Master Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      home: const ProfessionalPaletteExample(),
      debugShowCheckedModeBanner: false,
    );
  }
}

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

  @override
  State<ImageColorsPage> createState() => _ImageColorsPageState();
}

class _ImageColorsPageState extends State<ImageColorsPage> {
  PaletteGeneratorMaster? paletteGenerator;
  bool isLoading = false;
  String? errorMessage;

  final String targetAsset = 'assets/eva.png';

  @override
  void initState() {
    super.initState();
    _updatePaletteGenerator();
  }

  Future<void> _updatePaletteGenerator() async {
    setState(() {
      isLoading = true;
      errorMessage = null;
    });

    try {
      final PaletteGeneratorMaster generator =
          await PaletteGeneratorMaster.fromImageProvider(
            AssetImage(targetAsset),
            maximumColorCount: 20,
          );

      setState(() {
        paletteGenerator = generator;
        isLoading = false;
      });
    } catch (e) {
      setState(() {
        errorMessage = 'خطا در بارگذاری تصویر یا تولید پالت: $e';
        isLoading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Palette Generator Master'),
        centerTitle: true,
        backgroundColor: paletteGenerator?.vibrantColor?.color ?? Colors.blue,
        foregroundColor: paletteGenerator?.vibrantColor != null
            ? paletteGenerator!.getBestTextColorFor(
                paletteGenerator!.vibrantColor!.color,
              )
            : Colors.white,
      ),
      body: isLoading
          ? const Center(child: CircularProgressIndicator())
          : errorMessage != null
          ? Center(
              child: Text(
                errorMessage!,
                style: const TextStyle(color: Colors.red),
              ),
            )
          : SingleChildScrollView(
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(16),
                      child: Image.asset(
                        targetAsset,
                        height: 300,
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  if (paletteGenerator != null) ...[
                    _buildVibrantCard(),
                    const Divider(),
                    _buildFullPalette(),
                    const Divider(),
                    _buildHarmonySection(),
                  ],
                ],
              ),
            ),
      floatingActionButton: FloatingActionButton(
        onPressed: _updatePaletteGenerator,
        child: const Icon(Icons.refresh),
      ),
    );
  }

  Widget _buildVibrantCard() {
    final vibrant = paletteGenerator!.vibrantColor;
    if (vibrant == null) return const SizedBox.shrink();

    final color = vibrant.color;
    final textColor = paletteGenerator!.getBestTextColorFor(color);

    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      color: color,
      child: Padding(
        padding: const EdgeInsets.all(24.0),
        child: Column(
          children: [
            Text(
              'رنگ زنده (Vibrant)',
              style: TextStyle(
                color: textColor,
                fontSize: 22,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 8),
            Text(
              'HEX: #${color.toARGB32().toRadixString(16).toUpperCase().substring(2)}',
              style: TextStyle(
                color: textColor,
                fontSize: 18,
                fontFamily: 'monospace',
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildFullPalette() {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            'پالت رنگ‌های استخراج شده:',
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
          ),
          const SizedBox(height: 12),
          GridView.builder(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 4,
              mainAxisSpacing: 8,
              crossAxisSpacing: 8,
            ),
            itemCount: paletteGenerator!.paletteColors.length,
            itemBuilder: (context, index) {
              final pColor = paletteGenerator!.paletteColors[index];
              return Container(
                decoration: BoxDecoration(
                  color: pColor.color,
                  borderRadius: BorderRadius.circular(8),
                  border: Border.all(color: Colors.black12),
                ),
                child: Center(
                  child: Text(
                    '${pColor.population}',
                    style: TextStyle(
                      color: paletteGenerator!.getBestTextColorFor(
                        pColor.color,
                      ),
                      fontSize: 10,
                    ),
                  ),
                ),
              );
            },
          ),
        ],
      ),
    );
  }

  Widget _buildHarmonySection() {
    if (paletteGenerator!.harmonyColors.isEmpty) return const SizedBox.shrink();

    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            'هارمونی رنگ‌ها (بر اساس رنگ غالب):',
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
          ),
          const SizedBox(height: 12),
          ...paletteGenerator!.harmonyColors.map((harmony) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  harmony.type.toString().split('.').last,
                  style: const TextStyle(fontWeight: FontWeight.w600),
                ),
                const SizedBox(height: 4),
                Row(
                  children: harmony.colors
                      .map(
                        (c) => Container(
                          width: 40,
                          height: 40,
                          margin: const EdgeInsets.only(right: 8, bottom: 12),
                          decoration: BoxDecoration(
                            color: c,
                            borderRadius: BorderRadius.circular(4),
                          ),
                        ),
                      )
                      .toList(),
                ),
              ],
            );
          }),
        ],
      ),
    );
  }
}
11
likes
160
points
3.69k
downloads

Documentation

Documentation
API reference

Publisher

verified publisherswanflutterdev.com

Weekly Downloads

A powerful Flutter package for extracting prominent colors from images with advanced features and accessibility compliance.

Repository (GitHub)
View/report issues

Topics

#color #palette #image #accessibility #ui

License

MIT (license)

Dependencies

collection, flutter

More

Packages that depend on palette_generator_master