代码拉取完成,页面将自动刷新
import 'package:flutter/material.dart';
class ModelBindingV1 extends StatefulWidget {
ModelBindingV1({Key key, this.initialModel = const ViewModel(), this.child})
: assert(initialModel != null),
super(key: key);
final ViewModel initialModel;
final Widget child;
@override
_ModelBindingV1State createState() => _ModelBindingV1State();
}
class _ModelBindingV1State extends State<ModelBindingV1> {
ViewModel currentModel = ViewModel();
void updateModel(ViewModel newModel) {
if (currentModel != newModel) {
setState(() {
currentModel = newModel;
});
}
}
@override
Widget build(BuildContext context) {
return _ModelBindingScope(
modelBindingState: this,
child: widget.child,
);
}
}
class ViewModel {
final int value;
const ViewModel({this.value = 0});
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other.runtimeType != runtimeType) {
return false;
}
final ViewModel otherModel = other;
return otherModel.value == value;
}
@override
int get hashCode => value.hashCode;
static ViewModel of(BuildContext context) {
_ModelBindingScope scope =
context.dependOnInheritedWidgetOfExactType(aspect: _ModelBindingScope);
return scope.modelBindingState.currentModel;
}
static void update(BuildContext context, ViewModel newModel) {
_ModelBindingScope scope =
context.dependOnInheritedWidgetOfExactType(aspect: _ModelBindingScope);
scope.modelBindingState.updateModel(newModel);
}
}
class _ModelBindingScope extends InheritedWidget {
_ModelBindingScope({
Key key,
@required this.modelBindingState,
Widget child,
}) : assert(modelBindingState != null),
super(key: key, child: child);
final _ModelBindingV1State modelBindingState;
@override
bool updateShouldNotify(_ModelBindingScope oldWidget) => true;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。