Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cat-voices): Project duration months picker #1718

Merged
merged 7 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import 'package:catalyst_voices/common/ext/document_property_schema_ext.dart';
import 'package:catalyst_voices/widgets/dropdown/voices_dropdown.dart';
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:catalyst_voices_view_models/catalyst_voices_view_models.dart';
import 'package:flutter/material.dart';

class DurationInMonthsWidget extends StatefulWidget {
final DocumentValueProperty<int> property;
final DocumentDurationInMonthsSchema schema;
final bool isEditMode;
final ValueChanged<List<DocumentChange>> onChanged;

const DurationInMonthsWidget({
super.key,
required this.property,
required this.schema,
required this.isEditMode,
required this.onChanged,
});

@override
State<DurationInMonthsWidget> createState() => _DurationInMonthsWidgetState();
}

class _DurationInMonthsWidgetState extends State<DurationInMonthsWidget> {
late List<DropdownMenuEntry<int>> _dropdownMenuEntries;
late int? _selectedValue;

String get _title => widget.schema.formattedTitle;

int get _min => widget.schema.numRange?.min ?? 0;

int get _max => widget.schema.numRange?.max ?? 0;

List<DropdownMenuEntry<int>> get _mapItems {
final items = <DropdownMenuEntry<int>>[];
for (var i = _min; i <= _max; i++) {
items.add(DropdownMenuEntry(value: i, label: '$i'));
}

return items;
}

@override
void initState() {
super.initState();
_handleInitialValue();
}

@override
void didUpdateWidget(DurationInMonthsWidget oldWidget) {
super.didUpdateWidget(oldWidget);

if (oldWidget.isEditMode != widget.isEditMode &&
widget.isEditMode == false) {
_handleInitialValue();
}

if (oldWidget.property.value != widget.property.value) {
_handleInitialValue();
}
}

@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
if (_title.isNotEmpty) ...[
Text(
_title,
style: Theme.of(context).textTheme.titleSmall,
),
const SizedBox(height: 8),
SingleSelectDropdown(
items: _dropdownMenuEntries,
onChanged: _handleValueChanged,
validator: _validator,
initialValue: _selectedValue,
enabled: widget.isEditMode,
),
],
],
);
}

void _handleInitialValue() {
_selectedValue = widget.property.value;
_dropdownMenuEntries = _mapItems;
}

void _handleValueChanged(int? value) {
setState(() {
_selectedValue = value;
});

if (widget.property.value != value) {
_notifyChangeListener(value);
}
}

void _notifyChangeListener(int? value) {
widget.onChanged(
[
DocumentValueChange(
nodeId: widget.schema.nodeId,
value: value,
),
],
);
}

String? _validator(int? value) {
final result = widget.schema.validate(value);

return LocalizedDocumentValidationResult.from(result).message(context);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import 'package:catalyst_voices/common/ext/text_editing_controller_ext.dart';
import 'package:catalyst_voices/common/ext/document_property_schema_ext.dart';
import 'package:catalyst_voices/widgets/dropdown/voices_dropdown.dart';
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:catalyst_voices_view_models/catalyst_voices_view_models.dart';
import 'package:flutter/material.dart';

class SingleDropdownSelectionWidget extends StatefulWidget {
final String value;
final List<String> items;
final DocumentValueProperty<String> property;
final DocumentDropDownSingleSelectSchema schema;
final bool isEditMode;
final ValueChanged<List<DocumentChange>> onChanged;

const SingleDropdownSelectionWidget({
super.key,
required this.value,
required this.items,
required this.property,
required this.schema,
required this.isEditMode,
required this.onChanged,
Expand All @@ -27,41 +26,33 @@ class SingleDropdownSelectionWidget extends StatefulWidget {
class _SingleDropdownSelectionWidgetState
extends State<SingleDropdownSelectionWidget> {
late List<DropdownMenuEntry<String>> _dropdownMenuEntries;
late TextEditingController _textEditingController;
late String? _selectedValue;

String? value;
String get _title => widget.schema.formattedTitle;

List<DropdownMenuEntry<String>> get _mapItems {
final items = widget.items;
final items = widget.schema.enumValues ?? [];
return items.map((e) => DropdownMenuEntry(value: e, label: e)).toList();
}

@override
void initState() {
super.initState();
final textValue = TextEditingValueExt.collapsedAtEndOf(widget.value);
_textEditingController = TextEditingController.fromValue(textValue);
_dropdownMenuEntries = _mapItems;
_handleInitialValue();
}

@override
void didUpdateWidget(covariant SingleDropdownSelectionWidget oldWidget) {
super.didUpdateWidget(oldWidget);

if (oldWidget.items != widget.items) {
_dropdownMenuEntries = _mapItems;
}
if (oldWidget.isEditMode != widget.isEditMode &&
widget.isEditMode == false) {
final value = widget.value;
_textEditingController.textWithSelection = value;
_handleInitialValue();
}
}

@override
void dispose() {
_textEditingController.dispose();
super.dispose();
if (oldWidget.property.value != widget.property.value) {
_handleInitialValue();
}
}

@override
Expand All @@ -70,25 +61,48 @@ class _SingleDropdownSelectionWidgetState
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
widget.schema.title,
_title,
style: Theme.of(context).textTheme.titleSmall,
),
const SizedBox(height: 8),
SingleSelectDropdown(
textEditingController: _textEditingController,
items: _dropdownMenuEntries,
initialValue: _selectedValue,
onChanged: _handleValueChanged,
validator: _validator,
enabled: widget.isEditMode,
onSelected: (val) {
final change = DocumentValueChange(
nodeId: widget.schema.nodeId,
value: val,
);
widget.onChanged([change]);
},
initialValue: widget.value,
hintText: widget.schema.placeholder,
),
],
);
}

void _handleInitialValue() {
_selectedValue = widget.property.value;
_dropdownMenuEntries = _mapItems;
}

void _handleValueChanged(String? value) {
setState(() {
_selectedValue = value;
});

if (widget.property.value != value) {
_notifyChangeListener(value);
}
}

void _notifyChangeListener(String? value) {
widget.onChanged([
DocumentValueChange(
nodeId: widget.schema.nodeId,
value: value,
),
]);
}

String? _validator(String? value) {
final result = widget.schema.validate(value);

return LocalizedDocumentValidationResult.from(result).message(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ class _TagGroupsDropdown extends StatelessWidget {
.map((e) => DropdownMenuEntry(value: e, label: e.group))
.toList(),
initialValue: value,
onSelected: onChanged,
hintText: hintText,
onChanged: onChanged,
);
}
}
Expand Down
Loading
Loading