Package 介绍
通过使用 packages (的模式)可以创建易于共享的模块化代码。一个最基本的 package 由以下内容构成:
-
pubspec.yaml
文件
用于定义 package 名称、版本号、作者等其他信息的元数据文件。 -
lib
目录
包含共享代码的lib
目录,其中至少包含一个
文件。.dart
Package 类别
纯 Dart 库 (Dart packages)
用 Dart 编写的传统 package,比如 上面的gift_board_component UI组件包,纯dart的工具类包。其中一些可能包含 Flutter 的特定功能,因此依赖于 Flutter 框架,其使用范围仅限于 Flutter,没有调用原生功能代码的需求。
原生插件 (Plugin packages)
使用 Dart 编写的,按需使用 Java 或 Kotlin、ObjC 或 Swift 分别在 Android 和/或 iOS 平台实现的 package。有调用原生功能代码的需求
开发纯 Dart 库的 packages
第一步:创建 package
flutter create --template=package hello_plugin
其中 hello_plugin为包的名称
会在hello_plugin目录生成以下文件:
其中就有最基础的两个文件:pubspec.yaml 和 lib文件夹
将默认生成的hello_plugin.dart文件修改一下
library hello_plugin;
abstract class HelloPluginInterface{
int addOne(int value);
}
class HelloPlugin extends HelloPluginInterface {
@override
int addOne(int value) {
return value + 1;
}
}
第二步:实现 package
对于纯 Dart 库的 package,只要在 lib/
文件中添加功能实现,或在 lib
目录中的多个文件中添加功能实现。
如果要对 package 进行测试,在 test
目录下添加 单元测试。
关于如何组织 package 内容的更多详细信息,请参考 Dart library package 文档
常见的目录结构如下:
说明:
- example :使用例子
- lib : src(源码目录) +
.dart 或者其他需要单独公开的功能(如shelf_io.dart) - test: 单元测试目录
- tool: 工具目录,如一些批处理工具文件
由于纯dart库(不依赖Flutter的视图框架),基本没有其他步骤了。
下面主要说明一下实现UI组件package
1. 实现UI组件package
1.1 生成example工程
在上面的hello_plugin目录下执行
flutter create -i objc -a kotlin example
命令说明:-i 指定ios开发语言; -a指定android开发语言;
会在hello_plugin目录下生成example目录;example中包含android, ios 原生工程目录:
下面以android开发为例:
使用android studio 打开hello_plugin项目
1.2 在example中依赖本地库hello_plugin
在example目录中的pubspec.yaml文件中加入
hello_plugin:
path: ../
其中:
- hello_plugin:包的名称
- path为本地hello_plugin库lib目录所在的目录
1.3 使用pub get来获取依赖库
使用android studio中的
1.4 使用本地库开发
exmple目录下的lib目录:main.dart
导入hello_plugin.dart
import 'package:hello_plugin/hello_plugin.dart';
在_MyHomePageState中声明HelloPlugin
HelloPlugin helloPlugin = HelloPlugin();
点击按钮执行_incrementCounter()方法将_counter的值进行自增
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter = helloPlugin.addOne(_counter);
});
}
import 'package:flutter/material.dart';
import 'package:hello_plugin/hello_plugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
int _counter = 0;
HelloPlugin helloPlugin = HelloPlugin();
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter = helloPlugin.addOne(_counter);
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
参考
Flutter Packages 的开发和提交