Flutter 一些Package记录 1
1. styled_widget
使用扩展方法提升开发效率,增强代码可读性
示例代码:
Icon(OMIcons.home, color: Colors.white)
.padding(all: 10)
.decorated(color: Color(0xff7AC1E7), shape: BoxShape.circle)
.padding(all: 15)
.decorated(color: Color(0xffE8F2F7), shape: BoxShape.circle)
.padding(all: 20)
.card(
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
)
.alignment(Alignment.center)
.backgroundColor(Color(0xffEBECF1));
2. intl
提供国际化和本地化工具。可以用来格式化数字和日期
示例代码:
//格式化日期
var dateFormat = DateFormat("yyyy/MM/dd HH:mm:ss");
print(dateFormat.format(DateTime.now()));
3. logger
简单易用、美观的日志输出。
示例代码:
var logger = Logger();
logger.d("Logger is working!");
4. html_unescape
HTML特殊字符解码
实例代码:
var unescape = HtmlUnescape();
var text = unescape.convert("<strong>This "escaped" string");
print(text);
5. json_path
通过路径解析JSON
示例代码:
import 'dart:convert';
import 'package:json_path/json_path.dart';
void main() {
final json = jsonDecode('''
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
''');
final prices = JsonPath(r'$..price');
print('All prices in the store:');
/// The following code will print:
///
/// $['store']['book'][0]['price']: 8.95
/// $['store']['book'][6]['price']: 12.99
/// $['store']['book'][7]['price']: 8.99
/// $['store']['book'][8]['price']: 22.99
/// $['store']['bicycle']['price']: 19.95
prices
.read(json)
.map((match) => '${match.path}:\t${match.value}')
.forEach(print);
}
6. translator
封装谷歌翻译API,国内可用
示例代码:
final translator = GoogleTranslator();
final input = "Здравствуйте. Ты в порядке?";
translator.translate(input, from: 'ru', to: 'en').then(print);
// prints Hello. Are you okay?
var translation = await translator.translate("Dart is very cool!", to: 'pl');
print(translation);
// prints Dart jest bardzo fajny!
print(await "example".translate(to: 'pt'));
// prints exemplo
7. puppeteer
用于通过 DevTools 协议自动化 Chrome 浏览器(主要用于爬虫)