Skip to content

Instantly share code, notes, and snippets.

@malviyaritesh
Created September 12, 2019 12:30
Show Gist options
  • Save malviyaritesh/04b94f72c5c2047ea4bd06aaf0a99fc7 to your computer and use it in GitHub Desktop.
Save malviyaritesh/04b94f72c5c2047ea4bd06aaf0a99fc7 to your computer and use it in GitHub Desktop.
Add support for multiline text options in DropdownButtonFormField flutter widget.
import 'package:flutter/material.dart';
/// Use this widget in place of [DropdownButtonFormField] until support
/// for [isExapanded] and [isDense] is added. See:
/// <https://github.com/flutter/flutter/issues/9211>
/// A convenience widget that wraps a [DropdownButton] in a [FormField].
class MultilineDropdownButtonFormField<T> extends FormField<T> {
/// Creates a [DropdownButton] widget wrapped in an [InputDecorator] and
/// [FormField].
///
/// The [DropdownButton] [items] parameters must not be null.
MultilineDropdownButtonFormField({
Key key,
T value,
@required List<DropdownMenuItem<T>> items,
this.onChanged,
InputDecoration decoration = const InputDecoration(),
FormFieldSetter<T> onSaved,
FormFieldValidator<T> validator,
Widget hint,
bool isExpanded = false,
bool isDense = true,
}) : assert(decoration != null),
assert(isExpanded != null),
assert(isDense != null),
super(
key: key,
onSaved: onSaved,
initialValue: value,
validator: validator,
builder: (FormFieldState<T> field) {
final InputDecoration effectiveDecoration = decoration
.applyDefaults(Theme.of(field.context).inputDecorationTheme);
return InputDecorator(
decoration: effectiveDecoration.copyWith(errorText: field.errorText),
isEmpty: value == null,
child: DropdownButtonHideUnderline(
child: DropdownButton<T>(
isExpanded: isExpanded,
isDense: isDense,
value: value,
items: items,
hint: hint,
onChanged: field.didChange,
),
),
);
}
);
/// Called when the user selects an item.
final ValueChanged<T> onChanged;
@override
FormFieldState<T> createState() => _MultilineDropdownButtonFormFieldState<T>();
}
class _MultilineDropdownButtonFormFieldState<T> extends FormFieldState<T> {
@override
MultilineDropdownButtonFormField<T> get widget => super.widget;
@override
void didChange(T value) {
super.didChange(value);
if (widget.onChanged != null)
widget.onChanged(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment