i have a dropdown which contain list of text, then i want have different color of select text and color the text from the dropdown item.
DropdownButton<String>(
hint: Text(
'Topic',
style: TextStyle(fontFamily: 'Cairo', fontSize: 20),
),
isExpanded: true,
value: dropdownValue,
dropdownColor: Colors.black,
elevation: 4,
//notices 1
style: const TextStyle(
color: Colors.deepPurple, fontFamily: 'Cairo'),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: tpoicList
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Container(
// alignment: Alignment.centerRight,
child: Text(
value,
style: const
//notices 2
TextStyle(color: Colors.black),
),
),
);
}).toList(),
)
i notices that in the notice 1 style is also affect the notice 2 style. All i want to is to have different color in the text dropdown and selected text
what i am trying to archive
In the image you will notices the background color is different from the text color also if i select a item from the select item, the text should be in another colors
The DropdownButton has a selectedItemBuilder parameter that should solve your problem. Add something like this to your code:
selectedItemBuilder: (context) {
final List<DropdownMenuItem<String>> list = [];
for (int i = 0; i < tpoicList.lenght; i++) {
list.add(DropdownMenuItem<String>(
value: tpoicList[i],
child: Container(
child: Text(
value,
style: i == dropdownValue?
[YourNewTextStyle],
const TextStyle(color: Colors.black),
),
),
));
}
return list;
},
[official] change dropdown text colour from selected text
selectedItemBuilder: (BuildContext context) {
return tpoicList.map((String value) {
return Align(
alignment: Alignment.centerLeft,
child: Text(
dropdownValue,
style: const TextStyle(color: Colors.white),
),
);
}).toList();
},
Related
we're trying to do a dropdownbutton in flutter that has the arrow icon static all the way to the right of the button. We've tried doing isExpanded, but that just makes the text and icon dissapear from the box. We don't know what we're doing wrong.
Here is the code for our button:
padding:
const EdgeInsets.only(left: 30, right: 30),
//VIKTIG
child: DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text(
chosenActivity == null
//TODO: Hint text "aktivitet" i mitten av knappen?
? 'Välj aktivitet'
: chosenActivity.name,
style: Styles.h5,
textAlign: TextAlign.center,
),
//isExpanded: false,
value: dropdownvalue,
items: _activities.map((Activity activity) {
return DropdownMenuItem(
value: activity,
child: Text(activity.name),
);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (Object? newValue) {
setState(() {
chosenActivity = newValue!;
});
print(chosenActivity.name);
},
icon: const Padding(
//Icon at tail, arrow bottom is default icon
padding: EdgeInsets.only(left: 30),
child: Icon(
Icons.arrow_circle_down_sharp,
color: Colors.black,
)),
iconEnabledColor: Colors.white,
//Icon color
style: const TextStyle(
//te
color: Colors.black, //Font color
fontSize:
20 //font size on dropdown button
),
),
),
),
We want the icon to be here without it moving:
[1]: https://i.stack.imgur.com/0tQ1b.png
Sorry if this is unclear,
Thanks!
how to make the dropdown to the left, because what i get is the dropdown in the middle
DropdownButton<String>(
value: _chosenValue2,
//elevation: 5,
style: TextStyle(color: Colors.black),
items: <String>[
'Personal Type',
'Other',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
hint: Text(
"Customer Model",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.w600),
),
onChanged: (String value) {
setState(() {
_chosenValue2 = value;
});
},
),
Try below code hope its helpful to you. Add your dropdown widget inside Container and set Alignment.centerLeft
Container(
// padding: EdgeInsets.all(5), if you required some padding to dropdown to set padding
alignment:Alignment.centerLeft,
child: DropdownButton<String>(
value: _chosenValue2,
//elevation: 5,
style: TextStyle(color: Colors.black),
items: <String>[
'Personal Type',
'Other',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
hint: Text(
"Customer Model",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.w600),
),
onChanged: (String value) {
setState(() {
_chosenValue2 = value;
});
},
),
),
Your result screen->
You could use quite a few approaches. You could wrap your drop-down widget in a Container or Align widget and set their alignment property to Alignment.bottomLeft or Alignment.centerLeft or Alignment.topLeft. You could also wrap your dropdown widget inside just a Container and then use margin property to move your widget like margin: EdgeInsets.only(right: 20) or to any value of your desire. Lastly, you could also wrap your widget with a Padding widget and set the padding property to an extent you want your drop-down widget to move leftwards like padding: const EdgeInsets.only(right: 50)
I want to set the index of RadioListTile inside the listview.builder but i can't do that how to do it which I want to get the value of this index here is my code
int _value=0;
List valueList=[];
ListView.Builder(itemBuilder:(context,index){
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: RadioListTile<int>(
dense: true,
activeColor: colorsCheck(),
contentPadding:
EdgeInsets.symmetric(horizontal: 1.0),
title: Text(
'Present',
style: TextStyle(
color: Colors.white, fontSize: 12.0),
),
value: 1,
groupValue: _value,
onChanged: (value) {
setState(() {
_value=value;
valueList.add(value);
print('radio button value present ${valueList.toList().toString()}');
});
},
),
),
Expanded(
child: RadioListTile<int>(
contentPadding:
EdgeInsets.symmetric(horizontal: 1.0),
dense: true,
activeColor: colorsCheck(),
title: Text(
'Absent',
style: TextStyle(
color: Colors.white, fontSize: 12.0),
),
value: 2,
groupValue: _value,
onChanged: (value) {
setState(() {
_value=value;
valueList.add(value);
print('radio button value absent ${valueList.toList().toString()}');
});
},
),
),
Expanded(
child: RadioListTile<int>(
dense: true,
activeColor: colorsCheck(),
contentPadding:
EdgeInsets.symmetric(horizontal: 1.0),
title: Text(
'Leave',
style: TextStyle(
color: Colors.white, fontSize: 12.0),
),
value: 3,
groupValue:_value,
onChanged: (value) {
setState(() {
_value=value;
valueList.add(value);
print('radio button value leave ${valueList.toList().toString()}');
});
},
),
),
],
),
}
Row is inside card where 3 radio button is set (present, absent, leave) but when I click it changes all item builder value. I just want to change the value of that radio inside the card which it clicks. other not. here is an image ...
look inside the picture where the leave radio button is set only top value but it set all.
set index in radiotilelist inside listview.builder
currentStatus = List<int>.filled(data.length, initialValue);
groupValue: currentStatus[index];
currentStatus[index] = newStatus
I have made a drop-down list as in the following screen
enter image description here
Here:
I want the drop-down to be in a box like other TextFormFields and arrow to be bigger
my drop-down list background seems to be blurred and not visible
I want the selected value to be replaced on "Choose the id proof"
Following is my code:
Container(
padding: EdgeInsets.symmetric(horizontal: 20),
color: Colors.white,
child: _hintDown(),
),
SizedBox(height: 10),
my dropdown code is:
DropdownButton _hintDown() => DropdownButton<String>(
items: [
DropdownMenuItem<String>(
value: "1",
child: Text(
"Aadhar Card",
),
),
DropdownMenuItem<String>(
value: "2",
child: Text(
"Pancard",
),
),
],
onChanged: (value) {
print("value: $value");
},
hint: Text(
"Choose the id proof!",
style: TextStyle(
color: Colors.black,
),
),
);
Is this code enough or do I need to go for some other?
There are certain changes in the code as per the requirements. You must visit the flutter documentation and read about using DropdownButton class to achieve what you want
See the code clearly, and implement the code in your code base
// look at the comments too to understand the functionality
String dropdownValue = 'Choose the id proof!';
// Return a widgt container to give the border
// and then wrap it around your DropdownButton
Widget _hintDown() => Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(8.0)
),
child: Padding(
padding: EdgeInsets.all(5.0),
child: DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.arrow_drop_down),
iconSize: 30, //this inicrease the size
elevation: 16,
style: TextStyle(color: Colors.black),
// this is for underline
// to give an underline us this in your underline inspite of Container
// Container(
// height: 2,
// color: Colors.grey,
// )
underline: Container(),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['Choose the id proof!', 'Adhaar Card', 'Pancard', 'Voter card', 'Passport']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
)
)
);
Result
I am trying to use dropdown, and want to have Colors.white for the selected text, and Colors.black54 for the text of drop down list. But when I tried to use color attribute and change it to White, it also change the color of drop down text.
DropdownButton<String>(
//this changed the color of both text, intial text as well dropdown text color
dropdownColor: Colors.white,
value: value,
style: TextStyle(color: Colors.white),
icon: CircleAvatar(
radius: 12,
backgroundColor: Colors.white,
child: Icon(Icons.arrow_drop_down),
),
items: places.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(
value,
//I tried giving text style here , but same result
style: TextStyle(color: Colors.white),
),
);
}).toList(),
onChanged: (_) {
setState(() {
value = _;
});
},
)
Here is the picture of it.
Okay I found the Solution using selectedItemBuilder attribute of dropdown
final List<String> places = ['Delhi', 'Mumbai', 'Kerela', 'Agra'];
DropdownButton<String>(
selectedItemBuilder: (_) {
return places
.map((e) => Container(
alignment: Alignment.center,
child: Text(
e,
style: TextStyle(color: Colors.white),
),
))
.toList();
},
value: value,
icon: CircleAvatar(
radius: 12,
backgroundColor: Colors.white,
child: Icon(Icons.arrow_drop_down),
),
items: places.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(
value,
style: TextStyle(color: Colors.black54),
),
);
}).toList(),
onChanged: (_) {
setState(() {
value = _;
});
},
)
Here is the result