Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:meta/meta.dart';
- import 'package:bloc/bloc.dart';
- import 'package:flutter_bloc/flutter_bloc.dart';
- void main() {
- runApp(
- BlocProvider(
- builder: (context) => MyBloc(),
- child: MyApp(),
- ),
- );
- }
- enum MyEvent { eventA, eventB }
- @immutable
- abstract class MyState {}
- class StateA extends MyState {}
- class StateB extends MyState {}
- class MyBloc extends Bloc<MyEvent, MyState> {
- @override
- MyState get initialState => StateA();
- @override
- Stream<MyState> mapEventToState(MyEvent event) async* {
- switch (event) {
- case MyEvent.eventA:
- yield StateA();
- break;
- case MyEvent.eventB:
- yield StateB();
- break;
- }
- }
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- routes: {
- '/': (context) => PageA(),
- '/pageB': (context) => PageB(),
- },
- initialRoute: '/',
- );
- }
- }
- class PageA extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- final myBloc = BlocProvider.of<MyBloc>(context);
- return BlocListener<MyBloc, MyState>(
- listener: (context, state) {
- if (state is StateB) {
- Navigator.of(context).pushNamed('/pageB');
- }
- },
- child: Scaffold(
- appBar: AppBar(
- title: Text('Page A'),
- ),
- body: Center(
- child: RaisedButton(
- child: Text('Go to PageB'),
- onPressed: () {
- myBloc.dispatch(MyEvent.eventB);
- },
- ),
- ),
- ),
- );
- }
- }
- class PageB extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Page B'),
- ),
- body: Center(
- child: RaisedButton(
- child: Text('Pop'),
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement