Created
December 22, 2018 19:54
-
-
Save felangel/30373d32066946eeceaed868fd56e2bd to your computer and use it in GitHub Desktop.
Sharing Bloc Across Routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:bloc/bloc.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
class SimpleBlocDelegate extends BlocDelegate { | |
@override | |
void onTransition(Transition transition) { | |
print(transition.toString()); | |
} | |
} | |
void main() { | |
BlocSupervisor().delegate = SimpleBlocDelegate(); | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() => MyAppState(); | |
} | |
class MyAppState extends State<MyApp> { | |
final CounterBloc _counterBloc = CounterBloc(); | |
@override | |
Widget build(BuildContext context) { | |
return BlocProvider<CounterBloc>( | |
bloc: _counterBloc, | |
child: MaterialApp( | |
title: 'Flutter Demo', | |
routes: { | |
'/': (BuildContext context) => CounterPage(), | |
'/otherCounter': (BuildContext context) => OtherCounterPage(), | |
}, | |
initialRoute: '/', | |
), | |
); | |
} | |
@override | |
void dispose() { | |
_counterBloc.dispose(); | |
super.dispose(); | |
} | |
} | |
class CounterPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final CounterBloc _counterBloc = BlocProvider.of<CounterBloc>(context); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Counter'), | |
actions: <Widget>[ | |
IconButton( | |
icon: Icon(Icons.navigate_next), | |
onPressed: () { | |
Navigator.of(context).pushNamed('/otherCounter'); | |
}, | |
) | |
], | |
), | |
body: BlocBuilder<CounterEvent, int>( | |
bloc: _counterBloc, | |
builder: (BuildContext context, int count) { | |
return Center( | |
child: Text( | |
'$count', | |
style: TextStyle(fontSize: 24.0), | |
), | |
); | |
}, | |
), | |
floatingActionButton: Column( | |
crossAxisAlignment: CrossAxisAlignment.end, | |
mainAxisAlignment: MainAxisAlignment.end, | |
children: <Widget>[ | |
Padding( | |
padding: EdgeInsets.symmetric(vertical: 5.0), | |
child: FloatingActionButton( | |
heroTag: 0, | |
child: Icon(Icons.add), | |
onPressed: () { | |
_counterBloc.dispatch(Increment()); | |
}, | |
), | |
), | |
Padding( | |
padding: EdgeInsets.symmetric(vertical: 5.0), | |
child: FloatingActionButton( | |
heroTag: 1, | |
child: Icon(Icons.remove), | |
onPressed: () { | |
_counterBloc.dispatch(Decrement()); | |
}, | |
), | |
), | |
], | |
), | |
); | |
} | |
} | |
class OtherCounterPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final CounterBloc _counterBloc = BlocProvider.of<CounterBloc>(context); | |
return Scaffold( | |
appBar: AppBar(title: Text('Other Counter Page')), | |
body: BlocBuilder<CounterEvent, int>( | |
bloc: _counterBloc, | |
builder: (BuildContext context, int count) { | |
return Center( | |
child: Text( | |
'$count', | |
style: TextStyle(fontSize: 24.0), | |
), | |
); | |
}, | |
), | |
floatingActionButton: Column( | |
crossAxisAlignment: CrossAxisAlignment.end, | |
mainAxisAlignment: MainAxisAlignment.end, | |
children: <Widget>[ | |
Padding( | |
padding: EdgeInsets.symmetric(vertical: 5.0), | |
child: FloatingActionButton( | |
heroTag: 2, | |
child: Icon(Icons.add), | |
onPressed: () { | |
_counterBloc.dispatch(Increment()); | |
}, | |
), | |
), | |
Padding( | |
padding: EdgeInsets.symmetric(vertical: 5.0), | |
child: FloatingActionButton( | |
heroTag: 3, | |
child: Icon(Icons.remove), | |
onPressed: () { | |
_counterBloc.dispatch(Decrement()); | |
}, | |
), | |
), | |
], | |
), | |
); | |
} | |
} | |
abstract class CounterEvent {} | |
class Increment extends CounterEvent { | |
@override | |
String toString() => 'Increment'; | |
} | |
class Decrement extends CounterEvent { | |
@override | |
String toString() => 'Decrement'; | |
} | |
class CounterBloc extends Bloc<CounterEvent, int> { | |
@override | |
int get initialState => 0; | |
@override | |
Stream<int> mapEventToState(int currentState, CounterEvent event) async* { | |
if (event is Increment) { | |
yield currentState + 1; | |
} | |
if (event is Decrement) { | |
yield currentState - 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@felangel, I haven't had to use this yet, but why is it an issue sharing a bloc across routes?