-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp3_my_cart_inherited_widget.dart
More file actions
29 lines (25 loc) · 1007 Bytes
/
Copy pathp3_my_cart_inherited_widget.dart
File metadata and controls
29 lines (25 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import 'package:app/common/data/my_cart_change_notifier.dart';
import 'package:flutter/widgets.dart';
class P3MyCartInheritedWidget extends InheritedWidget {
const P3MyCartInheritedWidget({
super.key,
required super.child,
required this.myCart,
});
// ⭐️ Expose ChangeNotifier through InheritedWidget.
final MyCartChangeNotifier myCart;
// ⭐️ Since ChangeNotifier has change notification function,
// update notification by InheritedWidget is not required.
@override
bool updateShouldNotify(P3MyCartInheritedWidget oldWidget) => false;
// ⭐️ Static method `of` to get the InheritedWidget that is an ancestor of the Widget tree.
static P3MyCartInheritedWidget of(BuildContext context) {
final widget = context
.getElementForInheritedWidgetOfExactType<P3MyCartInheritedWidget>()
?.widget as P3MyCartInheritedWidget?;
if (widget != null) {
return widget;
}
throw Exception('No P3InheritedMyCart found in context');
}
}