added controller scope and extension for context
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import '../../service/controller_scope.dart';
|
||||||
|
|
||||||
|
extension ControllerContext on BuildContext {
|
||||||
|
T controller<T extends ChangeNotifier>() {
|
||||||
|
return ControllerScope.of<T>(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import 'package:flutter/widgets.dart';
|
||||||
|
|
||||||
|
class ControllerScope<T extends ChangeNotifier> extends InheritedNotifier<T> {
|
||||||
|
const ControllerScope({
|
||||||
|
super.key,
|
||||||
|
required T controller,
|
||||||
|
required super.child,
|
||||||
|
}) : super(notifier: controller);
|
||||||
|
|
||||||
|
static T of<T extends ChangeNotifier>(BuildContext context) {
|
||||||
|
final scope = context
|
||||||
|
.dependOnInheritedWidgetOfExactType<ControllerScope<T>>();
|
||||||
|
|
||||||
|
assert(scope != null, 'No ControllerScope<$T> found in context');
|
||||||
|
|
||||||
|
return scope!.notifier!;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user