Created
April 9, 2020 01:22
-
-
Save stegrams/a2d17dc45bdae1ddfcc92cf6af96b80b to your computer and use it in GitHub Desktop.
Question: How do i get the happy face by passing any type parameter to ```Foo``` except ```<dynamic>```
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
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Foo<void>(child: Bar(), txt: "Happy 😁"), | |
); | |
} | |
} | |
class Foo<T> extends InheritedWidget { | |
Foo({Key key, this.txt, this.child}) : super(key: key, child: child); | |
final String txt; | |
final Widget child; | |
static Foo of(BuildContext context) => | |
context.dependOnInheritedWidgetOfExactType<Foo>(); | |
@override | |
bool updateShouldNotify(Foo old) => true; | |
} | |
class Bar extends StatelessWidget { | |
@override | |
Widget build(BuildContext ctx) => Text(Foo.of(ctx)?.txt ?? "Sad 🤕"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[SOLVED]
Solution Code: kranfix/typed_inherited.dart