Last active
December 27, 2022 08:47
-
-
Save felangel/8d143cf3b7da38d80de4bcc6f65e9831 to your computer and use it in GitHub Desktop.
Recipe: Bloc Access (Named Routes)
This file contains hidden or 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 'package:flutter/material.dart'; | |
import 'package:bloc/bloc.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
class SimpleBlocDelegate extends BlocDelegate { | |
@override | |
void onEvent(Bloc bloc, Object event) { | |
super.onEvent(bloc, event); | |
print(event); | |
} | |
@override | |
void onTransition(Bloc bloc, Transition transition) { | |
super.onTransition(bloc, transition); | |
print(transition); | |
} | |
@override | |
void onError(Bloc bloc, Object error, StackTrace stacktrace) { | |
super.onError(bloc, error, stacktrace); | |
print(error); | |
} | |
} | |
void main() { | |
BlocSupervisor.delegate = SimpleBlocDelegate(); | |
runApp(App()); | |
} | |
class App extends StatefulWidget { | |
@override | |
_AppState createState() => _AppState(); | |
} | |
class _AppState extends State<App> { | |
final _counterBloc = CounterBloc(); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
routes: { | |
'/': (context) => BlocProvider.value( | |
value: _counterBloc, | |
child: HomePage(), | |
), | |
'/counter': (context) => BlocProvider.value( | |
value: _counterBloc, | |
child: CounterPage(), | |
), | |
}, | |
); | |
} | |
@override | |
void dispose() { | |
_counterBloc.close(); | |
super.dispose(); | |
} | |
} | |
class HomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final counterBloc = BlocProvider.of<CounterBloc>(context); | |
return Scaffold( | |
appBar: AppBar(title: Text('Counter')), | |
body: Center( | |
child: RaisedButton( | |
onPressed: () => Navigator.of(context).pushNamed('/counter'), | |
child: Text('Counter'), | |
), | |
), | |
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.add(CounterEvent.increment); | |
}, | |
), | |
), | |
Padding( | |
padding: EdgeInsets.symmetric(vertical: 5.0), | |
child: FloatingActionButton( | |
heroTag: 1, | |
child: Icon(Icons.remove), | |
onPressed: () { | |
counterBloc.add(CounterEvent.decrement); | |
}, | |
), | |
), | |
], | |
), | |
); | |
} | |
} | |
class CounterPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Counter'), | |
), | |
body: BlocBuilder<CounterBloc, int>( | |
builder: (context, count) { | |
return Center( | |
child: Text('$count'), | |
); | |
}, | |
), | |
); | |
} | |
} | |
enum CounterEvent { increment, decrement } | |
class CounterBloc extends Bloc<CounterEvent, int> { | |
@override | |
int get initialState => 0; | |
@override | |
Stream<int> mapEventToState(CounterEvent event) async* { | |
switch (event) { | |
case CounterEvent.decrement: | |
yield state - 1; | |
break; | |
case CounterEvent.increment: | |
yield state + 1; | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment