import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyNavigation(),
);
}
}
Route generatePage(child) {
return MaterialPageRoute(builder: (context) => child);
}
class MyNavigation extends StatelessWidget {
const MyNavigation({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MyInheritedWidget(
message: 'Hey, Geekss',
child: Navigator(
onGenerateRoute: (settings) {
switch (settings.name) {
case 'screen1':
return generatePage(const FirstScreen());
case 'screen2':
return generatePage(const SecondScreen());
}
},
initialRoute: 'screen1',
),
);
}
}
class MyInheritedWidget extends InheritedWidget {
const MyInheritedWidget({Key? key,required this.child,required this.message}) : super(key: key, child: child);
@override
final Widget child;
final String message;
static MyInheritedWidget of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<MyInheritedWidget>()!;
}
@override
bool updateShouldNotify(MyInheritedWidget oldWidget) {
return true;
}
}
class FirstScreen extends StatelessWidget {
const FirstScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Geeks For Geeks'),
backgroundColor: Colors.green,
),
body: Center(
child: ElevatedButton(
child: Text(MyInheritedWidget.of(context).message),
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (_) => SecondScreen()));
},
),
),
);
}
}
class SecondScreen extends StatelessWidget {
const SecondScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Geeks For Geeks'),
backgroundColor: Colors.green,
),
body: Center(
child: Text(MyInheritedWidget.of(context).message + "\n This is second screen",style: const TextStyle(fontWeight: FontWeight.bold),),
)
);
}
}