import 'package:flutter/material.dart';
class MyInheritedWidget extends InheritedWidget {
final String data;
MyInheritedWidget({
Key key,
@required this.data,
@required Widget child,
}) : super(key: key, child: child);
static MyInheritedWidget of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<MyInheritedWidget>();
}
@override
bool updateShouldNotify(MyInheritedWidget oldWidget) {
return data != oldWidget.data;
}
}
class ExampleWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final inheritedData = MyInheritedWidget.of(context).data;
return Text(inheritedData);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MyInheritedWidget(
data: 'Hello, Inherited Data!',
child: MaterialApp(
title: 'Inherited Widget Example',
home: Scaffold(
appBar: AppBar(
title: Text('Inherited Widget Example'),
),
body: Center(
child: ExampleWidget(),
),
),
),
);
}
}