ObservableBuilder<T> class
请叫我code哥
//*****************************************************
// use the example:
// 使用示例
//*****************************************************
class CounterState {
int counter = 0;
}
var observableCounter = Observable(CounterState());
class TestWidget extends StatefulWidget {
@override
_TestWidgetState createState() => _TestWidgetState();
}
class _TestWidgetState extends State<TestWidget> {
@override
void initState() {
observableCounter.listen((state) {
print(state.counter);
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
ObservableBuilder<CounterState>(
create: observableCounter,
memo: (state) => [state.counter],
builder: (context, state) {
return Text('${state.counter}');
},
),
IconButton(
onPressed: () => observableCounter.notify((state) => state.counter++),
icon: Icon(Icons.add),
)
],
);
}
}
//*****************************************************
// provide a solution to change the theme
// 提供一种改变主题的解决方案
// change the theme example:
// 使用示例
//*****************************************************
enum ThemeType {
dark,
light,
}
class ThemeState {
ThemeType type = ThemeType.dark;
Color get title =>
{ThemeType.dark: Colors.white, ThemeType.light: Color(0xff3c3f41)}[type]!;
Color get body =>
{ThemeType.dark: Color(0xff3c3f41), ThemeType.light: Colors.white}[type]!;
}
var observableTheme = Observable(ThemeState());
// change the theme:
observableTheme.notify((theme) => theme.type =
theme.type == ThemeType.light
? ThemeType.dark
: ThemeType.light);
class ThemeBuilder extends StatelessWidget {
const ThemeBuilder({
Key? key,
required this.builder,
}) : super(key: key);
final Widget Function(BuildContext, ThemeState) builder;
@override
Widget build(BuildContext context) => ObservableBuilder<ThemeState>(
create: observableTheme, memo: (theme) => [theme.type], builder: builder);
}
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatelessWidget
- ObservableBuilder
Constructors
-
ObservableBuilder({Key? key, required Observable<
T> create, _Memo<T> ? memo, required _Builder<T> builder}) -
默认builder函数不会跟随父组件rebuild,只会监听数据变化触发rebuild。
当builder函数使用了其它状态otherState(非回调参数state),通过给ObservableBuilder设置ValueKey(otherState),使其触发rebuild。
StatelessWidget引用其它状态otherState需要设置key,使其触发rebuild。
StatefulWidget通过参数传递其它状态otherState不需要设置key,参数变化会触发rebuild。
const
Properties
-
builder
→ _Builder<
T> -
final
-
create
→ Observable<
T> -
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
-
memo
→ _Memo<
T> ? -
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
build(
BuildContext context) → Widget -
Describes the part of the user interface represented by this widget.
override
-
createElement(
) → StatelessElement -
Creates a StatelessElement to manage this widget's location in the tree.
inherited
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited