在Dart中使用基本类,例如`Offset`/`Color` [英] Use basic classes like `Offset`/`Color` in Dart without Flutter
问题描述
我想在纯 dart中使用那些基本类(例如
程序.我无法将它们导入纯飞镖项目中.我该如何使用它们? Offset
, Size
和 Color
)
这些类确实是基础知识,非常有帮助.例如,使用 Color
(和 Colors
),可以轻松使用颜色系统.因此,如果可以使用它们,我将不胜感激.
我尝试了这,但是它给了我以下错误:错误:找不到:'dart:ui'
.我也尝试过复制和粘贴,但这有点难看...
您无法在控制台程序中导入 dart:ui
.但是,如果您想使用它们,可以做一些事情:
要使用 Color
类以及 RgbColor
, HexColor
, HslColor
, XyzColor
和 CielabColor
,则可以使用 color
包.通过将包导入pubspec.yaml文件(您需要手动创建),可以在cli应用程序中使用它.
pubspec.yaml:
名称:CLI描述:一个CLI应用程序版本:1.0.0publish_to:无"环境:sdk:"= 2.7.0< 3.0.0&";依赖项:颜色:2.1.1< ---
main.dart:
import'package:color/color.dart';void main(List< String> args){颜色color =新的Color.hex("FFFFFF");打印(彩色);//应打印"ffffff";}
如果要使用Flutter的 Color
类,请将其复制到单独的 color.dart
文件以及 _scaleAlpha()
与 Color
类( sky_engine \ lib \ ui \ painting.dart
)和 _clampInt()
+ _lerpInt()
(可在 sky_engine \ lib \ ui \ lerp.dart
中找到).不要忘记导入"dart:math";数学
到此新文件.您说只复制是很丑陋的,但是如果您想以后使它与Flutter兼容,那是您可以做的最好的事情.只需将 main.dart
文件中的 import" color.dart"
更改为 import" dart:ui"
对于 Size
和 Offset
,它们位于(抖动路径) flutter \ bin \ cache \ pkg \ sky_engine \ lib \ ui \ geometry.dart
.如您所说,复制和粘贴很丑陋,并且查看我想说的大多数功能对于控制台应用程序没有用.
也许可以尝试通过自己编写适合您需要的类来实现它们,或者剥离 geometry.dart
中的原始类.如果删除注释,这两个类都很小.
I want to use those basic classes (such as Offset
, Size
, and Color
), in a pure-dart
program. I cannot import them in a pure dart project. How can I use them?
Those classes are really fundamental and quite helpful. For example, with Color
(and Colors
) the color system can be easily used. So I would appreciate it if I could use them.
I have tried this, but it gives me the following error: Error: Not found: 'dart:ui'
. I have also tried copying and pasting, but that is a bit ugly...
You can't import dart:ui
in a console program. But you can make a few things if you want to use them:
To use a Color
class, along with RgbColor
, HexColor
, HslColor
, XyzColor
and CielabColor
, you can use the color
package. You can use it in a cli app by importing the package in the pubspec.yaml file (which you need to manually create)
pubspec.yaml:
name: CLI
description: A CLI App
version: 1.0.0
publish_to: 'none'
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
color: 2.1.1 <---
main.dart:
import 'package:color/color.dart';
void main(List<String> args) {
Color color = new Color.hex("FFFFFF");
print(color); //Should print "ffffff"
}
Edit: If you want to use Flutter's Color
class, copy it into a separate color.dart
file, as well as _scaleAlpha()
found in the same file as the Color
class (sky_engine\lib\ui\painting.dart
), and _clampInt()
+ _lerpInt()
(found in sky_engine\lib\ui\lerp.dart
). Don't forget to import "dart:math" as math
to this new file. You said it was ugly to just copy, but it is the best you can do if you want to make this compatible with Flutter later. Just change import "color.dart"
in your main.dart
file to import "dart:ui"
As for Size
and Offset
, they are located in (path to flutter) flutter\bin\cache\pkg\sky_engine\lib\ui\geometry.dart
. As you've said, copying and pasting is ugly, and looking at the functions I'd say most are not useful for a console app.
Maybe try to implement them yourself by writing these classes from scratch fit for your needs, or by stripping down the original classes in geometry.dart
. Both classes are quite small if you remove the comments.
这篇关于在Dart中使用基本类,例如`Offset`/`Color`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!