How to add flutter radio button with right side of image - flutter

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;
});
);

Related

How to implement checkbox list in flutter?

I am a flutter beginner. How to implement 2 checkbox lists in flutter like below?
Padding(
padding: const EdgeInsets.only(top: 20),
child: CheckboxlListTitle(
title: const Text('Title1'),
value: _isChecked,
onChanged: (bool? newValue) {
setState(() {
_isChecked = newValue;
});
},
activeColor: Colors.green,
controlAffinity: ListTileControlAffinity.leading,
tristate: false,
),
),
You need to create two bool variable to control two checkBox.
class TestFe161 extends StatefulWidget {
const TestFe161({super.key});
#override
State<TestFe161> createState() => _TestFe161State();
}
class _TestFe161State extends State<TestFe161> {
bool _isChecked0 = false;
bool _isChecked1 = false;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Padding( // you can use helper method/widget to reduce the snippet size.
padding: const EdgeInsets.only(top: 20),
child: CheckboxListTile(
title: const Text('Title1'),
value: _isChecked0,
onChanged: (bool? newValue) {
setState(() {
_isChecked0 = newValue ?? false;
});
},
activeColor: Colors.green,
controlAffinity: ListTileControlAffinity.leading,
),
),
Padding(
padding: const EdgeInsets.only(top: 20),
child: CheckboxListTile(
title: const Text('Title2'),
value: _isChecked1,
onChanged: (bool? newValue) {
setState(() {
_isChecked1 = newValue ?? false;
});
},
activeColor: Colors.green,
controlAffinity: ListTileControlAffinity.leading,
),
),
],
),
);
}

How do I reduce the space between Radio Buttons and shift them more towards left in Flutter

I have implemented Radio button in Flutter namely 'Today' , 'Tomorrow' , 'User Input Date', and have wrapped them in a Column, but I am unable to reduce the space between the 3 radio buttons and also I am not able to move them towards the left. Is there any way I can shift them towards left side and decrease the default space taken between the radio button. Also I want to stick to the use of RadioListTile only.
I am attaching a picture to give a clear idea.
Code
Column(
children: [
RadioListTile<DateTime>(
activeColor: Colors.white,
title: Text(
"Today",
style: Theme.of(context).textTheme.bodyMedium,
),
value: today,
groupValue: selectedMatchDateFilter,
onChanged: (DateTime? value) => setState(() {
selectedMatchDateFilter = value;
}),
),
RadioListTile<DateTime>(
activeColor: Colors.white,
title: Text(
"Tomorrow",
style: Theme.of(context).textTheme.bodyMedium,
),
value: DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day + 1),
groupValue: selectedMatchDateFilter,
onChanged: (DateTime? value) => setState(() {
selectedMatchDateFilter = value;
}),
),
RadioListTile<DateTime>(
activeColor: Colors.white,
title: Row(
children: [
DropdownButton2<String>(
isExpanded: true,
buttonHeight: 30.h,
buttonWidth: 220.w,
items: const [
DropdownMenuItem<String>(
value: "",
child: Text("Till Date"),
),
DropdownMenuItem<String>(
value: "",
child: Text("Precise Date"),
),
],
),
1 == 2
? Checkbox(
value: true,
onChanged: (bool? _value) {},
)
: IconButton(
icon: const Icon(Icons.calendar_today),
onPressed: () => showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2022, 11, 16),
lastDate: DateTime(2023, 1, 1),
),
),
],
),
value: DateTime.now(),
groupValue: selectedMatchDateFilter,
onChanged: (value) {},
)
],
),
You can add visualDensity and dense and contentPadding like this:
RadioListTile<DateTime>(
activeColor: Colors.white,
visualDensity: VisualDensity(horizontal: -4, vertical: -4),//<-- add this
dense: true,//<-- add this
contentPadding: EdgeInsets.zero,//<-- add this
title: Text(
"Tomorrow",
style: Theme.of(context).textTheme.bodyMedium,
),
value: DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day + 1),
groupValue: selectedMatchDateFilter,
onChanged: (DateTime? value) => setState(() {}),
),
you can't get better than this, if its not good for you, you need make a custom tile like this:
Widget CustomRadioTile<T>(
{required Function(T?) onChanged,
required String title,
required T groupValue,
required T value,
required BuildContext context}) {
return InkWell(
onTap: () {
onChanged(value);
},
child: Row(
children: [
SizedBox(
height: 20,
width: 20,
child: Radio<T>(
value: value,
groupValue: groupValue,
onChanged: (value) {},
),
),
Padding(
padding: const EdgeInsets.only(left: 4.0),
child: Text(
title,
style: Theme.of(context).textTheme.bodyMedium,
),
),
],
),
);
}
and use it like this:
CustomRadioTile<DateTime>(
onChanged: (DateTime? value) => setState(() {
selectedMatchDateFilter = value;
}),
title: 'Tomorrow',
groupValue: selectedMatchDateFilter,
value: DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day + 1),
context: context),
There isn't any options to change the width between the Radio and Text itself.
Instead you can create a separate widget which combines Radio and Text widgets.
class LabeledRadio extends StatelessWidget {
const LabeledRadio({
super.key,
required this.label,
required this.padding,
required this.groupValue,
required this.value,
required this.onChanged,
});
final String label;
final EdgeInsets padding;
final bool groupValue;
final bool value;
final ValueChanged<bool> onChanged;
#override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
if (value != groupValue) {
onChanged(value);
}
},
child: Padding(
padding: padding,
child: Row(
children: <Widget>[
Radio<bool>(
groupValue: groupValue,
value: value,
onChanged: (bool? newValue) {
onChanged(newValue!);
},
),
Text(label),
],
),
),
);
}
}
Use this widget as a radio box then replace RadioListTile with this.

how to make unselected color using radiotiles

I know that I can wrap my radio widgets with theme widget but in my case, i use different ways. how to make my radio button have a grey background color when on unselected?
#override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
RadioListTile<SingingCharacter>(
title: const Text('Akaun Individu', style: TextStyle(fontWeight: FontWeight.bold),),
subtitle: Text('Membuat pembayaran untuk diri sendiri'),
controlAffinity: ListTileControlAffinity.trailing,
value: SingingCharacter.akaunSendiri,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
Divider(thickness: 2),
RadioListTile<SingingCharacter>(
title: const Text('Akaun Syarikat', style: TextStyle(fontWeight: FontWeight.bold),),
subtitle: Text('Membuat pembayaran untuk organisasi'),
controlAffinity: ListTileControlAffinity.trailing,
value: SingingCharacter.akaunSyarikat,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
],
);
}
}
You can do this with overriding theme data.
Example:
...
body: Center(
child: Theme(
data: Theme.of(context).copyWith(
unselectedWidgetColor: Colors.purple,
),
child: Column(
children: [
RadioListTile<String>(
title: const Text(
'RadioListTile 1',
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: const Text('Selected'),
controlAffinity: ListTileControlAffinity.trailing,
value: 'value',
groupValue: 'value',
onChanged: (String? value) {},
),
RadioListTile<String>(
title: const Text(
'RadioListTile 2',
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: const Text('Unselected'),
controlAffinity: ListTileControlAffinity.trailing,
value: 'value',
groupValue: 'valuex',
onChanged: (String? value) {},
),
],
),
),
),
...
add this property to your tile tileColor: varUnselect ? Colors.grey : Colors.transparent, varUnselect is get value of unselect radio

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;
});
},
),
),
),
],
),

Flutter Dropdown with radio button

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;
});
},
),
),```