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

[FormBuilderTypeAhead]: How to Select a value to be preselected #86

Open
7 tasks done
abhilashsajeev opened this issue Sep 29, 2023 · 6 comments
Open
7 tasks done
Labels
bug Something isn't working

Comments

@abhilashsajeev
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Package/Plugin version

Current

Platforms

  • Android
  • iOS
  • Linux
  • MacOS
  • Web
  • Windows

Flutter doctor

Flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.10.6, on macOS 13.0 22A380 darwin-arm64, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] VS Code (version 1.82.2)
[✓] Connected device (2 available)
[✓] Network resources

• No issues found!

Minimal code example

Code sample
final initialValue = selected != null
                ? data.firstWhere(
                    (element) => element.iaCaseType == selected,
                  )
                : null;
            print('IA type initial value ${initialValue?.iaTypeName}');
            return FormBuilderTypeAhead<IaTypes>(
              initialValue: initialValue,
              decoration: const InputDecoration(
                border: OutlineInputBorder(),
                labelText: 'IA Type',
                hintText: 'IA Type',
              ),
              name: 'ia_type',
              itemBuilder: (context, item) {
                return ListTile(title: Text(item.iaTypeName!));
              },
              selectionToTextTransformer: (suggestion) =>
                  suggestion.iaTypeName!,
              controller: TextEditingController(text: ''),
              validator: FormBuilderValidators.compose(
                  [FormBuilderValidators.required()]),
              suggestionsCallback: (query) {
                final pattern = RegExp('[^a-zA-Z0-9\s]');
                if (query.isNotEmpty) {
                  var lowercaseQuery = query.toLowerCase();
                  return data.where((IaTypes item) {
                    return item.iaTypeName!
                        .toLowerCase()
                        .replaceAll(pattern, '')
                        .contains(lowercaseQuery);
                  }).toList(growable: false)
                    ..sort((a, b) => a.iaTypeName!
                        .toLowerCase()
                        .indexOf(lowercaseQuery)
                        .compareTo(b.iaTypeName!
                            .toLowerCase()
                            .indexOf(lowercaseQuery)));
                } else {
                  return data;
                }
              },
            );

Current Behavior

No preselected value is showing. Even though I have selected initialValue and passed it to intialvalue field in the FormBuilderTypeAhead

Expected Behavior

when initialvalue to be passed to filed it should show it as preseleted item

Steps To Reproduce

Using Obx of Getx

Aditional information

No response

@abhilashsajeev abhilashsajeev added the bug Something isn't working label Sep 29, 2023
@deandreamatias
Copy link
Contributor

Hi!,
Please update the issue info with a minimal code example (without dependencies) and add a steps to reproduce the issue. Thanks

@abhilashsajeev
Copy link
Author

@deandreamatias Code example already given above. It is just the component rendered in FutureBuilder when snapshot.data is valid one

            final initialValue = selected != null
                ? data.firstWhere(
                    (element) => element.iaCaseType == selected,
                  )
                : null;
            print('IA type initial value ${initialValue?.iaTypeName}');
            return FormBuilderTypeAhead<IaTypes>(
              initialValue: initialValue,
              decoration: const InputDecoration(
                border: OutlineInputBorder(),
                labelText: 'IA Type',
                hintText: 'IA Type',
              ),
              name: 'ia_type',
              itemBuilder: (context, item) {
                return ListTile(title: Text(item.iaTypeName!));
              },
              selectionToTextTransformer: (suggestion) =>
                  suggestion.iaTypeName!,
              controller: TextEditingController(text: ''),
              validator: FormBuilderValidators.compose(
                  [FormBuilderValidators.required()]),
              suggestionsCallback: (query) {
                final pattern = RegExp('[^a-zA-Z0-9\s]');
                if (query.isNotEmpty) {
                  var lowercaseQuery = query.toLowerCase();
                  return data.where((IaTypes item) {
                    return item.iaTypeName!
                        .toLowerCase()
                        .replaceAll(pattern, '')
                        .contains(lowercaseQuery);
                  }).toList(growable: false)
                    ..sort((a, b) => a.iaTypeName!
                        .toLowerCase()
                        .indexOf(lowercaseQuery)
                        .compareTo(b.iaTypeName!
                            .toLowerCase()
                            .indexOf(lowercaseQuery)));
                } else {
                  return data;
                }
              },
            );

the value selected will be set in the state as a number

@deandreamatias
Copy link
Contributor

@abhilashsajeev nice, But I need the definition of class IaTypes. You can modify the example/main.dart file and replace to use like minimal example code.
The idea is that I can copy the code and run directly, without add any other things.
Thanks

@abhilashsajeev
Copy link
Author

@deandreamatias IaTypes looks like this

class IaTypes {
  int? iaCaseType;
  String? iaTypeName;

  IaTypes({this.iaCaseType, this.iaTypeName});

  IaTypes.fromJson(Map<String, dynamic> json) {
    iaCaseType = json['ia_case_type'];
    iaTypeName = json['ia_type_name'];
  }

  Map<String, dynamic> toJson() {
    final data = <String, dynamic>{};
    data['ia_case_type'] = iaCaseType;
    data['ia_type_name'] = iaTypeName;
    return data;
  }
}

@deandreamatias
Copy link
Contributor

Maybe you need add equal comparison to this class @abhilashsajeev
I think thats better you modify the example code and try to reproduce the issue with that

@abhilashsajeev
Copy link
Author

@deandreamatias Most of people will work with some kind of class while using this. Can you include such example in the documentation using Array of Objects in addition to Current one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants