Skip to content

Instantly share code, notes, and snippets.

@IsmailAlamKhan
Created September 5, 2021 14:03
Show Gist options
  • Save IsmailAlamKhan/cbacaf9709a29828c508ef92cf16a602 to your computer and use it in GitHub Desktop.
Save IsmailAlamKhan/cbacaf9709a29828c508ef92cf16a602 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(
CounterScope(
notifier: CounterNotifier(),
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Home(),
);
}
}
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
print("HOME BUILD");
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => Page1()),
);
},
child: Text('GO TO NEXT BUTTON'),
),
),
);
}
}
class Page1 extends StatelessWidget {
const Page1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final counter = CounterNotifier.of(context);
return Scaffold(
body: Center(
child: Text(
'You have pressed the button this many times: ${counter.value}')),
floatingActionButton: FloatingActionButton(
onPressed: () => counter.value++,
child: Icon(Icons.add),
),
);
}
}
class CounterNotifier extends ValueNotifier<int> {
CounterNotifier() : super(0);
static CounterNotifier of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<CounterScope>()!.notifier!;
}
class CounterScope extends InheritedNotifier<CounterNotifier> {
CounterScope({
Key? key,
required Widget child,
required CounterNotifier notifier,
}) : super(key: key, child: child, notifier: notifier);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment