12.使用私人库 pubspec.yaml
可以指定 Git 私人库,引用链接、分支、tag,使用示例:
dependencies: # 混编路由库 flutter_boost: git: url: git@github.com:ReverseScale/flutter_boost.git ref: v1.1
13.检查是否为发布环境
判断当前环境是否为发布模式:
const bool kReleaseMode = bool.fromEnvironment('dart.vm.product')
使用系统级顶级状态 kReleaseMode
获取当前是否为发布环境:
import 'package:flutter/foundation.dart'; print('Is Release Mode: $kReleaseMode');
使用这个可以用于控制日志输出,比如 release 模式关闭日志:
if (isProduction) { debugPrint = (String message, {int wrapWidth}) => {}; }
14.状态栏的高度
final double statusBarHeight = MediaQuery.of(context).padding.top;
15.底部栏的高度
final double bottomHeight = MediaQuery.of(context).padding.bottom;
16.科学计数法表达
// 输出:1e-8 var number = 1 / pow(10, 8); // 输出:0.00000001 print(aaaa.toStringAsFixed(8));
17.InheritedWidget 存值方法
1.声明 ShareDataWidget:
class ShareDataWidget extends InheritedWidget { int count; ShareDataWidget({ @required this.count, Widget child }): super(child: child); static ShareDataWidget of(BuildContext context) { return context.inheritFromWidgetOfExactType(ShareDataWidget); } @override bool updateShouldNotify(InheritedWidget oldWidget) { return true; } }
2.存入传递值,例如:body 下嵌套传值:
body: ShareDataWidget( count: this.widget.count, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[ Text("Widget数量: ${this.widget.count}"), _Counter(), RaisedButton( child: Text("增加"), onPressed: () => setState(() => widget.count ++), ) ] ), ), )
3.取出传递值:
ShareDataWidget sdw = context.inheritFromWidgetOfExactType(ShareDataWidget); print(sdw.count);
18.Cocoapod 引入 Flutter Framework
flutter_application_path = '../Seller/'load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')install_all_flutter_pods(flutter_application_path)
19.mixins 语法(高级)
class Musician extends Performer with Musical { // ··· } class Maestro extends Person with Musical, Aggressive, Demented { Maestro(String maestroName) { name = maestroName; canConduct = true; } } mixin Musical { bool canPlayPiano = false; bool canCompose = false; bool canConduct = false; void entertainMe() { if (canPlayPiano) { print('Playing piano'); } else if (canConduct) { print('Waving hands'); } else { print('Humming to self'); } } }
20.泛型示例
var names = <String>['Seth', 'Kathy', 'Lars']; var pages = <String, String>{ 'index.html': 'Homepage', 'robots.txt': 'Hints for web robots', 'humans.txt': 'We are people, not machines' }; To specify one or more types when using a constructor, put the types in angle brackets (<...>) just after the class name. For example: var names = List<String>(); names.addAll(['Seth', 'Kathy', 'Lars']); var nameSet = Set<String>.from(names); var views = Map<int, View>();
打印泛型类型
class Foo<T extends SomeBaseClass> { // Implementation goes here... String toString() => "Instance of 'Foo<$T>'"; } class Extender extends SomeBaseClass {...}
21.导入库
库冲突
import 'package:lib1/lib1.dart'; import 'package:lib2/lib2.dart' as lib2;
导入库的一部分
// Import only foo. import 'package:lib1/lib1.dart' show foo; // Import all names EXCEPT foo. import 'package:lib2/lib2.dart' hide foo;
懒加载库
import 'package:greetings/hello.dart' deferred as hello; Future greet() async { await hello.loadLibrary(); hello.printGreeting(); }
22.Generators(迭代生成器)
同步的Generators
Iterable<int> naturalsTo(int n) sync* { int k = 0; while (k < n) yield k++; }
异步的Generators
Stream<int> asynchronousNaturalsTo(int n) async* { int k = 0; while (k < n) yield k++; }
23.定义元数据
示例
library todo; class Todo { final String who; final String what; const Todo(this.who, this.what); } 使用 import 'todo.dart'; @Todo('seth', 'make this do something') void doSomething() { print('do something'); }
24.私有 Pub
~ $ git clone <https://github.com/dart-lang/pub_server.git> ~ $ cd pub_server ~/pub_server $ pub get ... ~/pub_server $ dart example/example.dart -d /tmp/package-db
参数释义:
-s 是否fetch官方仓库 -h ${ip / domain} -p 端口 -d 上传上来的插件包在服务器上的存储地址
使用它可以将新软件包上载到本地运行的服务器或下载本地可用的软件包或通过回退到以下软件包 pub.dartlang.org 很容易:
~/foobar $ export PUB_HOSTED_URL=http://localhost:8080 ~/foobar $ pub get ... ~/foobar $ pub publish Publishing x 0.1.0 to <http://localhost:8080>: |-- ... '-- pubspec.yaml Looks great! Are you ready to upload your package (y/n)? y Uploading... Successfully uploaded package.
完善 pubspec.yaml 文件
插件名称 description: 插件描述 version: 0.0.1 版本号 author: xxxx<xx@xxx.com> homepage: 项目主页地址 publish_to: 填写私有服务器的地址(如果是发布到flutter pub则不用填写,插件默认是上传到flutter pub)
发布至私有服务器
flutter packages pub publish --server $服务器地址
25.原生和 Flutter 之间数据交互类型有限制
在进行插件的开发时,就必定会涉及到原生和 Flutter 之间的数据交互.这里需要注意的是,就像我们在进行 ReactNative 和 JNI 的开发时,并不是什么类型的数据都是支持交互的。下面我给出原生和 Flutter 之间可交互的数据类型:
这里我们用得最多的就是 bool
、int
、String
、Map
这几个类型了。