- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用以下代码获取在线图像的主色,但调色板颜色仍然为空。我怎么解决这个问题?N:B: 如果我使用本地镜像源 AssetImage("source_path"),它可以工作。
class HomePageUI extends StatefulWidget {
final HomeData _homeData;
HomePageUI(this._homeData);
@override
_HomePageUIState createState() => _HomePageUIState();
}
class _HomePageUIState extends State<HomePageUI> {
PaletteColor paletteColor;
@override
void initState(){
super.initState();
_updatePalettes();
}
_updatePalettes() async{
final PaletteGenerator paletteGenerator = await PaletteGenerator.fromImageProvider(
NetworkImage(NetworkConfig.getImageUrl(widget._homeData.trending[1].image)),
size: Size(200,100)
);
paletteColor = paletteGenerator.darkMutedColor;
//Here paletteColor is shown null value
}
}
最佳答案
您可以在下面复制粘贴运行完整代码
您不需要修改 _updatePalettes()
您可以直接使用 NetworkImage
并传递正确的 imageSize
代码片段
home: const ImageColors(
title: 'Image Colors',
image: NetworkImage('https://picsum.photos/250?image=9'), //AssetImage('assets/landscape.png'),
imageSize: Size(250.0, 250.0),
),
工作演示
完整代码
// Copyright 2018 The Chromium Authors. 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 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:palette_generator/palette_generator.dart';
void main() => runApp(MyApp());
const Color _kBackgroundColor = Color(0xffa0a0a0);
const Color _kSelectionRectangleBackground = Color(0x15000000);
const Color _kSelectionRectangleBorder = Color(0x80000000);
const Color _kPlaceholderColor = Color(0x80404040);
/// The main Application class.
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Image Colors',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const ImageColors(
title: 'Image Colors',
image: NetworkImage('https://picsum.photos/250?image=9'), //AssetImage('assets/landscape.png'),
imageSize: Size(250.0, 250.0),
),
);
}
}
/// The home page for this example app.
@immutable
class ImageColors extends StatefulWidget {
/// Creates the home page.
const ImageColors({
Key key,
this.title,
this.image,
this.imageSize,
}) : super(key: key);
/// The title that is shown at the top of the page.
final String title;
/// This is the image provider that is used to load the colors from.
final ImageProvider image;
/// The dimensions of the image.
final Size imageSize;
@override
_ImageColorsState createState() {
return _ImageColorsState();
}
}
class _ImageColorsState extends State<ImageColors> {
Rect region;
Rect dragRegion;
Offset startDrag;
Offset currentDrag;
PaletteGenerator paletteGenerator;
final GlobalKey imageKey = GlobalKey();
@override
void initState() {
super.initState();
region = Offset.zero & widget.imageSize;
_updatePaletteGenerator(region);
}
Future<void> _updatePaletteGenerator(Rect newRegion) async {
paletteGenerator = await PaletteGenerator.fromImageProvider(
widget.image,
size: widget.imageSize,
region: newRegion,
maximumColorCount: 20,
);
setState(() {});
}
// Called when the user starts to drag
void _onPanDown(DragDownDetails details) {
final RenderBox box = imageKey.currentContext.findRenderObject();
final Offset localPosition = box.globalToLocal(details.globalPosition);
setState(() {
startDrag = localPosition;
currentDrag = startDrag;
dragRegion = Rect.fromPoints(startDrag, currentDrag);
});
}
// Called as the user drags: just updates the region, not the colors.
void _onPanUpdate(DragUpdateDetails details) {
setState(() {
currentDrag += details.delta;
dragRegion = Rect.fromPoints(startDrag, currentDrag);
});
}
// Called if the drag is canceled (e.g. by rotating the device or switching
// apps)
void _onPanCancel() {
setState(() {
dragRegion = null;
startDrag = null;
});
}
// Called when the drag ends. Sets the region, and updates the colors.
Future<void> _onPanEnd(DragEndDetails details) async {
Rect newRegion =
(Offset.zero & imageKey.currentContext.size).intersect(dragRegion);
if (newRegion.size.width < 4 && newRegion.size.width < 4) {
newRegion = Offset.zero & imageKey.currentContext.size;
}
await _updatePaletteGenerator(newRegion);
setState(() {
region = newRegion;
dragRegion = null;
startDrag = null;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: _kBackgroundColor,
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20.0),
// GestureDetector is used to handle the selection rectangle.
child: GestureDetector(
onPanDown: _onPanDown,
onPanUpdate: _onPanUpdate,
onPanCancel: _onPanCancel,
onPanEnd: _onPanEnd,
child: Stack(children: <Widget>[
Image(
key: imageKey,
image: widget.image,
width: widget.imageSize.width,
height: widget.imageSize.height,
),
// This is the selection rectangle.
Positioned.fromRect(
rect: dragRegion ?? region ?? Rect.zero,
child: Container(
decoration: BoxDecoration(
color: _kSelectionRectangleBackground,
border: Border.all(
width: 1.0,
color: _kSelectionRectangleBorder,
style: BorderStyle.solid,
)),
)),
]),
),
),
// Use a FutureBuilder so that the palettes will be displayed when
// the palette generator is done generating its data.
PaletteSwatches(generator: paletteGenerator),
],
),
);
}
}
/// A widget that draws the swatches for the [PaletteGenerator] it is given,
/// and shows the selected target colors.
class PaletteSwatches extends StatelessWidget {
/// Create a Palette swatch.
///
/// The [generator] is optional. If it is null, then the display will
/// just be an empty container.
const PaletteSwatches({Key key, this.generator}) : super(key: key);
/// The [PaletteGenerator] that contains all of the swatches that we're going
/// to display.
final PaletteGenerator generator;
@override
Widget build(BuildContext context) {
final List<Widget> swatches = <Widget>[];
if (generator == null || generator.colors.isEmpty) {
return Container();
}
for (Color color in generator.colors) {
swatches.add(PaletteSwatch(color: color));
}
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Wrap(
children: swatches,
),
Container(height: 30.0),
PaletteSwatch(label: 'Dominant', color: generator.dominantColor?.color),
PaletteSwatch(
label: 'Light Vibrant', color: generator.lightVibrantColor?.color),
PaletteSwatch(label: 'Vibrant', color: generator.vibrantColor?.color),
PaletteSwatch(
label: 'Dark Vibrant', color: generator.darkVibrantColor?.color),
PaletteSwatch(
label: 'Light Muted', color: generator.lightMutedColor?.color),
PaletteSwatch(label: 'Muted', color: generator.mutedColor?.color),
PaletteSwatch(
label: 'Dark Muted', color: generator.darkMutedColor?.color),
],
);
}
}
/// A small square of color with an optional label.
@immutable
class PaletteSwatch extends StatelessWidget {
/// Creates a PaletteSwatch.
///
/// If the [color] argument is omitted, then the swatch will show a
/// placeholder instead, to indicate that there is no color.
const PaletteSwatch({
Key key,
this.color,
this.label,
}) : super(key: key);
/// The color of the swatch. May be null.
final Color color;
/// The optional label to display next to the swatch.
final String label;
@override
Widget build(BuildContext context) {
// Compute the "distance" of the color swatch and the background color
// so that we can put a border around those color swatches that are too
// close to the background's saturation and lightness. We ignore hue for
// the comparison.
final HSLColor hslColor = HSLColor.fromColor(color ?? Colors.transparent);
final HSLColor backgroundAsHsl = HSLColor.fromColor(_kBackgroundColor);
final double colorDistance = math.sqrt(
math.pow(hslColor.saturation - backgroundAsHsl.saturation, 2.0) +
math.pow(hslColor.lightness - backgroundAsHsl.lightness, 2.0));
Widget swatch = Padding(
padding: const EdgeInsets.all(2.0),
child: color == null
? const Placeholder(
fallbackWidth: 34.0,
fallbackHeight: 20.0,
color: Color(0xff404040),
strokeWidth: 2.0,
)
: Container(
decoration: BoxDecoration(
color: color,
border: Border.all(
width: 1.0,
color: _kPlaceholderColor,
style: colorDistance < 0.2
? BorderStyle.solid
: BorderStyle.none,
)),
width: 34.0,
height: 20.0,
),
);
if (label != null) {
swatch = ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 130.0, minWidth: 130.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
swatch,
Container(width: 5.0),
Text(label),
],
),
);
}
return swatch;
}
}
关于flutter - 在线图像源的调色板生成器库在 Flutter 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60900736/
是否可以更改 Matplotlib 在为图形线条生成自己的颜色时循环使用的颜色?我正在使用 pylab 模块。 from pylab import * import matplotlib.cm as
R中是否有可以处理许多数据类的调色板的好包?我有 16 个连续数据类。我试过 RColorBrewer 但它最多有 9 个数据类,我也试过 Rainbow() 但它对顺序数据没有吸引力。谢谢 最佳答案
一些与 previous question 相关的内容 我想采用 gnuplot 的默认(pm3d 默认)调色板,并在 X 处放置一个白色值,并将任何 >=X 的内容设置为白色,但其余部分( 例如,我
我正在尝试使用调用 Web 服务的结果(这是一个分段列表)来填充 Tapestry 5 中的调色板组件。 Segment 是一个普通的类,具有一些属性,如标题...我想将标题显示为可用、选定的调色板列
我最近在 Eclipse 4.3 (Kepler) 上安装了 windowBuilder 工具。当我尝试在布局设计器上进行一些工作时,我的调色板窗口上不会出现任何组件。只有一些组件,如“布局 ->布局
我计划从我的 RecyclerView 中的每个 bitmap 中获取每一种鲜艳的颜色,并将其设置为我的文本的背景。所以我尝试了以下: Picasso.with(ctx)
如何使用D3 Palettes在 Bokeh ?我尝试以这种方式导入,但收到未解析的引用错误消息 from bokeh.palettes import Category20 Bokeh 版本: pri
Seaborn 允许定义包含多种颜色的调色板,这对具有多行的图表很有用。但是,当将调色板设置为具有多种颜色的调色板时,仅使用前六种颜色,之后颜色会循环使用,因此很难区分线条。这可以通过显式调用调色板来
实际上我正在开发一个包含 Material 设计抽屉导航的 android 应用程序,我想每 5 分钟动态更改状态栏、操作栏、文本颜色的颜色。实际上我正在使用支持库主题来支持所有 android手机。
我为一个项目编写了一些自定义的复合小部件,并将它们编译成一个单独的 jar 文件。我将这个 jar 文件添加到 GWT 项目的构建路径中。当我尝试将小部件添加到调色板时(通过按“选择组件”按钮),我可
我在缓冲区(类型 - 长型)中有一个来自 32 位扫描仪的图像数据。 例如buffer[0]对应的像素值为952即[184, 3, 0, 0] <-[R,G,B,A]; 我想在屏幕上显示/绘画/绘图;
我的公司有一个正式的调色板,所以我需要在我的 seaborn 图表中使用这些颜色。因此,我想设置默认的 seaborn 调色板,并为这些颜色提供易于使用的名称,例如“p”代表紫色,“g”代表绿色。 这
我试图通过一个主要是数字的因素为 ggplot 着色,如下所示: iris %>% ggplot(aes(Sepal.Length, Sepal.Width, color = cut(Petal.
我在 Kubuntu 12.04 上使用 Netbeans 7。我这样启动 Netbeans: netbeans --cp:a /usr/share/java/xercesImpl.jar & Net
我已经用我选择的颜色制作了颜色图,但是我想将其转换为可用于“调色”seaborn 绘图的调色板。这可能吗?如果可能的话,怎么可能? 我用过... cmap = pl.colors.LinearSegm
我需要动态生成自定义调色板来为我的 Angular 应用配置主题。 可用的默认 Material 调色板:https://material.io/guidelines/style/color.html
首先,我从未使用过 Palette,当我想开始使用它时,我看到所有教程和博客都在谈论 v21 而不是 v22。 我的问题是我有一个 Swatch(在 swatch 数组 [palette.getSwa
我如何使用此工具中没有的颜色构建自定义 MDL:https://getmdl.io/customize/index.html 我想要那些颜色: 原色:红色 400 (#EF5350) 二次色:琥珀色
如何知道调色板png是否带有alpha?我获取有关图像 png_get_IHDR 的信息 之后我查看color_type - PNG_COLOR_TYPE_PALETTE 但是我找不到如何知道这个 p
有没有一种简单的方法可以将真彩色图像(在本例中为 24 位,位图)转换为 1Bpp 图像(调色板,转换为位图)。 我知道 imagemagick convert 可以进行转换,但出于某种原因,当我尝试
我是一名优秀的程序员,十分优秀!