指南

    资料

    # # Counting计数器 学习很多语言的时候都有hello world,在Flutter里面他的Hello World是个counting计数器。下面我们把这个计数器转换为Flutter Fair。 相对于前面的写一个红色小块,计数器demo引入了状态State的变更,为了支持状态/局部刷新,需要配套引入state proxy代理对象。 counting工程是模板工程,只需要利用flutter create就可以创建。不同版本可能会略有差异,但是差不太多。核心内容如下 - 一个文本标签,展示计数值 - 一个按钮,更新计数 ## # 预览效果
    Flutter Fair
    counting old counting new
    ## # Dart源码 参考源码: - main.dart
    1. import 'package:fair/fair.dart';
    2. import 'package:flutter/material.dart';
    3. void main() {
    4. runApp(MyApp());
    5. }
    6. class MyApp extends StatelessWidget {
    7. @override
    8. Widget build(BuildContext context) {
    9. return MaterialApp(
    10. title: 'Flutter Demo',
    11. theme: ThemeData(
    12. primarySwatch: Colors.blue,
    13. visualDensity: VisualDensity.adaptivePlatformDensity,
    14. ),
    15. home: MyHomePage(title: 'Flutter Demo Home Page'),
    16. );
    17. }
    18. }
    19. class MyHomePage extends StatefulWidget {
    20. MyHomePage({Key key, this.title}) : super(key: key);
    21. final String title;
    22. @override
    23. _MyHomePageState createState() => _MyHomePageState();
    24. }
    25. class _MyHomePageState extends State<MyHomePage> {
    26. int _counter = 0;
    27. void _incrementCounter() {
    28. setState(() {
    29. _counter++;
    30. });
    31. }
    32. @override
    33. Widget build(BuildContext context) {
    34. return Scaffold(
    35. appBar: AppBar(
    36. title: Text(widget.title),
    37. ),
    38. body: Center(
    39. child: Column(
    40. mainAxisAlignment: MainAxisAlignment.center,
    41. children: <Widget>[
    42. Text(
    43. 'You have pushed the button this many times:',
    44. ),
    45. Text(
    46. '$_counter',
    47. style: Theme.of(context).textTheme.headline4,
    48. ),
    49. ],
    50. ),
    51. ),
    52. floatingActionButton: FloatingActionButton(
    53. onPressed: _incrementCounter,
    54. tooltip: 'Increment',
    55. child: Icon(Icons.add),
    56. ), // This trailing comma makes auto-formatting nicer for build methods.
    57. );
    58. }
    59. }
    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 60 61 62 63 64 65 66 ## # Fair改造 为了生存代码,我们将main.dart拆分独立文件; - main.dart runApp入口 - app.dart 从main剥离出app部分 - proxy.dart 新增 ### # main.dart 替换MyHomePage的引用,改为对FairWidget的引用
    1. import 'package:demo/proxy.dart';
    2. import 'package:fair/fair.dart';
    3. import 'package:flutter/material.dart';
    4. void main() {
    5. runApp(FairApp(child: MyApp(), profile: true));
    6. }
    7. class MyApp extends StatelessWidget {
    8. @override
    9. Widget build(BuildContext context) {
    10. return MaterialApp(
    11. title: 'Flutter Demo',
    12. theme: ThemeData(
    13. primarySwatch: Colors.blue,
    14. visualDensity: VisualDensity.adaptivePlatformDensity,
    15. ),
    16. home: FairWidget(
    17. name: 'counting',
    18. path: 'assets/bundle/lib_app.fair.bin',
    19. data: {'widget.title': 'Flutter Demo Home Page'},
    20. stateProxy: CountingStateProxy(),
    21. ),
    22. // home: MyHomePage(title: 'Flutter Demo Home Page'),
    23. );
    24. }
    25. }
    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 ### # app.dart 改造后的app既可以直接运行,也可以作为基础生成fair产物。
    1. import 'package:fair/fair.dart';
    2. import 'package:flutter/material.dart';
    3. import 'package:flutter/widgets.dart';
    4. class MyHomePage extends StatefulWidget {
    5. MyHomePage({Key key, this.title}) : super(key: key);
    6. final String title;
    7. @override
    8. _MyHomePageState createState() => _MyHomePageState();
    9. }
    10. @FairPatch()
    11. class _MyHomePageState extends State<MyHomePage> {
    12. @FairWell('_counter')
    13. int _counter = 0;
    14. @FairWell('_incrementCounter')
    15. void _incrementCounter() {
    16. setState(() {
    17. _counter++;
    18. });
    19. }
    20. @FairWell('themeStyle')
    21. TextStyle themeStyle() {
    22. return Theme.of(context).textTheme.headline4;
    23. }
    24. @override
    25. Widget build(BuildContext context) {
    26. return Scaffold(
    27. appBar: AppBar(
    28. title: Text(widget.title),
    29. ),
    30. body: Center(
    31. child: Column(
    32. mainAxisAlignment: MainAxisAlignment.center,
    33. children: <Widget>[
    34. Text(
    35. 'You have pushed the button this many times:',
    36. ),
    37. Text(
    38. '$_counter',
    39. style: themeStyle(),
    40. ),
    41. ],
    42. ),
    43. ),
    44. floatingActionButton: FloatingActionButton(
    45. onPressed: _incrementCounter,
    46. tooltip: 'Increment',
    47. child: Icon(Icons.add),
    48. ), // This trailing comma makes auto-formatting nicer for build methods.
    49. );
    50. }
    51. }
    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 ### # proxy.dart 代理对象,负责状态绑定
    1. import 'package:fair/fair.dart';
    2. import 'package:flutter/material.dart';
    3. class CountingStateProxy extends FairStateProxy {
    4. ValueNotifier<int> _counter;
    5. void _incrementCounter() {
    6. _counter.value++;
    7. }
    8. @override
    9. Map<String, FairBlocBuilder> property() {
    10. var props = super.property();
    11. _counter ??= ValueNotifier(0);
    12. props['_counter'] = () => _counter;
    13. props['themeStyle'] = () => Theme.of(context).textTheme.headline4;
    14. return props;
    15. }
    16. @override
    17. Map<String, dynamic> func() {
    18. var func = super.func();
    19. func['_incrementCounter'] = _incrementCounter;
    20. return func;
    21. }
    22. }
    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 从零开始写一个Demo 模板格式