Skip to content

Instantly share code, notes, and snippets.

@stegrams
Created April 9, 2020 01:22
Show Gist options
  • Save stegrams/a2d17dc45bdae1ddfcc92cf6af96b80b to your computer and use it in GitHub Desktop.
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>```
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 🤕");
}
@stegrams
Copy link
Author

stegrams commented Apr 9, 2020

[SOLVED]
Solution Code: kranfix/typed_inherited.dart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment