Flutter颜色调节插件color_hue_n_tint的使用

发布于 1周前 作者 yuanlaile 来自 Flutter

Flutter颜色调节插件color_hue_n_tint的使用

概述

color_hue_n_tint 是一个用于生成 HSL 圆柱体颜色阴影调色板的 Flutter 插件。它允许开发者通过色调(hue)和色调值(tint)来创建不同的颜色调色板。

构造函数

HueNTint(int value)

从低 32 位构造颜色。

HueNTint color = HueNTint(0xff0000); // 红色

HueNTint(int r, int g, int b)

从低 8 位的四个整数构造颜色。

  • r 是红色,范围在 0 到 255。
  • g 是绿色,范围在 0 到 255。
  • b 是蓝色,范围在 0 到 255。
HueNTint color = HueNTint(00, 255, 00); // 绿色

功能

.palette({double? scale})

创建一个包含阴影的颜色调色板。

HueNTint color = HueNTint(0xff0000);
List<Color> colorList = color.palette();
// 输出:
// [
//   Color(0xffffd4d4),
//   Color(0xffffaaaa),
//   Color(0xffff7f7f),
//   Color(0xffff5555),
//   Color(0xffff2a2a),
//   Color(0xffff0000),
//   Color(0xffcc0000),
//   Color(0xff990000),
//   Color(0xff660000),
//   Color(0xff330000)
// ]

.tints({double? scale})

创建一个包含浅色部分的颜色调色板。

HueNTint color = HueNTint(0xff0000);
List<Color> colorList = color.tints();
// 输出:
// [
//   Color(0xffffd4d4),
//   Color(0xffffaaaa),
//   Color(0xffff7f7f),
//   Color(0xffff5555),
//   Color(0xffff2a2a)
// ]

.hues({double? scale})

创建一个包含深色部分的颜色调色板。

HueNTint color = HueNTint(0xff0000);
List<Color> colorList = color.hues();
// 输出:
// [
//   Color(0xffcc0000),
//   Color(0xff990000),
//   Color(0xff660000),
//   Color(0xff330000)
// ]

完整示例

以下是一个完整的 Flutter 应用程序示例,展示了如何使用 color_hue_n_tint 插件来创建和展示不同颜色调色板。

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Color Hue N Tint',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Color Hue N Tint'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Color pickerColor = const Color(0xffff0000);
  late List<Color> palettes;
  late List<Color> hues;
  late List<Color> tints;

  void palette() => palettes =
      HueNTint(pickerColor.value, shades: 10, index: 5, fullScale: false).palette();
  void hue() =>
      hues = HueNTint(pickerColor.value, shades: 10, index: 5, fullScale: false).hues();
  void tint() => tints =
      HueNTint(pickerColor.value, shades: 10, index: 5, fullScale: false).tints();

  [@override](/user/override)
  void initState() {
    palette();
    hue();
    tint();
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      initialIndex: 0,
      child: Scaffold(
          floatingActionButton: FloatingActionButton(
              elevation: 8,
              onPressed: () {
                showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return AlertDialog(
                      titlePadding: const EdgeInsets.all(0),
                      contentPadding: const EdgeInsets.all(0),
                      content: SingleChildScrollView(
                          child: HueRingPicker(
                        pickerColor: pickerColor,
                        onColorChanged: (value) => setState(() {
                          pickerColor = value;
                          palette();
                          hue();
                          tint();
                          // print('hello');
                        }),
                      )),
                    );
                  },
                );
              },
              child: const Icon(Icons.color_lens_outlined)),
          appBar: AppBar(
            title: Text(widget.title),
            bottom: const TabBar(tabs: [
              Tab(
                text: 'Palette',
              ),
              Tab(
                text: 'Hues',
              ),
              Tab(
                text: 'Tints',
              ),
            ]),
          ),
          body: TabBarView(
            children: [
              Row(
                children: [
                  for (var i in palettes)
                    Expanded(
                      child: thisContainer(
                        color: i,
                      ),
                    )
                ],
              ),
              Row(
                children: [
                  for (var i in hues)
                    Expanded(
                      child: thisContainer(
                        color: i,
                      ),
                    )
                ],
              ),
              Row(
                children: [
                  for (var i in tints)
                    Expanded(
                      child: thisContainer(
                        color: i,
                      ),
                    )
                ],
              ),
            ],
          )),
    );
  }

  Widget thisContainer({required Color color}) {
    return Container(
      color: color,
      child: Center(
        child: RotatedBox(
            quarterTurns: -1,
            child: Container(
                height: 30,
                width: 80,
                decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(15))),
                alignment: Alignment.center,
                child: Text(
                  color.value.toRadixString(16),
                  style: const TextStyle(fontWeight: FontWeight.bold),
                ))),
      ),
    );
  }
}

更多关于Flutter颜色调节插件color_hue_n_tint的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter颜色调节插件color_hue_n_tint的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter中使用color_hue_n_tint插件来调节颜色的示例代码。这个插件允许你调整颜色的色调(Hue)和色调强度(Tint),非常适合用于颜色选择器或者任何需要动态调整颜色的场景。

首先,你需要在你的pubspec.yaml文件中添加依赖:

dependencies:
  flutter:
    sdk: flutter
  color_hue_n_tint: ^latest_version  # 请替换为实际的最新版本号

然后运行flutter pub get来获取依赖。

接下来,你可以在你的Flutter应用中使用ColorHueNTint类来调整颜色。以下是一个简单的示例,展示如何创建一个颜色选择器,允许用户通过滑块来调整颜色的色调和色调强度:

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

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

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

class ColorAdjustmentScreen extends StatefulWidget {
  @override
  _ColorAdjustmentScreenState createState() => _ColorAdjustmentScreenState();
}

class _ColorAdjustmentScreenState extends State<ColorAdjustmentScreen> {
  Color _baseColor = Colors.blue;
  double _hue = 0.5; // 色调值,范围从0到1
  double _tint = 0.5; // 色调强度值,范围从0到1

  @override
  Widget build(BuildContext context) {
    Color adjustedColor = ColorHueNTint.adjustColor(_baseColor, _hue, _tint);

    return Scaffold(
      appBar: AppBar(
        title: Text('Color Hue & Tint Adjustment'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 100,
              height: 100,
              color: adjustedColor,
            ),
            SizedBox(height: 20),
            Slider(
              value: _hue,
              min: 0.0,
              max: 1.0,
              onChanged: (double value) {
                setState(() {
                  _hue = value;
                });
              },
              label: 'Hue',
            ),
            SizedBox(height: 20),
            Slider(
              value: _tint,
              min: 0.0,
              max: 1.0,
              onChanged: (double value) {
                setState(() {
                  _tint = value;
                });
              },
              label: 'Tint',
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的界面,其中包含一个显示调整后的颜色的方块和两个滑块,一个用于调整色调(Hue),另一个用于调整色调强度(Tint)。当用户拖动滑块时,颜色会实时更新。

  • ColorHueNTint.adjustColor(_baseColor, _hue, _tint)方法用于根据基础颜色、色调值和色调强度值计算出调整后的颜色。
  • Slider组件用于获取用户输入的色调和色调强度值,并通过setState方法更新UI。

这个示例展示了如何使用color_hue_n_tint插件来动态调整颜色,你可以根据自己的需求进一步扩展这个示例。

回到顶部