Skip to content

Commit

Permalink
fixed sublistenable parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
clragon committed Apr 5, 2023
1 parent 09cc1ed commit 4df841e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.2

- Fixed SubListenanble requiring a builder instead of a child

## 1.0.1

- Fixed an issue with SubListenable calling its listener too much
Expand Down
17 changes: 14 additions & 3 deletions lib/src/sub_listenable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class SubListener extends StatelessWidget {
/// - The optional [listener] is called when the listenable notifies.
/// - If [initialize] is true, then [listener] is also called once this Widget is built for the first time.
const SubListener({
required this.builder,
@required this.child,
@Deprecated('Use the child Parameter instead') this.builder,
required this.listenable,
this.listener,
this.initialize = false,
Expand All @@ -38,11 +39,20 @@ class SubListener extends StatelessWidget {
/// Whether to call [listener] when the [SubListener] is created initially.
final bool initialize;

/// The widget below this one.
final Widget? child;

/// Builds the widget below this one.
final WidgetBuilder builder;
@Deprecated('Use the child Parameter instead')
final WidgetBuilder? builder;

@override
Widget build(BuildContext context) {
assert(
// ignore: deprecated_member_use_from_same_package
builder != null || child != null,
'$runtimeType must have a child widget!',
);
return SubValue<_SubListenerState>(
create: () => _SubListenerState(
listenable: listenable,
Expand All @@ -51,7 +61,8 @@ class SubListener extends StatelessWidget {
),
keys: [listenable],
dispose: (value) => value.dispose(),
builder: (context, _) => builder(context),
// ignore: deprecated_member_use_from_same_package
builder: (context, _) => builder?.call(context) ?? child!,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/sub_value_listenable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SubValueListener<T> extends SubListener {
}) : super(
listenable: listenable,
listener: listener != null ? () => listener(listenable.value) : null,
builder: (context) => ValueListenableBuilder<T>(
child: ValueListenableBuilder<T>(
valueListenable: listenable,
builder: (context, value, child) => builder(context, value),
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_sub
description: Idiomatic State Lifecycles for Flutter. Part of the State, part of the Tree.
repository: https://github.com/clragon/flutter_sub/

version: 1.0.1
version: 1.0.2

environment:
sdk: ">=2.17.0 <3.0.0"
Expand Down
22 changes: 18 additions & 4 deletions test/sub_listenable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import 'mock.dart';

void main() {
group('SubListener', () {
Container mockBuilder(BuildContext context) => Container();

testWidgets('calls its listener', (tester) async {
final notifier = ValueNotifier(0);
int counter = 0;
Expand All @@ -16,7 +14,7 @@ void main() {
SubListener(
listenable: notifier,
listener: () => counter++,
builder: mockBuilder,
child: const SizedBox(),
),
);

Expand All @@ -40,12 +38,28 @@ void main() {
initialize: true,
listenable: notifier,
listener: () => counter++,
builder: mockBuilder,
child: const SizedBox(),
),
);

expect(counter, 1);
});

testWidgets('throws if neither child nor builder are used', (tester) async {
final notifier = ValueNotifier(0);
int counter = 0;

await tester.pumpWidget(
// ignore: missing_required_param
SubListener(
initialize: true,
listenable: notifier,
listener: () => counter++,
),
);

expect(tester.takeException(), isAssertionError);
});
});

group('SubDisposableListenable', () {
Expand Down

0 comments on commit 4df841e

Please sign in to comment.