Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/docs/cross-spawn-7.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanpodila authored Dec 16, 2024
2 parents 50db29a + d8ac087 commit 4ede8a3
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 26 deletions.
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
1 change: 1 addition & 0 deletions flutter_mobx/lib/flutter_mobx.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
/// );
///
// ignore: unnecessary_library_name
library flutter_mobx;

export 'package:flutter_mobx/src/multi_reaction_builder.dart';
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';
6 changes: 3 additions & 3 deletions flutter_mobx/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ 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
homepage: https://mobx.netlify.app
topics:
- reactive-programming
- state-management
- ui
- user-interface

environment:
sdk: ">=3.0.0 <4.0.0"
Expand All @@ -24,7 +24,7 @@ dependencies:

dev_dependencies:
build_runner: ^2.4.9
flutter_lints: ^4.0.0
flutter_lints: ^5.0.0
flutter_test:
sdk: flutter
mocktail: ^1.0.3
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: 1 addition & 1 deletion mobx_examples/lib/todos/todo_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class ActionBar extends StatelessWidget {
},
),
Observer(
builder: (_) => ButtonBar(
builder: (_) => OverflowBar(
children: <Widget>[
ElevatedButton(
onPressed: list.canRemoveAllCompleted
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

0 comments on commit 4ede8a3

Please sign in to comment.