Skip to content

Instantly share code, notes, and snippets.

@kekland
Created April 8, 2023 11:56
Show Gist options
  • Save kekland/a6c8332532452060e6d693dd2450e76d to your computer and use it in GitHub Desktop.
Save kekland/a6c8332532452060e6d693dd2450e76d to your computer and use it in GitHub Desktop.
KnobsNotifier that supports adding custom knobs
// ignore_for_file: invalid_use_of_internal_member
import 'package:widgetbook/widgetbook.dart';
import 'package:widgetbook/src/repositories/selected_use_case_repository.dart';
import 'package:widgetbook/src/knobs/options_knob.dart';
class CustomKnobsNotifier extends KnobsNotifier {
CustomKnobsNotifier(this.selectedStoryRepository)
: super(selectedStoryRepository);
final SelectedUseCaseRepository selectedStoryRepository;
final Map<WidgetbookUseCaseData, Map<String, Knob<dynamic>>> customKnobs =
<WidgetbookUseCaseData, Map<String, Knob<dynamic>>>{};
@override
List<Knob<dynamic>> all() {
if (!selectedStoryRepository.isSet()) {
return [];
}
final story = selectedStoryRepository.item;
final knobs = customKnobs[story]?.values.toList() ?? [];
return [...knobs, ...super.all()];
}
@override
void update<T>(String label, T value) {
if (!selectedStoryRepository.isSet()) {
return;
}
if (customKnobs[selectedStoryRepository.item] != null &&
customKnobs[selectedStoryRepository.item]![label] != null) {
customKnobs[selectedStoryRepository.item]![label]!.value = value;
notifyListeners();
return;
}
super.update(label, value);
}
T addCustomKnob<T>(Knob<T> value) {
final story = selectedStoryRepository.item!;
final knobs = customKnobs.putIfAbsent(
story,
() => <String, Knob<dynamic>>{},
);
return (knobs.putIfAbsent(value.label, () {
Future.microtask(notifyListeners);
return value;
}) as Knob<T>)
.value;
}
T optionsWithDefault<T>({
required String label,
String? description,
required List<T> options,
T? initialValue,
String Function(T)? labelBuilder,
}) {
assert(options.isNotEmpty, 'Must specify at least one option');
return addCustomKnob(
OptionsKnob(
label: label,
value: initialValue ?? options.first,
description: description,
options: options,
labelBuilder: labelBuilder,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment