Как я могу сделать выпадающего многоразового использования в флаттер - PullRequest
0 голосов
/ 25 сентября 2019

Я хочу сделать этот выпадающий список многоразовым.когда я хочу использовать.мне нужно просто вызвать этот выпадающий список и передать значение.Надеюсь, вы понимаете вопрос:)

Вот код, который я пробовал.

FormBuilder(
      key: _fbKey,
      autovalidate: true,
      initialValue: {
        'country': 5,
      },
      child: FormBuilderCustomField(
        attribute: "name",
        validators: [
          FormBuilderValidators.required(),
        ],
        formField: FormField(
          // key: _fieldKey,
          enabled: true,
          builder: (FormFieldState<dynamic> field) {
            return InputDecorator(
              decoration: InputDecoration(
                labelText: "Select Country",
                contentPadding: EdgeInsets.only(top: 10.0, bottom: 0.0),
                border: InputBorder.none,
                errorText: field.errorText,
              ),
              child: DropdownButton(
                isExpanded: true,
                items: ["One", "Two"].map((option) {
                  return DropdownMenuItem(
                    child: Text("$option"),
                    value: option,
                  );
                }).toList(),
                value: field.value,
                onChanged: (value) {
                  field.didChange(value);
                },
              ),
            );
          },
        ),
      ),
    );

1 Ответ

1 голос
/ 25 сентября 2019

Создайте пользовательский класс, как показано ниже

import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';

class DropDown extends StatelessWidget {
  final GlobalKey fbKey;
  final String attribute, labelText;
  final List<String> itemsList;

  DropDown({
    Key key,
    @required this.fbKey,
    this.attribute,
    this.labelText,
    this.itemsList,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return FormBuilder(
      key: fbKey,
      autovalidate: true,
      initialValue: {
        'country': 5,
      },
      child: FormBuilderCustomField(
        attribute: attribute,
        validators: [
          FormBuilderValidators.required(),
        ],
        formField: FormField(
          // key: _fieldKey,
          enabled: true,
          builder: (FormFieldState<dynamic> field) {
            return InputDecorator(
              decoration: InputDecoration(
                labelText: labelText,
                contentPadding: EdgeInsets.only(top: 10.0, bottom: 0.0),
                border: InputBorder.none,
                errorText: field.errorText,
              ),
              child: DropdownButton(
                isExpanded: true,
                items: itemsList.map((option) {
                  return DropdownMenuItem(
                    child: Text("$option"),
                    value: option,
                  );
                }).toList(),
                value: field.value,
                onChanged: (value) {
                  field.didChange(value);
                },
              ),
            );
          },
        ),
      ),
    );
  }
}

Используйте его, как показано ниже

DropDown(
          fbKey: _bfKey,
          attribute: 'Name',
          labelText: 'Select Country',
          itemsList: ['One', 'Two'],
        ),
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...