Comments (12)
Duplicate of #138
from dropdown_button2.
I tried the solution you provided but the same error appears @AhmedLSayed9
from dropdown_button2.
Your code is hard to read, can you format it?
You should use:
```dart
yourCode
```
from dropdown_button2.
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../utils/app_colors.dart';
class CustomDropDown extends StatelessWidget {
CustomDropDown(
{super.key,
required this.list,
this.items,
this.height,
this.iconSize = 24,
required this.hint,
this.isEmpty = false,
required this.onOptionSelected,
this.iconColor,
this.onTap,
this.width,
this.title,
this.validator});
final List list;
final List<DropdownMenuItem>? items;
final String hint;
static String? selectedValue;
final void Function(OptionItem?) onOptionSelected;
final double iconSize;
final Color? iconColor;
final bool isEmpty;
final void Function()? onTap;
final double? height;
final double? width;
final String? title;
final String? Function(OptionItem?)? validator;
final TextEditingController textEditingController = TextEditingController();
@OverRide
Widget build(BuildContext context) {
return DropdownButtonFormField2(
isExpanded: true,
hint: Text(
hint,
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(color: AppColors.textColor, fontSize: 16),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
items: items ??
list
.map((item) => DropdownMenuItem(
value: OptionItem(
id: item.id,
title: item.title,
subtitle: item.subtitle,
description: item.description,
image: item.image),
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
item.title,
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(color: AppColors.textColor, fontSize: 16),
),
),
))
.toList(),
validator: validator ??
(value) {
if (value == null) {
return "please enter $title";
}
return null;
},
onChanged: (value) {
onOptionSelected(value);
},
onSaved: (value) {
selectedValue = value.toString();
},
buttonStyleData: const ButtonStyleData(
padding: EdgeInsets.only(right: 8),
),
dropdownStyleData: DropdownStyleData(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
),
),
menuItemStyleData: const MenuItemStyleData(
padding: EdgeInsets.symmetric(horizontal: 16),
),
);
}
}
class OptionItem extends Equatable {
final String id;
final String title;
final String? subtitle;
final String description;
final String image;
const OptionItem(
{required this.id,
required this.title,
this.description = "",
this.subtitle,
this.image = ""});
@OverRide
List<Object?> get props => [id, title, description, image, subtitle];
}
from dropdown_button2.
It still unformatted 😃
from dropdown_button2.
What should i do 😂
from dropdown_button2.
Add ```dart at the beginning your code and ``` at the end of your code
from dropdown_button2.
This file contains dropdown widget only
from dropdown_button2.
I meant formatting the code here at github
from dropdown_button2.
class CustomDropDown extends StatelessWidget {
CustomDropDown(
{super.key,
required this.list,
this.items,
this.height,
this.iconSize = 24,
required this.hint,
this.isEmpty = false,
required this.onOptionSelected,
this.iconColor,
this.onTap,
this.width,
this.title,
this.validator});
final List list;
final List? items;
final String hint;
static String? selectedValue;
final void Function(OptionItem?) onOptionSelected;
final double iconSize;
final Color? iconColor;
final bool isEmpty;
final void Function()? onTap;
final double? height;
final double? width;
final String? title;
final String? Function(OptionItem?)? validator;
final TextEditingController textEditingController = TextEditingController();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@OverRide
Widget build(BuildContext context) {
return DropdownButtonFormField2(
isExpanded: true,
hint: Text(
hint,
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(color: AppColors.textColor, fontSize: 16),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
items: items ??
list
.map((item) => DropdownMenuItem(
value: OptionItem(
id: item.id,
title: item.title,
subtitle: item.subtitle,
description: item.description,
image: item.image),
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
item.title,
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(color: AppColors.textColor, fontSize: 16),
),
),
))
.toList(),
validator: validator ??
(value) {
if (value == null) {
return "please enter $title";
}
return null;
},
onChanged: (value) {
onOptionSelected(value);
},
onSaved: (value) {
selectedValue = value.toString();
},
buttonStyleData: const ButtonStyleData(
padding: EdgeInsets.only(right: 8),
),
dropdownStyleData: DropdownStyleData(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
),
),
menuItemStyleData: const MenuItemStyleData(
padding: EdgeInsets.symmetric(horizontal: 16),
),
);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class OptionItem extends Equatable {
final String id;
final String title;
final String? subtitle;
final String description;
final String image;
const OptionItem(
{required this.id,
required this.title,
this.description = "",
this.subtitle,
this.image = ""});
@OverRide
List<Object?> get props => [id, title, description, image, subtitle];
} #
from dropdown_button2.
Like this 😅
from dropdown_button2.
No. Your code should be formatted like this one:
code
Anyway, You can't use String
for selectedValue
while using OptionItem
as the value of your items.
To be compile-safe, you can annotate your DropdownButtonFormField2
with the type you want. i.e:
return DropdownButtonFormField2< OptionItem>(
....
);
from dropdown_button2.
Related Issues (20)
- Very slow loading multiple dropdown buttons on one screen HOT 4
- Dropdown remains static on iPhone app HOT 6
- Allow closing dropdown programatically HOT 5
- Accept focusNode property in DropdownItem
- Cannot simultaneously update items and values in beta HOT 2
- The splash effect goes above the error / validation text
- Close programatically on Escape pressed HOT 4
- How can i create menu items with exactly height for each element
- Not possible to have label, text and dropdown values all aligned with OutlineInputBorder HOT 2
- Show selected item when page initialized HOT 6
- onChanged doesn't work when using multiValueListenable HOT 2
- DropdownButtonFormField2 initial value
- barrierCoversButton: false property not working
- Not resetting the search querry before reopening the menu breaks the dropdown menu scroll position calculation algorithm
- The package interferes with TextField HOT 1
- Being able to set the errorText
- Ink splashes get displayed over search widget HOT 2
- Disable focus on DropdownItem
- How to wrap TapRegion with groupID around DropdownButtonFormField2 to track TapInside / TapOutside with other TapRegions
- Add an ability to override comparator function HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dropdown_button2.