animated_custom_dropdown_v2 1.5.2
animated_custom_dropdown_v2: ^1.5.2 copied to clipboard
Custom dropdown widget allows to add highly customizable widget in your projects with proper open and close animations and also comes with form required validation.
example/lib/main.dart
import 'package:animated_custom_dropdown_v2/custom_dropdown.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const App());
}
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Custom Dropdown App',
home: Home(),
);
}
}
const _labelStyle = TextStyle(fontWeight: FontWeight.w600);
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class RoleItem extends DropDownItem {
final String value;
RoleItem({required this.value});
@override
String get displayText => value;
@override
String toString() {
return 'RoleItem{value: $value}';
}
}
class _HomeState extends State<Home> {
final formKey = GlobalKey<FormState>();
final List<RoleItem> list = ['Developer', 'Designer', 'Consultant', 'Student']
.map((e) => RoleItem(value: e))
.toList();
RoleItem? selectedItem1;
RoleItem? selectedItem2;
RoleItem? selectedItem3;
RoleItem? selectedItem4;
Future<List<RoleItem>> getFakeRequestData(String query) async {
print('Future Search Query: $query');
List<RoleItem> data = list;
return await Future.delayed(const Duration(seconds: 1), () {
return data.where((e) {
return e.value.toLowerCase().contains(query.toLowerCase());
}).toList();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
systemOverlayStyle: SystemUiOverlayStyle.dark.copyWith(
statusBarColor: Colors.white,
),
backgroundColor: Colors.white,
elevation: .25,
title: const Text(
'Custom Dropdown Example',
style: TextStyle(color: Colors.black, fontSize: 18),
),
),
body: ListView(
padding: const EdgeInsets.all(16.0),
children: [
const Text('Job Roles Dropdown', style: _labelStyle),
const SizedBox(height: 8),
CustomDropdownV2<RoleItem>(
hintText: 'Select job role',
items: list,
excludeSelected: false,
onItemSelected: (value) {
print('onItemSelected item $value');
setState(() {
selectedItem1 = value;
});
},
selectedItem: selectedItem1,
),
const SizedBox(height: 24),
const Divider(height: 0),
const SizedBox(height: 24),
// dropdown having search field
const Text('Job Roles Search Dropdown', style: _labelStyle),
const SizedBox(height: 8),
CustomDropdownV2<RoleItem>.search(
hintText: 'Select job role',
items: list,
onItemSelected: (value) {
setState(() {
selectedItem2 = value;
});
print('onItemSelected item $value');
},
onSearchTextChange: (value) {
print('onSearchTextChange $value');
},
selectedItem: selectedItem2,
emptyResultWidget: const Center(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 12.0),
child: Text(
'Add New Job Role',
style: TextStyle(fontSize: 16),
),
),
),
onEmptyResultClick: () {
print('onEmptyResultClick');
},
),
const SizedBox(height: 24),
const Divider(height: 0),
const SizedBox(height: 24),
// dropdown having search request field (making fake call)
const Text('Job Roles Search Request Dropdown', style: _labelStyle),
const SizedBox(height: 8),
CustomDropdownV2<RoleItem>.searchRequest(
selectedItem: selectedItem3,
futureRequest: getFakeRequestData,
hintText: 'Search job role',
// waits 3 seconds before start searching (before execute the 'futureRequest' function)
futureRequestDelay: const Duration(seconds: 3),
onItemSelected: (value) {
setState(() {
selectedItem3 = value;
});
print('onItemSelected item $value');
},
),
const SizedBox(height: 24),
const Divider(height: 0),
const SizedBox(height: 24),
// using form for validation
Form(
key: formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Job Roles Dropdown with Form validation',
style: _labelStyle,
),
const SizedBox(height: 8),
CustomDropdownV2<RoleItem>.search(
selectedItem: selectedItem4,
hintText: 'Select job role',
items: list,
excludeSelected: false,
listItemBuilder: (context, result) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(result.displayText),
const Icon(Icons.person)
],
);
},
onItemSelected: (value) {
setState(() {
selectedItem4 = value;
});
print('onItemSelected item $value');
},
onSearchTextChange: (value) {
print('onSearchTextChange $value');
},
),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
if (!formKey.currentState!.validate()) {
return;
}
},
child: const Text(
'Submit',
style: TextStyle(fontWeight: FontWeight.w600),
),
),
),
],
),
),
],
),
);
}
}