Created
April 30, 2019 19:58
-
-
Save axellebot/f79579b11ab2ccfa3857b20d62068127 to your computer and use it in GitHub Desktop.
BlocListener Notifications
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:bloc/bloc.dart'; | |
class BlocA extends Bloc<BlocATriggered, BlocAState> { | |
@override | |
BlocAState get initialState => BlocAUninitialized(); | |
@override | |
Stream<BlocAState> mapEventToState(BlocAEvent event) async* { | |
if (event is BlocATriggered) { | |
yield BlocALoading(); | |
Future.delayed(Duration(seconds: 1)); | |
yield BlocASucceed(); | |
} | |
} | |
} | |
abstract class BlocAEvent {} | |
class BlocATriggered extends BlocAEvent {} | |
abstract class BlocAState {} | |
class BlocAUninitialized extends BlocAState { | |
@override | |
String toString() { | |
return 'BlocAUninitialized{}'; | |
} | |
} | |
class BlocALoading extends BlocAState {} | |
class BlocASucceed extends BlocAState { | |
@override | |
String toString() { | |
return 'BlocASucceed{}'; | |
} | |
} |
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:bloc/bloc.dart'; | |
class BlocB extends Bloc<BlocBTriggered, BlocBState> { | |
@override | |
BlocBState get initialState => BlocBUninitialized(); | |
@override | |
Stream<BlocBState> mapEventToState(BlocBEvent event) async* { | |
if (event is BlocBTriggered) { | |
yield BlocBLoading(); | |
Future.delayed(Duration(seconds: 1)); | |
yield BlocBSucceed(); | |
} | |
} | |
} | |
abstract class BlocBEvent {} | |
class BlocBTriggered extends BlocBEvent {} | |
abstract class BlocBState {} | |
class BlocBLoading extends BlocBState { | |
@override | |
String toString() { | |
return 'BlocBLoading{}'; | |
} | |
} | |
class BlocBUninitialized extends BlocBState { | |
@override | |
String toString() { | |
return 'BlocBSucceed{}'; | |
} | |
} | |
class BlocBSucceed extends BlocBState { | |
@override | |
String toString() { | |
return 'BlocBSucceed{}'; | |
} | |
} |
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:flutter_bloc/flutter_bloc.dart'; | |
import 'bloc_a.dart'; | |
import 'bloc_b.dart'; | |
import 'main_page.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
// This widget is the root of your application. | |
BlocA blocA; | |
BlocB blocB; | |
@override | |
void initState() { | |
super.initState(); | |
blocA = BlocA(); | |
blocB = BlocB(); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
blocA.dispose(); | |
blocB.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Snackbar Test', | |
theme: ThemeData(), | |
home: BlocProviderTree( | |
blocProviders: [ | |
BlocProvider<BlocA>(bloc: blocA), | |
BlocProvider<BlocB>(bloc: blocB), | |
], | |
child: MainPage(), | |
), | |
); | |
} | |
} |
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 'page_a.dart'; | |
import 'page_b.dart'; | |
class MainPage extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() => _MainPageState(); | |
} | |
class _MainPageState extends State<MainPage> { | |
int _activeBody = 0; | |
final _widgetOptions = [ | |
PageA(), | |
PageB(), | |
]; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: _widgetOptions.elementAt(_activeBody), | |
bottomNavigationBar: BottomNavigationBar( | |
currentIndex: _activeBody, | |
items: [ | |
BottomNavigationBarItem( | |
icon: Icon(Icons.cloud_done), | |
title: Text('A'), | |
), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.all_inclusive), | |
title: Text('B'), | |
) | |
], | |
onTap: _onItemSelected, | |
), | |
); | |
} | |
void _onItemSelected(int value) { | |
setState(() { | |
_activeBody = value; | |
}); | |
} | |
} |
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:flutter_bloc/flutter_bloc.dart'; | |
import 'bloc_a.dart'; | |
class PageA extends StatelessWidget { | |
final String _tag = 'PageA'; | |
@override | |
Widget build(BuildContext context) { | |
print('$_tag:build'); | |
BlocA blocA = BlocProvider.of<BlocA>(context); | |
return BlocListener( | |
bloc: blocA, | |
listener: (BuildContext context, BlocAState state) { | |
if (state is BlocASucceed) { | |
Scaffold.of(context).showSnackBar(SnackBar( | |
content: Text('${state.toString()}'), | |
)); | |
} | |
}, | |
child: Center( | |
child: FlatButton( | |
child: Text('Trigger Bloc A'), | |
onPressed: () => blocA.dispatch(BlocATriggered()), | |
), | |
), | |
); | |
} | |
} |
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:flutter_bloc/flutter_bloc.dart'; | |
import 'bloc_b.dart'; | |
class PageB extends StatelessWidget { | |
final String _tag = 'PageB'; | |
@override | |
Widget build(BuildContext context) { | |
print('$_tag:build'); | |
BlocB blocB = BlocProvider.of<BlocB>(context); | |
return BlocListener( | |
bloc: blocB, | |
listener: (BuildContext context, BlocBState state) { | |
if (state is BlocBSucceed) { | |
Scaffold.of(context).showSnackBar(SnackBar( | |
content: Text('${state.toString()}'), | |
)); | |
} | |
}, | |
child: Center( | |
child: FlatButton( | |
child: Text('Trigger Bloc B'), | |
onPressed: () => blocB.dispatch(BlocBTriggered()), | |
), | |
), | |
); | |
} | |
} |
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
name: bloc_listener_test | |
description: A new Flutter project. | |
version: 1.0.0+1 | |
environment: | |
sdk: ">=2.1.0 <3.0.0" | |
dependencies: | |
flutter: | |
sdk: flutter | |
flutter_bloc: ^0.11.1 | |
dev_dependencies: | |
flutter_test: | |
sdk: flutter | |
flutter: | |
uses-material-design: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment