Skip to content

Commit 613333e

Browse files
committed
bloc to bloc communication - BlocListener
1 parent dad32b9 commit 613333e

File tree

4 files changed

+102
-126
lines changed

4 files changed

+102
-126
lines changed
+1-29
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,14 @@
1-
import 'dart:async';
2-
31
import 'package:equatable/equatable.dart';
42
import 'package:flutter_bloc/flutter_bloc.dart';
5-
import 'package:new_bloc/constants/enums.dart';
6-
import 'package:new_bloc/logic/cubit/internet_cubit.dart';
73

84
part 'counter_state.dart';
95

106
class CounterCubit extends Cubit<CounterState> {
11-
final InternetCubit internetCubit;
12-
late StreamSubscription internetStreamSubscription;
13-
14-
CounterCubit({required this.internetCubit})
15-
: super(const CounterState(countValue: 0)) {
16-
monitorInternetCubit();
17-
}
18-
19-
StreamSubscription<InternetState> monitorInternetCubit() {
20-
return internetStreamSubscription = internetCubit.listen((internetState) {
21-
if (internetState is InternetConnected &&
22-
internetState.connectionType == ConnectionType.Wifi) {
23-
increment();
24-
} else if (internetState is InternetConnected &&
25-
internetState.connectionType == ConnectionType.Mobile) {
26-
decrement();
27-
}
28-
});
29-
}
7+
CounterCubit() : super(const CounterState(countValue: 0));
308

319
void increment() => emit(
3210
CounterState(countValue: state.countValue + 1, wasIncremented: true));
3311

3412
void decrement() => emit(
3513
CounterState(countValue: state.countValue - 1, wasIncremented: false));
36-
37-
@override
38-
Future<void> close() {
39-
internetStreamSubscription.cancel();
40-
return super.close();
41-
}
4214
}

new_bloc/lib/logic/cubit/internet_cubit.dart

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:async';
2-
32
import 'package:connectivity_plus/connectivity_plus.dart';
43
import 'package:flutter/cupertino.dart';
54
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -16,17 +15,15 @@ class InternetCubit extends Cubit<InternetState> {
1615

1716
StreamSubscription<ConnectivityResult> monitorInternetConnection() {
1817
return connectivityStreamSubscription =
19-
connectivity.onConnectivityChanged.listen(
20-
(event) {
21-
if (event == ConnectivityResult.wifi) {
22-
emitInternetConnected(ConnectionType.Wifi);
23-
} else if (event == ConnectivityResult.mobile) {
24-
emitInternetConnected(ConnectionType.Mobile);
25-
} else if (event == ConnectivityResult.none) {
26-
emitInternetDisconnected();
27-
}
28-
},
29-
);
18+
connectivity.onConnectivityChanged.listen((event) {
19+
if (event == ConnectivityResult.wifi) {
20+
emitInternetConnected(ConnectionType.Wifi);
21+
} else if (event == ConnectivityResult.mobile) {
22+
emitInternetConnected(ConnectionType.Mobile);
23+
} else if (event == ConnectivityResult.none) {
24+
emitInternetDisconnected();
25+
}
26+
});
3027
}
3128

3229
void emitInternetConnected(ConnectionType _connectionType) =>

new_bloc/lib/main.dart

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class MyApp extends StatelessWidget {
1818
const MyApp({Key? key, required this.appRouter, required this.connectivity})
1919
: super(key: key);
2020

21-
// final CounterCubit _counterCubit = CounterCubit();
22-
2321
@override
2422
Widget build(BuildContext context) {
2523
return MultiBlocProvider(
@@ -28,9 +26,7 @@ class MyApp extends StatelessWidget {
2826
create: (_) => InternetCubit(connectivity: connectivity),
2927
),
3028
BlocProvider<CounterCubit>(
31-
create: (_) => CounterCubit(
32-
internetCubit: BlocProvider.of<InternetCubit>(context),
33-
),
29+
create: (_) => CounterCubit(),
3430
),
3531
],
3632
child: MaterialApp(

new_bloc/lib/presentation/screens/home_screen.dart

+91-80
Original file line numberDiff line numberDiff line change
@@ -17,86 +17,97 @@ class HomeScreen extends StatefulWidget {
1717
class _HomeScreenState extends State<HomeScreen> {
1818
@override
1919
Widget build(BuildContext context) {
20-
return Scaffold(
21-
appBar: AppBar(
22-
title: Text(widget.title),
23-
backgroundColor: widget.color,
24-
),
25-
body: Center(
26-
child: Column(
27-
mainAxisAlignment: MainAxisAlignment.center,
28-
children: <Widget>[
29-
BlocBuilder<InternetCubit, InternetState>(
30-
builder: (context, state) {
31-
if (state is InternetConnected &&
32-
state.connectionType == ConnectionType.Wifi) {
33-
return const Text('Wifi');
34-
} else if (state is InternetConnected &&
35-
state.connectionType == ConnectionType.Mobile) {
36-
return const Text('Mobile');
37-
} else if (state is InternetDisconnected) {
38-
return const Text('Disconnected');
39-
}
40-
return const CircularProgressIndicator();
41-
},
42-
),
43-
const Text('You have pushed the button this many times:'),
44-
BlocConsumer<CounterCubit, CounterState>(
45-
listener: (context, state) {
46-
if (state.wasIncremented == true) {
47-
ScaffoldMessenger.of(context).showSnackBar(
48-
const SnackBar(content: Text('Incremented')));
49-
} else {
50-
ScaffoldMessenger.of(context).showSnackBar(
51-
const SnackBar(content: Text('Decremented')));
52-
}
53-
},
54-
builder: (context, state) {
55-
return Text(
56-
'${state.countValue}',
57-
style: Theme.of(context).textTheme.headline4,
58-
);
59-
},
60-
),
61-
const SizedBox(height: 12),
62-
Row(
63-
mainAxisAlignment: MainAxisAlignment.spaceAround,
64-
children: [
65-
FloatingActionButton(
66-
onPressed: () {
67-
BlocProvider.of<CounterCubit>(context).increment();
68-
},
69-
tooltip: 'Increment',
70-
backgroundColor: widget.color,
71-
child: const Icon(Icons.add),
72-
),
73-
FloatingActionButton(
74-
onPressed: () {
75-
BlocProvider.of<CounterCubit>(context).decrement();
76-
},
77-
tooltip: 'Decrement',
78-
backgroundColor: widget.color,
79-
child: const Icon(Icons.remove),
80-
),
81-
],
82-
),
83-
const SizedBox(height: 24),
84-
MaterialButton(
85-
onPressed: () {
86-
Navigator.of(context).pushNamed('/second');
87-
},
88-
color: widget.color,
89-
child: const Text('Go to Second Screen'),
90-
),
91-
const SizedBox(height: 24),
92-
MaterialButton(
93-
onPressed: () {
94-
Navigator.of(context).pushNamed('/third');
95-
},
96-
color: widget.color,
97-
child: const Text('Go to Third Screen'),
98-
),
99-
],
20+
return BlocListener<InternetCubit, InternetState>(
21+
listener: (context, state) {
22+
if (state is InternetConnected &&
23+
state.connectionType == ConnectionType.Wifi) {
24+
BlocProvider.of<CounterCubit>(context).increment();
25+
} else if (state is InternetConnected &&
26+
state.connectionType == ConnectionType.Mobile) {
27+
BlocProvider.of<CounterCubit>(context).decrement();
28+
}
29+
},
30+
child: Scaffold(
31+
appBar: AppBar(
32+
title: Text(widget.title),
33+
backgroundColor: widget.color,
34+
),
35+
body: Center(
36+
child: Column(
37+
mainAxisAlignment: MainAxisAlignment.center,
38+
children: <Widget>[
39+
BlocBuilder<InternetCubit, InternetState>(
40+
builder: (context, state) {
41+
if (state is InternetConnected &&
42+
state.connectionType == ConnectionType.Wifi) {
43+
return const Text('Wifi');
44+
} else if (state is InternetConnected &&
45+
state.connectionType == ConnectionType.Mobile) {
46+
return const Text('Mobile');
47+
} else if (state is InternetDisconnected) {
48+
return const Text('Disconnected');
49+
}
50+
return const CircularProgressIndicator();
51+
},
52+
),
53+
const Text('You have pushed the button this many times:'),
54+
BlocConsumer<CounterCubit, CounterState>(
55+
listener: (context, state) {
56+
if (state.wasIncremented == true) {
57+
ScaffoldMessenger.of(context).showSnackBar(
58+
const SnackBar(content: Text('Incremented')));
59+
} else {
60+
ScaffoldMessenger.of(context).showSnackBar(
61+
const SnackBar(content: Text('Decremented')));
62+
}
63+
},
64+
builder: (context, state) {
65+
return Text(
66+
'${state.countValue}',
67+
style: Theme.of(context).textTheme.headline4,
68+
);
69+
},
70+
),
71+
const SizedBox(height: 12),
72+
Row(
73+
mainAxisAlignment: MainAxisAlignment.spaceAround,
74+
children: [
75+
FloatingActionButton(
76+
onPressed: () {
77+
BlocProvider.of<CounterCubit>(context).increment();
78+
},
79+
tooltip: 'Increment',
80+
backgroundColor: widget.color,
81+
child: const Icon(Icons.add),
82+
),
83+
FloatingActionButton(
84+
onPressed: () {
85+
BlocProvider.of<CounterCubit>(context).decrement();
86+
},
87+
tooltip: 'Decrement',
88+
backgroundColor: widget.color,
89+
child: const Icon(Icons.remove),
90+
),
91+
],
92+
),
93+
const SizedBox(height: 24),
94+
MaterialButton(
95+
onPressed: () {
96+
Navigator.of(context).pushNamed('/second');
97+
},
98+
color: widget.color,
99+
child: const Text('Go to Second Screen'),
100+
),
101+
const SizedBox(height: 24),
102+
MaterialButton(
103+
onPressed: () {
104+
Navigator.of(context).pushNamed('/third');
105+
},
106+
color: widget.color,
107+
child: const Text('Go to Third Screen'),
108+
),
109+
],
110+
),
100111
),
101112
),
102113
);

0 commit comments

Comments
 (0)