Last active
November 22, 2023 18:32
-
-
Save rapPayne/cbde42289a26b2eedf8f89037e867746 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
// ignore_for_file: avoid_print | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const RootWidget()); | |
} | |
class RootWidget extends StatefulWidget { | |
const RootWidget({super.key}); | |
@override | |
State<RootWidget> createState() => _RootWidgetState(); | |
} | |
class _RootWidgetState extends State<RootWidget> { | |
@override | |
Widget build(BuildContext context) { | |
AppState mainState = AppState() | |
..first = "Initial value set on instantiation in main.dart"; | |
return SuperState( | |
state: mainState, | |
child: MaterialApp( | |
title: 'State maintenance demo', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const A(), | |
), | |
); | |
} | |
} | |
class AppState { | |
String first = ""; | |
} | |
class SuperState extends StatefulWidget { | |
final AppState state; | |
final Widget child; | |
const SuperState({super.key, required this.state, required this.child}); | |
@override | |
State<SuperState> createState() => SuperStateState(); | |
// static SuperStateState of(BuildContext context) { | |
// var foo = context.findAncestorStateOfType<SuperStateState>(); | |
// assert(foo != null, "Wait, how can the State be null?"); | |
// return foo!; | |
// } | |
} | |
class SuperStateState extends State<SuperState> { | |
late AppState _state; | |
@override | |
void initState() { | |
_state = widget.state; | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return SuperStateInheritedWidget( | |
state: _state, | |
update: update, | |
child: widget.child, | |
); | |
} | |
void update(AppState newState) { | |
print("SuperState setState: ${widget.state.first} => ${newState.first}"); | |
setState(() { | |
_state = newState; | |
}); | |
} | |
AppState getState() { | |
return widget.state; | |
} | |
} | |
class SuperStateInheritedWidget extends InheritedWidget { | |
final AppState state; | |
final void Function(AppState) update; | |
const SuperStateInheritedWidget( | |
{super.key, | |
required this.state, | |
required this.update, | |
required super.child}); | |
static SuperStateInheritedWidget of(BuildContext context) { | |
return context.getInheritedWidgetOfExactType<SuperStateInheritedWidget>()!; | |
} | |
@override | |
bool updateShouldNotify(covariant SuperStateInheritedWidget oldWidget) { | |
return true; | |
} | |
} | |
class A extends StatefulWidget { | |
const A({super.key}); | |
@override | |
State<A> createState() => _AState(); | |
} | |
class _AState extends State<A> { | |
AppState? state; | |
String first = ""; | |
final TextEditingController _controller = TextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
var sss = SuperStateInheritedWidget.of(context); | |
state = sss.state; | |
first = state!.first; | |
_controller.text = first; | |
print("A builder is running. first is '${state!.first}'"); | |
return Scaffold( | |
body: Center( | |
child: Column( | |
children: [ | |
Text("A $first"), | |
TextField( | |
onChanged: (val) => first = val, | |
decoration: const InputDecoration( | |
labelText: "First name", | |
), | |
controller: _controller, | |
), | |
const A1(), | |
], | |
), | |
), | |
); | |
} | |
} | |
class A1 extends StatefulWidget { | |
const A1({super.key}); | |
@override | |
State<A1> createState() => _A1State(); | |
} | |
class _A1State extends State<A1> { | |
AppState? state; | |
String first = ""; | |
final TextEditingController _controller = TextEditingController(); | |
@override | |
void didChangeDependencies() { | |
print("A1 dcd is running"); | |
super.didChangeDependencies(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
state = SuperStateInheritedWidget.of(context).state; | |
print("A1 builder is running. First is ${state!.first}"); | |
first = state!.first; | |
_controller.text = first; | |
return Center( | |
child: Column( | |
children: [ | |
Text("A1 $first"), | |
TextField( | |
onChanged: (val) => first = val, | |
decoration: const InputDecoration( | |
labelText: "First name", | |
), | |
controller: _controller, | |
), | |
TextButton( | |
onPressed: () => SuperStateInheritedWidget.of(context) | |
.update(AppState()..first = "Set in A1 btn click"), | |
child: const Text("Update"), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment