Skip to content

Instantly share code, notes, and snippets.

@rubywai
Created February 22, 2023 07:41
Show Gist options
  • Save rubywai/e4d229f226b46b2674dc9f40daee84d1 to your computer and use it in GitHub Desktop.
Save rubywai/e4d229f226b46b2674dc9f40daee84d1 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:inherited_widget_tuto/counter.dart';
import 'package:inherited_widget_tuto/home.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int count = 0;
@override
void didChangeDependencies() {
super.didChangeDependencies();
debugPrint('did change dependency is called');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(count.toString()),
),
body: CountState(
count: 1000,
increment: _increment,
child: const Home(),
)),
);
}
void _increment() {
setState(() {
count++;
});
}
}
class CountState extends InheritedWidget {
final int count;
final Function increment;
const CountState({super.key,required this.count, required super.child,required this.increment});
static CountState of(BuildContext context) {
return (context.dependOnInheritedWidgetOfExactType<CountState>())!;
}
@override
bool updateShouldNotify(CountState oldWidget) {
//return true;
return false;
}
}
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
debugPrint('did change dependency is called');
}
@override
Widget build(BuildContext context) {
final CountState countState = CountState.of(context);
return Center(child: InkWell(
onTap: (){
countState.increment();
},
child: Text('Hello ${countState.count}')),);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment