reactive_dropdown_button2 0.0.4-beta.10 reactive_dropdown_button2: ^0.0.4-beta.10 copied to clipboard
Wrapper around dropdown_button2 to use with reactive_forms.
import 'package:flutter/material.dart';
import 'package:reactive_dropdown_button2/reactive_dropdown_button2.dart';
import 'package:reactive_forms/reactive_forms.dart';
void main() {
runApp(const MyApp());
}
final List<String> items = [
'Item1',
'Item2',
'Item3',
'Item4',
'Item5',
'Item6',
'Item7',
'Item8',
];
const icon = Icon(
Icons.access_alarms,
size: 16,
);
const hint = Text('Hint text');
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
FormGroup buildForm() => fb.group({
'input': FormControl<String>(
value: null,
validators: [
const RequiredValidator(),
],
),
'input2': FormControl<String>(
value: null,
validators: [
const RequiredValidator(),
],
),
});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Scaffold(
appBar: AppBar(),
body: SafeArea(
child: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
padding: const EdgeInsets.symmetric(
horizontal: 20.0,
vertical: 20.0,
),
child: ReactiveFormBuilder(
form: buildForm,
builder: (context, form, child) {
return Column(
children: [
const SizedBox(height: 16),
DropdownButtonHideUnderline(
child: ReactiveDropdownButton2<String, String>(
formControlName: 'input',
isExpanded: true,
items: items
.map(
(String item) => DropdownItem<String>(
value: item,
child: Text(item),
),
)
.toList(),
buttonStyleData: ButtonStyleData(
height: 50,
width: 160,
padding: const EdgeInsets.only(left: 14, right: 14),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(14),
border: Border.all(
color: Colors.black26,
),
color: Colors.redAccent,
),
elevation: 2,
),
iconStyleData: const IconStyleData(
icon: Icon(
Icons.arrow_forward_ios_outlined,
),
iconSize: 14,
iconEnabledColor: Colors.yellow,
iconDisabledColor: Colors.grey,
),
dropdownStyleData: DropdownStyleData(
maxHeight: 200,
width: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(14),
color: Colors.redAccent,
),
offset: const Offset(-20, 0),
scrollbarTheme: ScrollbarThemeData(
radius: const Radius.circular(40),
thickness: MaterialStateProperty.all(6),
thumbVisibility: MaterialStateProperty.all(true),
),
),
menuItemStyleData: const MenuItemStyleData(
padding: EdgeInsets.only(left: 14, right: 14),
),
),
),
const SizedBox(height: 16),
ReactiveDropdownButton2<String, String>(
formControlName: 'input2',
isExpanded: true,
inputDecoration: const InputDecoration(
border: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
contentPadding: EdgeInsets.zero,
helperText: "",
helperStyle: TextStyle(height: 0.8),
errorStyle: TextStyle(height: 0.8),
),
hint: Row(
children: [
const SizedBox.square(
dimension: 24,
child: icon,
),
const SizedBox(width: 12),
Expanded(
child: DefaultTextStyle.merge(
style: const TextStyle(
fontSize: 14.0,
height: 1.8,
),
overflow: TextOverflow.ellipsis,
child: hint,
),
),
],
),
underline: const SizedBox.shrink(),
errorButtonStyleData: ButtonStyleData(
height: 48,
padding: const EdgeInsets.only(right: 8),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Theme.of(context).colorScheme.error),
borderRadius: const BorderRadius.all(
Radius.circular(12),
),
),
),
buttonStyleData: ButtonStyleData(
height: 48,
padding: const EdgeInsets.only(right: 8),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: const Color(0xFFBCC2CE)),
borderRadius: const BorderRadius.all(
Radius.circular(12),
),
),
),
iconStyleData: const IconStyleData(
icon: Icon(Icons.arrow_drop_down),
iconEnabledColor: Colors.black,
iconDisabledColor: Colors.grey,
openMenuIcon: Icon(Icons.arrow_drop_up),
),
errorBuilder: (_, error) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Text(
error,
style: const TextStyle(height: 0.8),
),
),
disabledHint: Row(
children: [
SizedBox.square(
dimension: 24,
child: Builder(
builder: (context) {
final color =
DefaultTextStyle.of(context).style.color;
return IconTheme(
data: IconTheme.of(context).copyWith(
color: color,
size: 16,
),
child: icon,
);
},
),
),
const SizedBox(width: 12),
const Text(''),
],
),
selectedItemBuilder: (_) {
return items.map(
(value) {
return DropdownMenuItem<String>(
alignment: Alignment.centerLeft,
value: value,
child: Row(
children: [
SizedBox.square(
dimension: 24,
child: Center(
child: Builder(
builder: (context) {
final color =
DefaultTextStyle.of(context)
.style
.color;
return IconTheme(
data:
IconTheme.of(context).copyWith(
color: color,
size: 16,
),
child: icon,
);
},
), // ?? const SizedBox(),
),
),
// PrefixIcon(
// child:
// ),
const SizedBox(width: 12),
Expanded(
child: Text(
value,
overflow: TextOverflow.ellipsis,
),
),
],
),
);
},
).toList();
},
dropdownStyleData: DropdownStyleData(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
),
padding: EdgeInsets.zero,
maxHeight: 300,
),
menuItemStyleData: const MenuItemStyleData(
padding: EdgeInsets.symmetric(horizontal: 8),
),
items: items.map(
(String item) {
return DropdownItem<String>(
value: item,
child: Row(
children: [
const SizedBox.square(dimension: 24),
const SizedBox(width: 12),
Text(item),
],
),
);
},
).toList(),
),
const SizedBox(height: 16),
ElevatedButton(
child: const Text('Submit'),
onPressed: () {
form.markAllAsTouched();
if (form.valid) {
debugPrint(form.value.toString());
}
},
),
],
);
},
),
),
),
),
);
}
}