Skip to content

Commit

Permalink
feat: changing type of yes no choice form field
Browse files Browse the repository at this point in the history
  • Loading branch information
LynxLynxx committed Jan 13, 2025
1 parent 96f222c commit afd93a7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
import 'package:catalyst_voices/common/ext/document_property_ext.dart';
import 'package:catalyst_voices/widgets/widgets.dart';
import 'package:catalyst_voices_localization/catalyst_voices_localization.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';

enum _YesNoChoice {
yes(true),
no(false);

// ignore: avoid_positional_boolean_parameters
const _YesNoChoice(this.value);

// ignore: avoid_positional_boolean_parameters
static _YesNoChoice fromBool(bool value) {
return _YesNoChoice.values.firstWhere((e) => e.value == value);
}

final bool value;

String localizedName(VoicesLocalizations localizations) {
return switch (this) {
yes => localizations.yes,
no => localizations.no,
};
}
}

class YesNoChoiceWidget extends StatefulWidget {
final DocumentProperty<bool> property;
final ValueChanged<DocumentChange> onChanged;
Expand All @@ -47,6 +26,8 @@ class YesNoChoiceWidget extends StatefulWidget {
class _YesNoChoiceWidgetState extends State<YesNoChoiceWidget> {
late bool? selectedValue;

String get _description => widget.property.formattedDescription;

@override
void initState() {
super.initState();
Expand All @@ -73,16 +54,16 @@ class _YesNoChoiceWidgetState extends State<YesNoChoiceWidget> {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (widget.description.isNotEmpty) ...[
if (_description.isNotEmpty) ...[
Text(
widget.description,
_description,
style: Theme.of(context).textTheme.titleSmall,
),
const SizedBox(height: 8),
],
_YesNoChoiceSegmentButton(
context,
value: widget.property.value,
value: selectedValue,
enabled: widget.isEditMode,
onChanged: _handleValueChanged,
validator: (value) {
Expand All @@ -103,7 +84,7 @@ class _YesNoChoiceWidgetState extends State<YesNoChoiceWidget> {

void _handleValueChanged(bool? value) {
setState(() {
selectedValue = widget.property.value;
selectedValue = value;
});
if (value == null && widget.property.value != value) {
_notifyChangeListener(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'package:catalyst_voices/widgets/document_builder/agreement_confirmation_
import 'package:catalyst_voices/widgets/document_builder/document_token_value_widget.dart';
import 'package:catalyst_voices/widgets/document_builder/single_dropdown_selection_widget.dart';
import 'package:catalyst_voices/widgets/document_builder/single_grouped_tag_selector_widget.dart';
import 'package:catalyst_voices/widgets/document_builder/yes_no_choice_widget.dart';
import 'package:catalyst_voices/widgets/document_builder/single_line_https_url_widget.dart.dart';
import 'package:catalyst_voices/widgets/document_builder/yes_no_choice_widget.dart';
import 'package:catalyst_voices/widgets/widgets.dart';
import 'package:catalyst_voices_localization/catalyst_voices_localization.dart';
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
Expand Down Expand Up @@ -278,7 +278,6 @@ class _PropertyBuilder extends StatelessWidget {
return YesNoChoiceWidget(
property: castProperty,
onChanged: onChanged,
description: castProperty.schema.description ?? '',
isEditMode: isEditMode,
isRequired: castProperty.schema.isRequired,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class YesNoChoiceDefinition extends BaseDocumentDefinition<bool> {
DocumentSchemaProperty<bool> schema,
bool? value,
) {
// TODO(dtscalac): validate yes no choice
return DocumentValidator.validateBool(schema, value);
}

Expand Down

0 comments on commit afd93a7

Please sign in to comment.