Created
June 9, 2021 09:46
-
-
Save abhaysood/1a93ebc2ff2b601342a5d28d5ea86c30 to your computer and use it in GitHub Desktop.
inherited_widget_issue
This file contains 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
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const DemoApp()); | |
} | |
class DemoApp extends StatefulWidget { | |
const DemoApp({Key? key}) : super(key: key); | |
@override | |
_DemoAppState createState() => _DemoAppState(); | |
} | |
class _DemoAppState extends State<DemoApp> { | |
late ThemeMode themeMode; | |
@override | |
void initState() { | |
themeMode = ThemeMode.light; | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
/// Not working with itemBuilder context. | |
return FrogColor( | |
child: MaterialApp( | |
home: SafeArea( | |
child: Scaffold( | |
body: ListView.builder( | |
itemCount: 1, | |
itemBuilder: (context, index) { | |
final frogColor = FrogColor.of(context); | |
return RadioListTile( | |
activeColor: frogColor, | |
value: index, | |
groupValue: 0, | |
onChanged: (value) {}, | |
); | |
}, | |
), | |
), | |
), | |
), | |
); | |
/// Working with outer context | |
// return FrogColor( | |
// child: Builder( | |
// builder: (context) => MaterialApp( | |
// home: SafeArea( | |
// child: Scaffold( | |
// body: ListView.builder( | |
// itemCount: 1, | |
// itemBuilder: (_, index) => RadioListTile( | |
// activeColor: FrogColor.of(context), | |
// value: index, | |
// groupValue: 0, | |
// onChanged: (value) {}, | |
// ), | |
// ), | |
// ), | |
// ), | |
// ), | |
// ), | |
// ); | |
} | |
} | |
class FrogColor extends StatefulWidget { | |
final Widget child; | |
const FrogColor({Key? key, required this.child}) : super(key: key); | |
static Color of(BuildContext context) { | |
final _FrogInherited? result = | |
context.dependOnInheritedWidgetOfExactType<_FrogInherited>(); | |
assert(result != null, 'No FrogColor found in context'); | |
return result!.color; | |
} | |
@override | |
_FrogColorState createState() => _FrogColorState(); | |
} | |
class _FrogColorState extends State<FrogColor> with WidgetsBindingObserver { | |
@override | |
void initState() { | |
super.initState(); | |
WidgetsBinding.instance!.addObserver(this); | |
} | |
@override | |
void didChangePlatformBrightness() { | |
super.didChangePlatformBrightness(); | |
setState(() {}); | |
} | |
@override | |
void dispose() { | |
WidgetsBinding.instance!.removeObserver(this); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
final resolveColor = | |
WidgetsBinding.instance!.window.platformBrightness == Brightness.light | |
? Colors.blueAccent | |
: Colors.blueGrey; | |
return _FrogInherited( | |
child: widget.child, | |
state: this, | |
color: resolveColor, | |
); | |
} | |
} | |
class _FrogInherited extends InheritedWidget { | |
const _FrogInherited({ | |
Key? key, | |
required this.color, | |
required Widget child, | |
required _FrogColorState state, | |
}) : super(key: key, child: child); | |
final Color color; | |
@override | |
bool updateShouldNotify(_FrogInherited old) => color != old.color; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment