指南
资料
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
class DynamicWidget extends StatelessWidget {final String content;const DynamicWidget({Key key, this.content}) : super(key: key);@overrideWidget build(BuildContext context) {return Center(child: Container(child: Text(content,style: TextStyle(fontSize: 30, color: Colors.yellow),),alignment: Alignment.center,margin: EdgeInsets.only(top: 30, bottom: 30),color: Colors.redAccent,width: 300,height: 300,),);}}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 第二步:现在然我们生成bundle吧
// 修饰当前页面为一个动态bundle资源@FairPatch()class DynamicWidget extends StatelessWidget {// 修饰该属性会被build函数使用@FairWell('content')final String content;const DynamicWidget({Key key, this.content}) : super(key: key);@overrideWidget build(BuildContext context) {return Center(child: Container(child: Text(content,style: TextStyle(fontSize: 30, color: Colors.yellow),),alignment: Alignment.center,margin: EdgeInsets.only(top: 30, bottom: 30),color: Colors.redAccent,width: 300,height: 300,),);}}
编译成功后,在build/fair目录下找到同名bundle资源: - .fair.bin 格式为release产物 - .fair.json 格式为debug产物 - .fair.metadata 格式为元数据,标记了源码与产物的关联信息flutter pub run build_runner build
第三步:复制产物并使用
现在我们可以把资源拷贝出来,放到assets下(别忘了先在yaml中配置assets目录/路径)
1 2 3 4 重新运行app后,可以看到新的效果,前后效果是像素级别一模一样的。
FairWidget(path: 'assets/bundle/lib_page_dynamic_widget.fair.bin',data: {'content': 'Red Box'},)
| Flutter源码效果 | Fair 动态效果 |
|---|---|
![]() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 编写完代码后,重新生成bundle,app加载新的资源即可。 注意我们的数据原相比一个字段,多了三个,为了方便,这里使用了Bean,当然也可以分散为多个变量: - bean不能嵌套,名字需要于参数对齐
@FairPatch()class DynamicWidget extends StatelessWidget {@FairWell('data')final Data data;const DynamicWidget({Key key, this.data}) : super(key: key);@overrideWidget build(BuildContext context) {return Center(child: Container(child: Stack(alignment: Alignment.center,children: [Text.rich(TextSpan(text: data.content,style: TextStyle(fontSize: 30, color: Colors.yellow),children: [TextSpan(text: data.content2,style: TextStyle(fontSize: 30, color: Colors.blue),),],)),Positioned.fill(child: Icon(Icons.android, color: Colors.white),top: 100,),Positioned.fill(child: Icon(Icons.people, color: Colors.white),top: 100,left: 100,),Positioned.fill(child: Icon(Icons.desktop_mac, color: Colors.white),top: 100,right: 100,),],),margin: EdgeInsets.only(top: 30, bottom: 30),decoration: BoxDecoration(color: Colors.redAccent,image: DecorationImage(image: NetworkImage(data.url))),width: 300,height: 300,),);}}class Data {final String content;final String content2;final String url;Data(this.content, this.content2, this.url);}
1 2 3 4 5 6 7 8
FairWidget(path: 'assets/bundle/lib_page_dynamic_widget2.fair.bin',data: {'data.content': '张三','data.content2': '李四','data.url':'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2665922796,3139592461&fm=26&gp=0.jpg'},)
| 第一版效果 | Fair 动态效果 |
|---|---|
![]() | ![]() |
1 2 3 4 ← Flutter Fair接入 Counting计数器 →
FairWidget(path:'http://a.b.c/xxx/hello.json',name: 'sample_list_view')


