Last active
December 30, 2023 10:43
-
-
Save felangel/386c840aad41c7675ab8695f15c4cb09 to your computer and use it in GitHub Desktop.
[flutter_bloc_recipes] Navigation: Direct
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 '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( | |
home: BlocBuilder<MyBloc, MyState>( | |
builder: (_, state) => state is StateA ? PageA() : PageB(), | |
), | |
); | |
} | |
} | |
class PageA extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Page A'), | |
), | |
body: Center( | |
child: RaisedButton( | |
child: Text('Go to PageB'), | |
onPressed: () { | |
BlocProvider.of<MyBloc>(context).add(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('Go to PageA'), | |
onPressed: () { | |
BlocProvider.of<MyBloc>(context).add(MyEvent.eventA); | |
}, | |
), | |
), | |
); | |
} | |
} |
@RaulUrtecho thanks for pointing that out! I've updated the gist to be compatible with flutter_bloc v0.20.0. 👍
updated for flutter_bloc 6.0.1
import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:meta/meta.dart';
void main() {
runApp(
BlocProvider(
create: (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> {
MyBloc() : super(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(
home: BlocBuilder<MyBloc, MyState>(
builder: (_, state) => state is StateA ? PageA() : PageB(),
),
);
}
}
class PageA extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Page A'),
),
body: Center(
child: RaisedButton(
child: Text('Go to PageB'),
onPressed: () {
BlocProvider.of<MyBloc>(context).add(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('Go to PageA'),
onPressed: () {
BlocProvider.of<MyBloc>(context).add(MyEvent.eventA);
},
),
),
);
}
}
updated for flutter_bloc 8.1.3
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
void main() {
runApp(
BlocProvider(
create: (context) => MyBloc(),
child: const MyApp(),
),
);
}
@immutable
sealed class MyEvent {}
final class EventA extends MyEvent {}
final class EventB extends MyEvent {}
@immutable
abstract class MyState {}
class StateA extends MyState {}
class StateB extends MyState {}
class MyBloc extends Bloc<MyEvent, MyState> {
MyBloc() : super(StateA()) {
on<EventA>((event, emit) => emit(StateA()));
on<EventB>((event, emit) => emit(StateB()));
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BlocBuilder<MyBloc, MyState>(
builder: (_, state) => state is StateA ? const PageA() : const PageB(),
),
);
}
}
class PageA extends StatelessWidget {
const PageA({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Page A'),
),
body: Center(
child: ElevatedButton(
child: const Text('Go to PageB'),
onPressed: () {
BlocProvider.of<MyBloc>(context).add(EventB());
},
),
),
);
}
}
class PageB extends StatelessWidget {
const PageB({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Page B'),
),
body: Center(
child: ElevatedButton(
child: const Text('Go to PageA'),
onPressed: () {
BlocProvider.of<MyBloc>(context).add(EventA());
},
),
),
);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got this error in the example. I'm already using the flutter v0.20
'MyEvent' doesn't extend 'Bloc<dynamic, MyState>'.
Try using a type that is or is a subclass of 'Bloc<dynamic, MyState>'.dart(type_argument_not_matching_bounds)