一个轻量级且简单的 Flutter 包,用于在各种类别(如长度、重量、温度等)之间进行单位转换。设计简洁,易于集成,性能高效,支持 Android、iOS、Web、macOS、Windows 和 Linux 等多个平台。非常适合需要实时单位转换而无需依赖重型库的应用程序。该包提供清晰的 API 和直观的方法,用于常见转换,使所有水平的开发者都能轻松使用。
适合场景
带测量单位转换的应用程序
旅行或烹饪应用
教育工具
科学计算器
跨单元数据展示
主要特点
支持多种单位类型
轻量且快速
跨平台兼容性
简单 API
可扩展的单位定义
适合对象
需要基本单位转换的开发者
具有国际测量需求的应用程序
优先考虑最小依赖项的项目
使用建议
限于预定义的单位
无法创建自定义单位
可能需要为新单位类型更新
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
units_converter
units_converter is a package written in dart for dart & flutter developers. You should not take care of unit conversion when you want to internationalize your app, everything is already done with this package! You can also add your own custom conversion!
Note: most of the time is what you need, use this method if you just want to convert a unit in another unit. BUT, it is not recommended if you want to convert a unit to multiple units, or if you are interested in the symbol of a unit or if you are interested in the format of the string representation of the value (significant figures, scientific notation, etc.).
Convert a unit to another unit
Example 2: convert 1 meter in inches
// We give 1 meter as inputvar length = Length()..convert(LENGTH.meters, 1);
// We get the inchesvar unit = length.inches;
// We print the Unitprint('name:${unit.name}, value:${unit.value}, stringValue:${unit.stringValue}, symbol:${unit.symbol}');
Example 3: convert 1 degree in all the other angles units. This time we want also to specify that we just want 7 significant figures and we don't want trailing zeros (e.g. 1.000000 -> 1).
// Initialization of the objectvar angle = Angle(significantFigures: 7, removeTrailingZeros: false); // conversion
angle.convert(ANGLE.degree, 1);
// We get all the unitsvar units = angle.getAll(); //We get all ther others units// Let's print all of themfor (var unit in units) {
print('name:${unit.name}, value:${unit.value}, stringValue:${unit.stringValue}, symbol:${unit.symbol}');
}
As you can see in this example if you specify removeTrailingZeros: false, the stringValue keeps all the trailing zeros (the default is true). You can also ask for an certain number of significant figures in the stringValue.
Special type of conversion: numeral systems conversion
Warning! Numeral systems conversion is the only conversion that need the input as a String, and not as a double / int for obvious reasons
Example 4: convert '100' (decimal) to binary and hexadecimal
// We give '100' decimal as inputvar numeralSystems = NumeralSystems()..convert(NUMERAL_SYSTEMS.decimal, '100');
// We get the binary valueprint('Binary: ${numeralSystems.binary.stringValue}');
// We get the hexadecimal valueprint('Hexadecimal: ${numeralSystems.hexadecimal.stringValue}');
Output:
Binary: 1100100Hexadecimal: 64
Simple custom conversion
Use SimpleCustomProperty when you are dealing with a linear conversion between the units, i.e. when a unit is x times a value. In SimpleCustomProperty we have to define a conversionMap between a base unit, which must have a value of 1, and all the other units. In the example below we say that 1€ is 1.2271$, but also 0.9033₤, and so on and so forth.
Example 5: define the currency exchange rate with their symbols (optional) and perform the conversion between two of them.
finalMap<String, double> conversionMap = {
'EUR': 1,
'USD': 1.2271,
'GBP': 0.9033,
'JPY': 126.25,
'CNY': 7.9315,
};
// The map of the symbols is optional but nicefinalMap<String, String> mapSymbols = {
'EUR': '€',
'USD': '\$',
'GBP': '₤',
'JPY': '¥',
'CNY': '¥',
};
var customConversion = SimpleCustomProperty(conversionMap, mapSymbols: mapSymbols);
customConversion.convert('EUR', 1);
Unit usd = customConversion.getUnit('USD');
print('1€ = ${usd.stringValue}${usd.symbol}');
Output:
1€ = 1.2271$
Custom conversion
In most cases, you will only need SimpleCustomProperty (see the previous section). SimpleCustomProperty allow you to define conversions in the form of y=ax. But if you need to define special relationship between units you need CustomProperty. This allow you to perform conversion like: y=ax+b and y=a/x+b (where y and x are the value of two units and a and b are two coefficient), for example the conversion between Celsius and Fahrenheit use the first relation and the conversion between km/l and l/100km has to be done with the second relation. Both can't be done with SimpleCustomProperty.
Example 6: let's define the following imaginary units of measurement:
KiloDash = 1000 * Dash
DashPlus1 = Dash + 1
OneOver(DashPlus1) = 1 / (DashPlus1)
As you can see all this relations can be structured like a conversion tree. Now you can take a look at the example below: it does in the code what we have just said.