Skip to content

Instantly share code, notes, and snippets.

@umuieme
Last active October 30, 2021 19:41
Show Gist options
  • Select an option

  • Save umuieme/60383edc97fa0dc968e142c285e7243d to your computer and use it in GitHub Desktop.

Select an option

Save umuieme/60383edc97fa0dc968e142c285e7243d to your computer and use it in GitHub Desktop.
import 'package:custom_form_field/src/choose_option_screen.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class CustomDropDownFormField<T> extends FormField<T> {
CustomDropDownFormField({
Key? key,
required List<DropdownMenuItem<T>> items,
FormFieldValidator<T>? validator,
Widget? hintText,
}) : super(
key: key,
validator: validator,
builder: (state) {
var selectedItem =
items.where((element) => element.value == state.value).toList();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
InkWell(
onTap: () async {
var result = await Navigator.push(
state.context,
CupertinoPageRoute(
builder: (_) => ChooseOptionScreen(
items: items, selectedValue: state.value)));
if (result != null) state.didChange(result);
},
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black26)),
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Row(
children: [
Expanded(
child: selectedItem.isNotEmpty
? selectedItem[0]
: Padding(
padding: const EdgeInsets.all(8.0),
child: hintText ??
const Text("Choose an option"),
)),
const Icon(Icons.arrow_right)
],
),
),
),
if (state.hasError)
Text(
state.errorText ?? "Invalid field",
style: const TextStyle(color: Colors.red),
)
],
);
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment