Skip to content

Commit 025b21b

Browse files
authored
feat(flutter_bloc): override debugFillProperties (#4082)
1 parent 95b7fee commit 025b21b

12 files changed

+209
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependency_overrides:
2+
flutter_bloc:
3+
path: ../

packages/flutter_bloc/lib/src/bloc_builder.dart

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/foundation.dart';
12
import 'package:flutter/widgets.dart';
23
import 'package:flutter_bloc/flutter_bloc.dart';
34

@@ -88,6 +89,14 @@ class BlocBuilder<B extends StateStreamable<S>, S>
8889

8990
@override
9091
Widget build(BuildContext context, S state) => builder(context, state);
92+
93+
@override
94+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
95+
super.debugFillProperties(properties);
96+
properties.add(
97+
ObjectFlagProperty<BlocWidgetBuilder<S>>.has('builder', builder),
98+
);
99+
}
91100
}
92101

93102
/// {@template bloc_builder_base}
@@ -117,6 +126,19 @@ abstract class BlocBuilderBase<B extends StateStreamable<S>, S>
117126

118127
@override
119128
State<BlocBuilderBase<B, S>> createState() => _BlocBuilderBaseState<B, S>();
129+
130+
@override
131+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
132+
super.debugFillProperties(properties);
133+
properties
134+
..add(
135+
ObjectFlagProperty<BlocBuilderCondition<S>?>.has(
136+
'buildWhen',
137+
buildWhen,
138+
),
139+
)
140+
..add(DiagnosticsProperty<B?>('bloc', bloc));
141+
}
120142
}
121143

122144
class _BlocBuilderBaseState<B extends StateStreamable<S>, S>

packages/flutter_bloc/lib/src/bloc_consumer.dart

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/foundation.dart';
12
import 'package:flutter/widgets.dart';
23
import 'package:flutter_bloc/flutter_bloc.dart';
34

@@ -96,6 +97,27 @@ class BlocConsumer<B extends StateStreamable<S>, S> extends StatefulWidget {
9697

9798
@override
9899
State<BlocConsumer<B, S>> createState() => _BlocConsumerState<B, S>();
100+
101+
@override
102+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
103+
super.debugFillProperties(properties);
104+
properties
105+
..add(DiagnosticsProperty<B?>('bloc', bloc))
106+
..add(ObjectFlagProperty<BlocWidgetBuilder<S>>.has('builder', builder))
107+
..add(ObjectFlagProperty<BlocWidgetListener<S>>.has('listener', listener))
108+
..add(
109+
ObjectFlagProperty<BlocBuilderCondition<S>?>.has(
110+
'buildWhen',
111+
buildWhen,
112+
),
113+
)
114+
..add(
115+
ObjectFlagProperty<BlocListenerCondition<S>?>.has(
116+
'listenWhen',
117+
listenWhen,
118+
),
119+
);
120+
}
99121
}
100122

101123
class _BlocConsumerState<B extends StateStreamable<S>, S>

packages/flutter_bloc/lib/src/bloc_listener.dart

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:async';
22

3+
import 'package:flutter/foundation.dart';
34
import 'package:flutter/widgets.dart';
45
import 'package:flutter_bloc/flutter_bloc.dart';
56
import 'package:provider/single_child_widget.dart';
@@ -129,6 +130,20 @@ abstract class BlocListenerBase<B extends StateStreamable<S>, S>
129130
@override
130131
SingleChildState<BlocListenerBase<B, S>> createState() =>
131132
_BlocListenerBaseState<B, S>();
133+
134+
@override
135+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
136+
super.debugFillProperties(properties);
137+
properties
138+
..add(DiagnosticsProperty<B?>('bloc', bloc))
139+
..add(ObjectFlagProperty<BlocWidgetListener<S>>.has('listener', listener))
140+
..add(
141+
ObjectFlagProperty<BlocListenerCondition<S>?>.has(
142+
'listenWhen',
143+
listenWhen,
144+
),
145+
);
146+
}
132147
}
133148

134149
class _BlocListenerBaseState<B extends StateStreamable<S>, S>

packages/flutter_bloc/lib/src/bloc_provider.dart

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:bloc/bloc.dart';
2+
import 'package:flutter/foundation.dart';
23
import 'package:flutter/widgets.dart';
34
import 'package:provider/provider.dart';
45
import 'package:provider/single_child_widget.dart';
@@ -141,4 +142,10 @@ class BlocProvider<T extends StateStreamableSource<Object?>>
141142
);
142143
return subscription.cancel;
143144
}
145+
146+
@override
147+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
148+
super.debugFillProperties(properties);
149+
properties.add(DiagnosticsProperty<bool>('lazy', lazy));
150+
}
144151
}

packages/flutter_bloc/lib/src/bloc_selector.dart

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/foundation.dart';
12
import 'package:flutter/widgets.dart';
23
import 'package:flutter_bloc/flutter_bloc.dart';
34

@@ -52,6 +53,20 @@ class BlocSelector<B extends StateStreamable<S>, S, T> extends StatefulWidget {
5253

5354
@override
5455
State<BlocSelector<B, S, T>> createState() => _BlocSelectorState<B, S, T>();
56+
57+
@override
58+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
59+
super.debugFillProperties(properties);
60+
properties
61+
..add(DiagnosticsProperty<B?>('bloc', bloc))
62+
..add(ObjectFlagProperty<BlocWidgetBuilder<T>>.has('builder', builder))
63+
..add(
64+
ObjectFlagProperty<BlocWidgetSelector<S, T>>.has(
65+
'selector',
66+
selector,
67+
),
68+
);
69+
}
5570
}
5671

5772
class _BlocSelectorState<B extends StateStreamable<S>, S, T>

packages/flutter_bloc/test/bloc_builder_test.dart

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/foundation.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_bloc/flutter_bloc.dart';
34
import 'package:flutter_test/flutter_test.dart';
@@ -53,6 +54,7 @@ class MyThemeAppState extends State<MyThemeApp> {
5354
key: const Key('raised_button_2'),
5455
child: const SizedBox(),
5556
onPressed: () {
57+
// ignore: no_self_assignments
5658
setState(() => _themeCubit = _themeCubit);
5759
},
5860
),
@@ -74,7 +76,6 @@ class ThemeCubit extends Cubit<ThemeData> {
7476
class DarkThemeCubit extends Cubit<ThemeData> {
7577
DarkThemeCubit() : super(ThemeData.dark());
7678

77-
void setDarkTheme() => emit(ThemeData.dark());
7879
void setLightTheme() => emit(ThemeData.light());
7980
}
8081

@@ -132,7 +133,6 @@ class CounterCubit extends Cubit<int> {
132133
CounterCubit({int seed = 0}) : super(seed);
133134

134135
void increment() => emit(state + 1);
135-
void decrement() => emit(state - 1);
136136
}
137137

138138
void main() {
@@ -518,5 +518,29 @@ void main() {
518518

519519
expect(find.text('Count 101'), findsOneWidget);
520520
});
521+
522+
testWidgets('overrides debugFillProperties', (tester) async {
523+
final builder = DiagnosticPropertiesBuilder();
524+
525+
BlocBuilder(
526+
bloc: CounterCubit(),
527+
builder: (context, state) => const SizedBox(),
528+
buildWhen: (previous, current) => previous != current,
529+
).debugFillProperties(builder);
530+
531+
final description = builder.properties
532+
.where((node) => !node.isFiltered(DiagnosticLevel.info))
533+
.map((node) => node.toString())
534+
.toList();
535+
536+
expect(
537+
description,
538+
<String>[
539+
'has buildWhen',
540+
"bloc: Instance of 'CounterCubit'",
541+
'has builder',
542+
],
543+
);
544+
});
521545
});
522546
}

packages/flutter_bloc/test/bloc_consumer_test.dart

+29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/foundation.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_bloc/flutter_bloc.dart';
34
import 'package:flutter_test/flutter_test.dart';
@@ -399,5 +400,33 @@ void main() {
399400
expect(find.text('Count 101'), findsOneWidget);
400401
expect(states, expectedStates);
401402
});
403+
404+
testWidgets('overrides debugFillProperties', (tester) async {
405+
final builder = DiagnosticPropertiesBuilder();
406+
407+
BlocConsumer(
408+
bloc: CounterCubit(),
409+
buildWhen: (previous, current) => previous != current,
410+
builder: (context, state) => const SizedBox(),
411+
listener: (context, state) {},
412+
listenWhen: (previous, current) => previous != current,
413+
).debugFillProperties(builder);
414+
415+
final description = builder.properties
416+
.where((node) => !node.isFiltered(DiagnosticLevel.info))
417+
.map((node) => node.toString())
418+
.toList();
419+
420+
expect(
421+
description,
422+
<String>[
423+
"bloc: Instance of 'CounterCubit'",
424+
'has builder',
425+
'has listener',
426+
'has buildWhen',
427+
'has listenWhen',
428+
],
429+
);
430+
});
402431
});
403432
}

packages/flutter_bloc/test/bloc_listener_test.dart

+27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/foundation.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_bloc/flutter_bloc.dart';
34
import 'package:flutter_test/flutter_test.dart';
@@ -54,6 +55,7 @@ class _MyAppState extends State<MyApp> {
5455
key: const Key('cubit_listener_noop_button'),
5556
child: const SizedBox(),
5657
onPressed: () {
58+
// ignore: no_self_assignments
5759
setState(() => _counterCubit = _counterCubit);
5860
},
5961
),
@@ -504,5 +506,30 @@ void main() {
504506

505507
expect(states, expectedStates);
506508
});
509+
510+
testWidgets('overrides debugFillProperties', (tester) async {
511+
final builder = DiagnosticPropertiesBuilder();
512+
513+
BlocListener(
514+
bloc: CounterCubit(),
515+
listener: (context, state) {},
516+
listenWhen: (previous, current) => previous != current,
517+
child: const SizedBox(),
518+
).debugFillProperties(builder);
519+
520+
final description = builder.properties
521+
.where((node) => !node.isFiltered(DiagnosticLevel.info))
522+
.map((node) => node.toString())
523+
.toList();
524+
525+
expect(
526+
description,
527+
<String>[
528+
"bloc: Instance of 'CounterCubit'",
529+
'has listener',
530+
'has listenWhen',
531+
],
532+
);
533+
});
507534
});
508535
}

packages/flutter_bloc/test/bloc_provider_test.dart

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:async';
22

3+
import 'package:flutter/foundation.dart';
34
import 'package:flutter/material.dart';
45
import 'package:flutter_bloc/flutter_bloc.dart';
56
import 'package:flutter_test/flutter_test.dart';
@@ -727,5 +728,21 @@ void main() {
727728
);
728729
expect(tester.takeException(), isNull);
729730
});
731+
732+
testWidgets('overrides debugFillProperties', (tester) async {
733+
final builder = DiagnosticPropertiesBuilder();
734+
735+
BlocProvider(
736+
create: (context) => CounterCubit(),
737+
child: const SizedBox(),
738+
).debugFillProperties(builder);
739+
740+
final description = builder.properties
741+
.where((node) => !node.isFiltered(DiagnosticLevel.info))
742+
.map((node) => node.toString())
743+
.toList();
744+
745+
expect(description, <String>['lazy: true']);
746+
});
730747
});
731748
}

packages/flutter_bloc/test/bloc_selector_test.dart

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/foundation.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_bloc/flutter_bloc.dart';
34
import 'package:flutter_test/flutter_test.dart';
@@ -182,5 +183,29 @@ void main() {
182183
expect(find.text('isEven: false'), findsOneWidget);
183184
expect(find.text('isEven: true'), findsNothing);
184185
});
186+
187+
testWidgets('overrides debugFillProperties', (tester) async {
188+
final builder = DiagnosticPropertiesBuilder();
189+
190+
BlocSelector(
191+
bloc: CounterCubit(),
192+
builder: (context, state) => const SizedBox(),
193+
selector: (state) => state,
194+
).debugFillProperties(builder);
195+
196+
final description = builder.properties
197+
.where((node) => !node.isFiltered(DiagnosticLevel.info))
198+
.map((node) => node.toString())
199+
.toList();
200+
201+
expect(
202+
description,
203+
<String>[
204+
"bloc: Instance of 'CounterCubit'",
205+
'has builder',
206+
'has selector',
207+
],
208+
);
209+
});
185210
});
186211
}

packages/flutter_bloc/test/repository_provider_test.dart

+1-20
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MyApp extends StatelessWidget {
1616

1717
@override
1818
Widget build(BuildContext context) {
19-
if (useValueProvider == true) {
19+
if (useValueProvider) {
2020
return MaterialApp(
2121
home: RepositoryProvider<Repository>.value(
2222
value: repository,
@@ -96,25 +96,6 @@ class CounterPage extends StatelessWidget {
9696
}
9797
}
9898

99-
class RoutePage extends StatelessWidget {
100-
const RoutePage({Key? key}) : super(key: key);
101-
102-
@override
103-
Widget build(BuildContext context) {
104-
return Scaffold(
105-
body: ElevatedButton(
106-
key: const Key('route_button'),
107-
child: const SizedBox(),
108-
onPressed: () {
109-
Navigator.of(context).pushReplacement(
110-
MaterialPageRoute<Widget>(builder: (context) => const SizedBox()),
111-
);
112-
},
113-
),
114-
);
115-
}
116-
}
117-
11899
class Repository {
119100
const Repository(this.data);
120101

0 commit comments

Comments
 (0)