Let's learn how to add InheritedWidget to our project:
- Create a new file called plan_provider.dart for storing our plans. Place this file in the root of the project's lib directory. This widget extends InheritedWidget:
import 'package:flutter/material.dart';
import './models/data_layer.dart';
class PlanProvider extends InheritedWidget {
final _plan = Plan();
PlanProvider({Key key, Widget child}) : super(key: key, child:
child);
@override
bool updateShouldNotify(InheritedWidget oldWidget) => false;
}
- To make the data accessible from anywhere in the app, we need to create our first of-context method. Add a static Plan of method that takes a BuildContext just after updateShouldNotify:
static Plan of(BuildContext context) {
final provider = context.dependOnInheritedWidgetOfExactType<PlanProvider>();
return provider._plan;
}
- Now that the provider widget is ready, it needs to be placed in the tree. In the build method...