Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withBuildChild fix #1034

Merged
merged 8 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/docs/api/observers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,36 @@ class _CounterExampleState extends State<CounterExample> {
}
```

#### Performance optimizations
Mostly you try to make `Observer` as small as possible. But sometimes you want to exclude
the widget subtree from being rebuilt and improve the performance.

For that you can use an `Observer.withBuiltChild` constructor. It takes `builder`
and `child` arguments and internally uses the same technique as in [AnimatedBuilder](https://api.flutter.dev/flutter/widgets/AnimatedBuilder-class.html)

Here is a brief example:

```dart
final obsColor = Observable(Colors.green);

Observer.withBuiltChild(
builder: (context, child) {
return Container(
padding: const EdgeInsets.all(16),
color: obsColor.value, // this widget will be rebuilt when the color value changes
child: child,
);
},
child: ListView.builder( // this part will not be rebuilt
shrinkWrap: true,
itemCount: 1000,
itemBuilder: (context, index) {
return ListTile(title: Text('Item $index'));
},
),
)
```

## ReactionBuilder widget

If you ever ran into a need for running a reaction when a Widget loads, you most
Expand Down
4 changes: 4 additions & 0 deletions flutter_mobx/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.2

- fix: removed runtime asserts and nullables from `Observer.withBuiltChild` - [@subzero911](https://github.com/subzero911)

## 2.2.1+1

- Analyzer fixes
Expand Down
28 changes: 10 additions & 18 deletions flutter_mobx/lib/src/observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,24 @@ class Observer extends StatelessObserverWidget {
required this.builder,
super.name,
super.warnWhenNoObservables,
}) : debugConstructingStackFrame = debugFindConstructingStackFrame(),
builderWithChild = null,
child = null,
assert(builder != null);
}) : debugConstructingStackFrame = debugFindConstructingStackFrame();

/// Observer which excludes the child branch from being rebuilt
///
/// - [builder] is a builder function with a child widget as a parameter;
///
/// - [child] is the widget to pass to the [builder] function.
// ignore: prefer_const_constructors_in_immutables
Observer.withBuiltChild({
super.key,
required this.builderWithChild,
required this.child,
required Widget Function(BuildContext, Widget) builder,
required Widget child,
super.name,
super.warnWhenNoObservables,
}) : debugConstructingStackFrame = debugFindConstructingStackFrame(),
builder = null,
assert(builderWithChild != null && child != null);

/// regular builder, suitable for most cases
final WidgetBuilder? builder;

/// builder function with child parameter
final TransitionBuilder? builderWithChild;
builder = ((context) => builder(context, child));

/// The child widget to pass to the [builderWithChild].
final Widget? child;
final WidgetBuilder builder;

/// The stack frame pointing to the source that constructed this instance.
final String? debugConstructingStackFrame;
Expand All @@ -60,8 +53,7 @@ class Observer extends StatelessObserverWidget {
: '');

@override
Widget build(BuildContext context) =>
builderWithChild?.call(context, child) ?? builder!.call(context);
Widget build(BuildContext context) => builder.call(context);

/// Matches constructor stack frames, in both VM and web environments.
static final _constructorStackFramePattern = RegExp(r'\bnew\b');
Expand Down
2 changes: 1 addition & 1 deletion flutter_mobx/lib/version.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated via set_version.dart. !!!DO NOT MODIFY BY HAND!!!

/// The current version as per `pubspec.yaml`.
const version = '2.2.1+1';
const version = '2.2.2';
2 changes: 1 addition & 1 deletion flutter_mobx/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_mobx
description:
Flutter integration for MobX. It provides a set of Observer widgets that automatically rebuild
when the tracked observables change.
version: 2.2.1+1
version: 2.2.2

repository: https://github.com/mobxjs/mobx.dart
issue_tracker: https://github.com/mobxjs/mobx.dart/issues
Expand Down
4 changes: 2 additions & 2 deletions flutter_mobx/test/flutter_mobx_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Observer.withBuiltChild(
builderWithChild: (context, child) {
builder: (context, child) {
return Column(
children: [
ElevatedButton(
onPressed: () => message.value = 'Clicked',
child: Container()),
Text(message.value, key: key1),
child!,
child,
Builder(builder: (context) {
return Text(message.value, key: key3);
}),
Expand Down
2 changes: 0 additions & 2 deletions mobx_codegen/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
include: package:lints/recommended.yaml

analyzer:
errors:
unused_element: ignore
exclude:
- test/data/**.dart
- test/*.g.dart
1 change: 1 addition & 0 deletions mobx_codegen/test/generator_usage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TestStore = _TestStore with _$TestStore;
bool customEquals(String? oldValue, String? newValue) => oldValue != newValue;

abstract class _TestStore with Store {
// ignore: unused_element
_TestStore(this.field1, {this.field2});

@observable
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ environment:
sdk: ">=3.0.0 <4.0.0"

dev_dependencies:
melos: ^5.2.1
melos: ^6.2.0
Loading