Flutter Dropdown with radio button - flutter

How to Design this type of Dropdown dropdown with radio button - sample
I will tryed more time of this design.. and if I select one option it sohwes only Text
My Code
child: DropdownButton<String>(
value: selectedVtypes,
items: ['New', 'Pre-Owned'].map((String value) {
return DropdownMenuItem(
child: Container(
width: 150,
child: ListTile(
title: Text(value),
trailing: Radio(
value: value,
groupValue: value,
onChanged: (val) {},
),
),
),
value: value,
);
}).toList(),
onChanged: (String? vtype) {
setState(() {
selectedVtypes = vtype;
});
},
),
),```

Related

How to place these two buttons on the left?

I'm trying to put these two buttons on the left, but to no avail. Can you give me any tips on how to make them stay on the left margin? I've tried to remove the Container and align using only the Row, but without success.
Container(
alignment: AlignmentDirectional.bottomStart,
padding: EdgeInsets.fromLTRB(0.0, 5.0, 70.0, 5.0),
child: Row(
children: [
Expanded(
child: ListTile(
title: const Text('CPF'),
leading: Radio<SingingCharacter>(
value: SingingCharacter.cpf,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
),
Expanded(
child: ListTile(
title: const Text('RNE'),
leading: Radio<SingingCharacter>(
value: SingingCharacter.rne,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
),
],
),
),
ListTile occupies an entire row, and if you wrap each ListTile with Expanded widget then it gets divided into 2 ListTile in the same row.
So if you want to create 2 Radio's and align them into the left row, then don't use listTile and expanded.
Instead create a row, pass 2 columns. Each column will have 2 Widgets. A text widget and a Radio Widget. That's how you can achieve your desired outlook.
Scaffold(
appBar: AppBar(),
body: Column(children: [
Row(
children: [
Column(
children: [
const Text("Radio 1"),
Radio<SingingCharacter>(
value: SingingCharacter.cpf,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
],
),
const SizedBox(
width: 20,
),
Column(
children: [
const Text("Radio 2"),
Radio<SingingCharacter>(
value: SingingCharacter.rne,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
],
),
],
)
]),
);
Container(
alignment: AlignmentDirectional.bottomStart,
padding: EdgeInsets.fromLTRB(0.0, 5.0, 70.0, 5.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start, // add this line
children: [
// Expanded(
// child:
ListTile(
title: const Text('CPF'),
leading: Radio<SingingCharacter>(
value: SingingCharacter.cpf,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
// ),
// Expanded(
// child:
ListTile(
title: const Text('RNE'),
leading: Radio<SingingCharacter>(
value: SingingCharacter.rne,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
// ),
],
),
),
the closest I got was using this code:
Row(
children: [
Expanded(
child:
ListTile(
contentPadding: EdgeInsets.only(left: 0.0,),
title: const Text('CPF'),
leading: Radio<SingingCharacter>(
value: SingingCharacter.cpf,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
),
Expanded(
child:
ListTile(
contentPadding: EdgeInsets.only(left: 0.0,),
title: const Text('RNE'),
leading: Radio<SingingCharacter>(
value: SingingCharacter.rne,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
),
],
),

How to add flutter radio button with right side of image

I'm beginner for the flutter and I'm try to added radio button with right side to images , but its not working correctly, anyone know how to do that correctly
Thanks
here the code
Container(
margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.all(3.0),
decoration: BoxDecoration( borderRadius: BorderRadius.circular(8),
border: Border.all( color: Color(0xFFD8D8D8),)
),
child: Column( mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
ListTile(
title: const Text('Debit Card'),
leading: Radio<SingingCharacter>(
value: SingingCharacter.lafayette,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
ListTile(
title: const Text('Credit Card'),
leading: Radio<SingingCharacter>(
value: SingingCharacter.jefferson,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
],
),
),
I Think you use RadioListTile try to below answer Hope it help.
enum SingingCharacter { one, two }
SingingCharacter? _character = SingingCharacter.one;
//Your Widget
Column(
children: <Widget>[
RadioListTile<SingingCharacter>(
title: const Text('Credit Card'),
value: SingingCharacter.one,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
RadioListTile<SingingCharacter>(
title: const Text('Debit Card'),
value: SingingCharacter.two,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
],
);
Or You Add this Radio Button on right side of the Name Just use the trailing in Listview insted of leading.
Column(
children: <Widget>[
ListTile(
title: const Text('Credit Card'),
trailing: Image.network('https://picsum.photos/250?image=9'),
leading: Radio<SingingCharacter>(
value: SingingCharacter.one,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
ListTile(
title: const Text('Debit Card'),
trailing: Image.network('https://picsum.photos/250?image=9'),
leading: Radio<SingingCharacter>(
value: SingingCharacter.two,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
],
)
Your Screen using RadioListTile ->
Your screen using ListTile with image
You can use like this:
ListTile(
title: const Text('Debit Card'),
trailing: Radio( // type code here
you might want to use a RadioListTile Instead of a ListTile
RadioListTile<SingingCharacter>(
title: Text('Credit Card'),
controlAffinity: ListTileControlAffinity.trailing,//**this will help align the control to the right**
value: SingingCharacter.jefferson,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
);

Dropdown is not keeping the value selected

I am working on a dropdown and I have the values in a variable, but I can't get the selected value to be shown, instead is always a static value on.
So far I did this:
class DropDownWidget extends State {
String dropdownValue = 'Activities';
String holder = '';
List<String> postType = ['Activities', 'Sell/buy', 'New Friends', 'City Recommendations', 'Post'];
void getDropDownItem() {
setState(() {
holder = dropdownValue;
});
}
#override
Widget build(BuildContext context) {
return Container(
height: 100,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/img/blue.png'),
fit: BoxFit.cover,
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
child: DropdownButton<String>(
value: dropdownValue,
icon: Icon(
Icons.keyboard_arrow_down_outlined,
color: TheBaseColors.lightRed,
),
iconSize: 30,
elevation: 16,
style: TextStyle(color: TheBaseColors.lightRed, fontSize: 18),
onChanged: (String data) {
setState(() {
dropdownValue = data;
});
switch (data) {
case 'Activities':
Navigator.push(
context,
MaterialPageRoute(builder: (context) => CreateActivity()),
);
break;
case 'Sell/buy':
Navigator.push(
context,
}
},
items: postType.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
],
),
);
}
}
I have saved the dropdow value in a variable and then set it in the SetState, but how to show the selected item each time?
Try This
String dropdownValue = 'Activities';
DropdownButton<String>(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['Activities', 'Sell/buy', 'New Friends', 'City Recommendations', 'Post']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),

Dropdown button not expanding

I'm trying to build a form with a DropdownButtonFormField with 4 items but i don't know why it doesn't want to expand.
Added isDense : true but it wasn't because of it being too small
Here's the button code
new Container(
child: DropdownButtonFormField(
isDense: false,
hint: Text('Ecole'),
onSaved: (value) {
this._data.ecole = value.toString();
},
items: ['HEI', 'ISEN', 'ISA', 'all'].map((String value) {
return DropdownMenuItem(
value: value,
child: Text(value),
);
}).toList()),
),
1
Try this
String dropdownValue = 'HEI';
DropdownButton<String>(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['HEI', 'ISEN', 'ISA', 'all']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),

remove dropdown bottom line in flutter

How to remove dropdown bottom line?
My Code
Container(
padding: EdgeInsets.fromLTRB(15, 5, 10, 5),
child: Row(
children: <Widget>[
dropdownButton,
Expanded(child: phoneField),
],
),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey, width: 1),
borderRadius: BorderRadius.circular(32.0)),
)
And this is Dropdown
var dropdownButton = DropdownButton(
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['99451', '99450', '99455', '99470 ']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
You have to wrap your DropdownButton inside 
DropdownButtonHideUnderline like this 
var dropdownButton = DropdownButtonHideUnderline(
child: DropdownButton(
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['99451', '99450', '99455', '99470 '].map<DropdownMenuItem<String>>
((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
)
);
you can use like that
underline: SizedBox()
Found a solution using
underline: Container(),
Full Changed Code
var dropdownButton = DropdownButton(
value: dropdownValue,
underline: Container(),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['99451', '99450', '99455', '99470 ']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);