Flutter中怎么使用第三方插件
2019-08-19 本文已影响0人
iDevOps
https://pub.dev/flutter是Google官方的Dart Packages仓库,类似于node中的npm仓库,我们可以在上面查找我们需要的包和插件,也可以向pub发布我们的包和插件
我们这里使用一个叫english_words, 它包含了数千个常用的英文单词并提供了一些比较使用的功能,
添加依赖项
在pubspec.yaml文件中添加依赖项

我们保存pubspec.yaml文件, 会自动将依赖包安装到项目中, 控制台输出以下内容

接下来我们就可以使用这个依赖包了
lib/main.dart
import 'package:flutter/material.dart';
//引入english_words
import 'package:english_words/english_words.dart';
//main是入口函数
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// 生成一个随机的单词
final wordPair = new WordPair.random();
return new MaterialApp(
title: 'Hello Flutter',
home: new Scaffold(
appBar: new AppBar(
title: new Text('Hello Flutter Title'),
),
body: new Center(
child: new Text(wordPair.asPascalCase), //asPascalCase以简单字符串的形式返回单词对,每个单词大写
),
),
);
}
}
这样的话, 我们每次按R, 页面重新渲染, 我们都会看到一个随机的英语单词
