flutter_redux_template

一个崭新的 Flutter 项目。

JSON

模型类需要这些行来进行 JSON 生成

part 'foo.g.dart'; //on imports

factory Foo.fromJson(Map<String, dynamic> json) => _$FooFromJson(json);
Map<String, dynamic> toJson() => _$FooToJson(this);

当模型或 API 类发生更改时,请运行

flutter pub run build_runner build --delete-conflicting-outputs

Flutter Redux

React 视图

  1. 作为一种性能优化Widget 仅在
    ViewModel 更改时重建。为确保此功能正常工作,您必须
    ViewModel 实现 ==hashCode,并在创建 StoreConnector 时将 distinct
    选项设置为 true

  2. 使用示例方法 fromStore 来封装 store.stateviewModel 的转换

@immutable
class _FooPageViewModel {
  final String foo;
  final Function dispatcher;

  const _FooPageViewModel(this.foo, this.dispatcher);

  static _FooPageViewModel fromStore(Store<AppState> store) {
    return _FooPageViewModel(store.state.foo, (action) => store.dispatch(action));
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is _FooPageViewModel &&
        other.foo == foo &&
        other.dispatcher == dispatcher;
  }

  @override
  int get hashCode {
    return foo.hashCode ^ dispatcher.hashCode;
  }
}

class FooPage extends StatelessWidget {
  const FooPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, _FooPageViewModel>(
      distinct: true,
      converter: (store) => _FooPageViewModel.fromStore(store),
      builder: (context, viewModel) {
        return Scaffold(
          body: TextButton(
            child: Text(viewModel.foo),
            onPressed: () => viewModel.dispatcher(FooAction()),
          )
        );
      },
    );
  }
}

GitHub

查看 Github