adding packages
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'feature.dart';
|
||||
import 'provider.dart';
|
||||
|
||||
class FeatureBuilder extends StatelessWidget {
|
||||
final String? featureKey;
|
||||
final Feature? feature;
|
||||
final Widget? child;
|
||||
final Widget Function(BuildContext, bool, Widget?) builder;
|
||||
final bool hideOnInactive;
|
||||
|
||||
const FeatureBuilder({
|
||||
Key? key,
|
||||
required this.builder,
|
||||
this.featureKey,
|
||||
this.child,
|
||||
this.feature,
|
||||
this.hideOnInactive = false,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final _features = Features.of(context);
|
||||
final _feature = feature ??
|
||||
_features?.items.firstWhere((element) => element.key == featureKey);
|
||||
if (_feature == null) {
|
||||
return Visibility(
|
||||
visible: hideOnInactive ? false : true,
|
||||
child: builder(context, false, child),
|
||||
);
|
||||
}
|
||||
return ValueListenableBuilder<bool>(
|
||||
valueListenable: _feature.isActive,
|
||||
builder: (context, active, child) {
|
||||
return Visibility(
|
||||
visible: hideOnInactive ? active : true,
|
||||
child: builder(context, active, child),
|
||||
);
|
||||
},
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class Feature {
|
||||
@override
|
||||
final String key;
|
||||
|
||||
final ValueNotifier<bool> isActive = ValueNotifier(false);
|
||||
|
||||
Feature(this.key);
|
||||
|
||||
bool get enabled => isActive.value;
|
||||
set enabled(bool value) {
|
||||
if (value != enabled) {
|
||||
isActive.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
void defaultValue(bool value) {
|
||||
enabled = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'feature.dart';
|
||||
|
||||
class Features extends InheritedWidget {
|
||||
const Features({
|
||||
Key? key,
|
||||
required this.items,
|
||||
required Widget child,
|
||||
}) : super(key: key, child: child);
|
||||
|
||||
final List<Feature> items;
|
||||
|
||||
static Features? of(BuildContext context) {
|
||||
return context.dependOnInheritedWidgetOfExactType<Features>();
|
||||
}
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(Features old) => items != old.items;
|
||||
}
|
||||
Reference in New Issue
Block a user