Created
August 8, 2021 11:02
-
-
Save AkaashSaini/08177828e699f4ea4255122ebc4603f7 to your computer and use it in GitHub Desktop.
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
//anonymous route sharing a existing instance | |
onPressed: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (_) => BlocProvider.value( | |
value: BlocProvider.of<CounterCubit>(context), | |
child: SecondScreen( | |
title: "Second Page", | |
color: Colors.redAccent, | |
), | |
), | |
), | |
); | |
}, | |
//anonymous route sharing a new instance | |
onPressed: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (_) => BlocProvider.value( | |
value: CounterCubit(), | |
child: SecondScreen( | |
title: "Second Page", | |
color: Colors.redAccent, | |
), | |
), | |
), | |
); | |
}, | |
//Named Route sharing a existing instance | |
class MyApp extends StatefulWidget { | |
// This widget is the root of your application. | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
final CounterCubit _counterCubit = CounterCubit(); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter bloc demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
routes: { | |
'/': (context) => BlocProvider.value( | |
value: _counterCubit, | |
child: HomeScreen( | |
title: "Home Page", | |
color: Colors.blueAccent, | |
), | |
), | |
'/second': (context) => BlocProvider.value( | |
value: _counterCubit, | |
child: SecondScreen( | |
title: "Second Page", | |
color: Colors.redAccent, | |
), | |
), | |
'/third': (context) => BlocProvider.value( | |
value: _counterCubit, | |
child: ThirdScreen( | |
title: "Second Page", | |
color: Colors.greenAccent, | |
), | |
), | |
}, | |
); | |
} | |
@override | |
void dispose() { | |
_counterCubit.close(); | |
super.dispose(); | |
} | |
} | |
//call named route by | |
onPressed: () { | |
Navigator.of(context).pushNamed('/second'); | |
}, | |
// Generated Route | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'package:flutter_bloc_concept2/logic/cubit/counter_cubit.dart'; | |
import 'package:flutter_bloc_concept2/presentation/screens/home_screen.dart'; | |
import 'package:flutter_bloc_concept2/presentation/screens/second_screen.dart'; | |
import 'package:flutter_bloc_concept2/presentation/screens/third_screen.dart'; | |
class AppRouter { | |
final CounterCubit _counterCubit = CounterCubit(); | |
Route? onGenerateRoute(RouteSettings routeSettings) { | |
switch (routeSettings.name) { | |
case '/': | |
return MaterialPageRoute( | |
builder: (_) => BlocProvider.value( | |
value: _counterCubit, | |
child: HomeScreen( | |
title: "Home Page", | |
color: Colors.blueAccent, | |
), | |
), | |
); | |
case '/second': | |
return MaterialPageRoute( | |
builder: (_) => BlocProvider.value( | |
value: _counterCubit, | |
child: SecondScreen( | |
title: "Second Page", | |
color: Colors.redAccent, | |
), | |
), | |
); | |
case '/third': | |
return MaterialPageRoute( | |
builder: (_) => BlocProvider.value( | |
value: _counterCubit, | |
child: ThirdScreen( | |
title: "Third Page", | |
color: Colors.greenAccent, | |
), | |
), | |
); | |
default: | |
return null; | |
} | |
} | |
void dispose() { | |
_counterCubit.close(); | |
} | |
} | |
//call Generated Route | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
final AppRouter _appRouter = AppRouter(); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter bloc demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
onGenerateRoute: _appRouter.onGenerateRoute, | |
); | |
} | |
@override | |
void dispose() { | |
_appRouter.dispose(); | |
super.dispose(); | |
} | |
} | |
//call genereted pressed | |
onPressed: () { | |
Navigator.of(context).pushNamed('/second'); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment