// https://bloclibrary.dev/#/recipesflutternavigation?id=direct-navigation |
<> |
// https://bloclibrary.dev/#/recipesflutternavigation?id=route-navigation |
// Direct Navigation version |
|
// Route Navigation version |
// Code: https://gist.github.com/felangel/386c840aad41c7675ab8695f15c4cb09 |
|
// Code: https://gist.github.com/felangel/6bcd4be10c046ceb33eecfeb380135dd |
|
|
|
|
= |
|
import 'package:flutter/material.dart'; |
|
import 'package:flutter/material.dart'; |
import 'package:meta/meta.dart'; |
|
import 'package:meta/meta.dart'; |
import 'package:bloc/bloc.dart'; |
|
import 'package:bloc/bloc.dart'; |
import 'package:flutter_bloc/flutter_bloc.dart'; |
|
import 'package:flutter_bloc/flutter_bloc.dart'; |
|
|
|
void main() { |
|
void main() { |
runApp( |
|
runApp( |
BlocProvider( |
|
BlocProvider( |
builder: (context) => MyBloc(), |
|
builder: (context) => MyBloc(), |
child: MyApp(), |
|
child: MyApp(), |
), |
|
), |
); |
|
); |
} |
|
} |
|
|
|
enum MyEvent { eventA, eventB } |
|
enum MyEvent { eventA, eventB } |
|
|
|
@immutable |
|
@immutable |
abstract class MyState {} |
|
abstract class MyState {} |
|
|
|
class StateA extends MyState {} |
|
class StateA extends MyState {} |
|
|
|
class StateB extends MyState {} |
|
class StateB extends MyState {} |
|
|
|
class MyBloc extends Bloc<MyEvent, MyState> { |
|
class MyBloc extends Bloc<MyEvent, MyState> { |
@override |
|
@override |
MyState get initialState => StateA(); |
|
MyState get initialState => StateA(); |
|
|
|
@override |
|
@override |
Stream<MyState> mapEventToState(MyEvent event) async* { |
|
Stream<MyState> mapEventToState(MyEvent event) async* { |
switch (event) { |
|
switch (event) { |
case MyEvent.eventA: |
|
case MyEvent.eventA: |
yield StateA(); |
|
yield StateA(); |
break; |
|
break; |
case MyEvent.eventB: |
|
case MyEvent.eventB: |
yield StateB(); |
|
yield StateB(); |
break; |
|
break; |
} |
|
} |
} |
|
} |
} |
|
} |
|
|
|
class MyApp extends StatelessWidget { |
|
class MyApp extends StatelessWidget { |
@override |
|
@override |
Widget build(BuildContext context) { |
|
Widget build(BuildContext context) { |
return MaterialApp( |
|
return MaterialApp( |
home: BlocBuilder<MyBloc, MyState>( |
<> |
routes: { |
builder: (_, state) => state is StateA ? PageA() : PageB(), |
|
'/': (context) => PageA(), |
|
|
'/pageB': (context) => PageB(), |
), |
|
}, |
|
|
initialRoute: '/', |
); |
= |
); |
} |
|
} |
} |
|
} |
|
|
|
class PageA extends StatelessWidget { |
|
class PageA extends StatelessWidget { |
@override |
|
@override |
Widget build(BuildContext context) { |
|
Widget build(BuildContext context) { |
|
<> |
return BlocListener<MyBloc, MyState>( |
|
|
listener: (context, state) { |
|
|
if (state is StateB) { |
|
|
Navigator.of(context).pushNamed('/pageB'); |
|
|
} |
|
|
}, |
return Scaffold( |
|
child: Scaffold( |
appBar: AppBar( |
|
appBar: AppBar( |
title: Text('Page A'), |
|
title: Text('Page A'), |
), |
|
), |
body: Center( |
|
body: Center( |
child: RaisedButton( |
|
child: RaisedButton( |
child: Text('Go to PageB'), |
|
child: Text('Go to PageB'), |
onPressed: () { |
|
onPressed: () { |
BlocProvider.of<MyBloc>(context).add(MyEvent.eventB); |
|
BlocProvider.of<MyBloc>(context).add(MyEvent.eventB); |
}, |
|
}, |
|
|
), |
), |
= |
), |
), |
|
), |
); |
|
); |
} |
|
} |
} |
|
} |
|
|
|
class PageB extends StatelessWidget { |
|
class PageB extends StatelessWidget { |
@override |
|
@override |
Widget build(BuildContext context) { |
|
Widget build(BuildContext context) { |
return Scaffold( |
|
return Scaffold( |
appBar: AppBar( |
|
appBar: AppBar( |
title: Text('Page B'), |
|
title: Text('Page B'), |
), |
|
), |
body: Center( |
|
body: Center( |
child: RaisedButton( |
|
child: RaisedButton( |
child: Text('Go to PageA'), |
<> |
child: Text('Pop'), |
onPressed: () { |
= |
onPressed: () { |
BlocProvider.of<MyBloc>(context).add(MyEvent.eventA); |
<> |
Navigator.of(context).pop(); |
}, |
= |
}, |
), |
|
), |
), |
|
), |
); |
|
); |
} |
|
} |
} |
|
} |