Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active November 22, 2023 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save slightfoot/8ff677dee3e4ee2c7ae41907c2a77f72 to your computer and use it in GitHub Desktop.
Save slightfoot/8ff677dee3e4ee2c7ae41907c2a77f72 to your computer and use it in GitHub Desktop.
Inherited Widget Example - by Simon Lightfoot - Humpday Q&A :: 22nd November 2023 #Flutter #Dart
// MIT License
//
// Copyright (c) 2023 Simon Lightfoot
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const App());
}
class CounterModel {
CounterModel({this.value = 0});
int value;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is CounterModel &&
runtimeType == other.runtimeType &&
value == other.value;
@override
String toString() {
return '${describeIdentity(this)}{$value}';
}
}
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return InheritedCounter(
child: MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
home: const Home(),
),
);
}
}
class InheritedCounter extends StatefulWidget {
const InheritedCounter({
super.key,
required this.child,
});
final Widget child;
static InheritedCounterState of(BuildContext context) {
return context
.dependOnInheritedWidgetOfExactType<_InheritedCounterScope>()!
.state;
}
@override
State<InheritedCounter> createState() => InheritedCounterState();
}
class InheritedCounterState extends State<InheritedCounter> {
var counter = CounterModel();
void add() {
setState(() {
counter.value++;
});
}
@override
Widget build(BuildContext context) {
return _InheritedCounterScope(
state: this,
counter: counter,
child: widget.child,
);
}
}
class _InheritedCounterScope extends InheritedWidget {
_InheritedCounterScope({
required this.state,
required this.counter,
required super.child,
}) : value = counter.value;
final InheritedCounterState state;
final CounterModel counter;
late final int value;
@override
bool updateShouldNotify(_InheritedCounterScope oldWidget) {
print('counter value $value');
print('oldWidget value ${oldWidget.value}');
return value != oldWidget.value;
}
}
class Home extends StatelessWidget {
const Home({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Inherited Counter'),
),
body: const Center(
child: CounterDisplay(),
),
floatingActionButton: const CounterAddButton(),
);
}
}
class CounterAddButton extends StatelessWidget {
const CounterAddButton({super.key});
@override
Widget build(BuildContext context) {
return FloatingActionButton(
onPressed: InheritedCounter.of(context).add,
child: const Icon(Icons.add),
);
}
}
class CounterDisplay extends StatelessWidget {
const CounterDisplay({super.key});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final counter = InheritedCounter.of(context).counter;
print('build display $counter');
return Text(
'Count: ${counter.value}',
style: theme.textTheme.displaySmall,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment