How to create the switch button in a flutter? - flutter

In flutter, how to create a switch button and text in a row. I tried but didn't get what I wanted. And how to remove the tag inside the custom switch.
import 'package:custom_switch/custom_switch.dart';
bool status = true;
Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 40),
child: CustomSwitch(
activeColor: Color(0xff771ae1),
value: status,
onChanged: (value) {
setState(() {});
},
),
SizedBox(height: 12.0,),
Text('Value : $status', style: TextStyle(
color: Colors.black,
fontSize: 20.0
),)
),
],
),

You can use SwitchListTile widget. For your case, you need to use CupertinoSwitch.
CupertinoListTile(
title: Text("title"),
trailing: CupertinoSwitch(
value: true,
thumbColor: Colors.white,
activeColor: Colors.deepPurple,
// trackColor: ,
onChanged: (value) {},
)),

Related

flutter: Right overflowed by pixels

Here is my code:
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.grey[900],
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => Navigator.of(context).pop(false),
),
),
backgroundColor: Colors.grey[900],
body: Visibility(
visible: questionLoaded,
child: Builder(builder: (context) {
return Wrap(
spacing: 8.0,
runSpacing: 4.0,
direction: Axis.horizontal,
children: [
Container(
width: double.infinity,
child: Text(
question!.question,
textAlign: TextAlign.center,
style: GoogleFonts.mukta(
textStyle: TextStyle(
color: Colors.amber,
fontSize: 24,
shadows: const [
Shadow(
color: Colors.white,
offset: Offset.zero,
blurRadius: 15)
]),
),
),
),
if (question?.answer1 != "")
RadioButton(
textStyle: TextStyle(color: Colors.white),
description: (question?.answer1)!,
value: "1",
groupValue: _decision,
onChanged: (value) => setState(
() => _decision = value!,
),
),
if (question?.answer2 != "")
RadioButton(
textStyle: TextStyle(color: Colors.white),
description: (question?.answer2)!,
value: "2",
groupValue: _decision,
onChanged: (value) => setState(
() => _decision = value!,
),
),
if (question?.answer3 != "")
RadioButton(
textStyle: TextStyle(color: Colors.white),
description: (question?.answer3)!,
value: "3",
groupValue: _decision,
onChanged: (value) => setState(
() => _decision = value!,
),
),
if (question?.answer4 != "")
RadioButton(
textStyle: TextStyle(color: Colors.white),
description: (question?.answer4)!,
value: "4",
groupValue: _decision,
onChanged: (value) => setState(
() => _decision = value!,
),
),
],
);
})),
);
This produces the following issue:
Any idea why and how can I fix it ?
Wrap your Text widget Flexible or Expanded widget.
Expnaded( child: Text( question!.question,....
If your text is wrapped in column then column should be wrapped expanded widget.
Expanded(
child: Column(
children: [
Text(
'datadatadatadatadatadata',
overflow: TextOverflow.ellipsis,
)
],
),
),
But if your text is wrapped in a row then your text should be wrapped expanded widget.
Row(
children: [
Expanded(
child: Text(
'datadatadatadatadatadata',
overflow: TextOverflow.ellipsis,
),
)
],
),

flutter: how to stop dropdown button text from moving with different length

So I have a dropdown button, but the item text has different length, so when I choose different item, the text moves to a different position like shown in the picture. How should I fix that?
Here is the code:
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
DropdownButton<TaskAssort>(
value: _selected,
icon: const Icon(Icons.arrow_drop_down),
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.black,
decoration: TextDecoration.none,
),
isExpanded: true,
onChanged: (TaskAssort? v) {
_selected = v!;
setState(() {});
},
items: [
DropdownMenuItem(
value: TaskAssort.Unfinished,
child: Text(getAssortText(TaskAssort.Unfinished),
textAlign: TextAlign.end),
),
DropdownMenuItem(
value: TaskAssort.UnfinishedDaily,
child: Text(getAssortText(TaskAssort.UnfinishedDaily),
textAlign: TextAlign.end),
),
DropdownMenuItem(
value: TaskAssort.Histroy,
child: Text(getAssortText(TaskAssort.Histroy),
textAlign: TextAlign.end),
),
]),
],
),
Set alignment of DropdownButton
alignment: Alignment.centerRight

how to display remember me and forget password buttons on the same line.- flutter

I want to display remember me button and the forget password text in a same line. how can I do this. image is showing my implementation so far. appreciate your help on this
CheckboxListTile(
title: const Text(
"Remember Me",
style: TextStyle(
color: textGrey, fontFamily: "Dubai", fontSize: 14),
),
value: checkedValue,
onChanged: (newValue) {
FocusManager.instance.primaryFocus?.unfocus();
setState(() {
if (isLoading != true) {
checkedValue = newValue!;
print(newValue);
}
});
},
contentPadding: EdgeInsets.only(left: 0, top: 0),
controlAffinity:
ListTileControlAffinity.leading, // <-- leading Checkbox
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
child: Text(
"Forget Password",
style: TextStyle(
color: textGrey, fontFamily: "Dubai", fontSize: 14),
),
onPressed: () {
//Get.to(ForgetPassword());
},
)
],
),
You can try this, it's solve my problem.
Create a row
Wrap CheckboxListTile with sizedbox
Add width to the SizedBox
Add SizedBox into a this row
An example:
Row(
children [
SizedBox(
width: ..
height: ..
child: CheckboxListTile(...)
),
Text(...)
]
)

RadioListTile inside Listview.builder index in flutter

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

RadioListTile does not select correctly

I am Vaibhav Pathak. I have a a problem in using RadioListTile. I created two RadioListTile where on onChnaged property to call a function in which in call setState to set the value of selectedRadio variable to that value which I got from the function and It changed the variable value but does not reflect changes to the Ui means it does not select the pressed radio button.
My code is here :
showPaymentOptions() {
showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(25.0),
topRight: Radius.circular(25.0))),
elevation: 8.0,
context: context,
builder: (context) {
return Padding(
padding: const EdgeInsets.all(15.0),
child: Container(
height: 240,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Column(
children: <Widget>[
Align(
alignment: Alignment.topLeft,
child: Text(
"Select Payment Method : ",
style: TextStyle(
color: Colors.black54,
fontSize: 16.0,
fontWeight: FontWeight.bold),
),
),
SizedBox(
height: 7.0,
),
Card(
elevation: 8.0,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
RadioListTile(
activeColor: myTheme.primaryColor,
title: Text("COD"),
value: 1,
groupValue: selectedPaymentMethod,
onChanged: (value) =>
_changePayment(value)),
SizedBox(
height: 5.0,
),
RadioListTile(
activeColor: myTheme.primaryColor,
title: Text("Paytm"),
value: 2,
groupValue: selectedPaymentMethod,
onChanged: (value) =>
_changePayment(value)),
],
),
),
),
],
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Pay ₹499",
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold),
),
isOrderSuccessful
? RaisedButton(
onPressed: () =>
placeOrder(selectedPaymentMethod),
color: myTheme.primaryColor,
textColor: Colors.white,
child: Text(
"Place Order",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16.0),
))
: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
myTheme.primaryColor))
],
),
],
),
)
],
)),
);
});
}
Code of setState function :
_changePayment(int value) {
print(value);
print(selectedPaymentMethod);
setState(() {
selectedPaymentMethod = value;
});
}
friends I have done it by myself. I used the StatefulBuilder widget and wrap the Column inside the column children I put my two ReadioListTile and in the onChanged property I put the _setStatewhich I got from the builder of StatefulBuilder and set the value of payment method variable to its value and then it works properly in which way I want it.
Answer Code :
StatefulBuilder(
builder: (context, _setState) {
return Column(
children: <Widget>[
RadioListTile(
activeColor: myTheme.primaryColor,
title: Text("Cash on Delivery"),
value: 1,
groupValue: selectedPaymentMethod,
onChanged: (value) => _setState(() {
selectedPaymentMethod = value;
})),
RadioListTile(
activeColor: myTheme.primaryColor,
title: Text("Paytm"),
value: 2,
groupValue: selectedPaymentMethod,
onChanged: (value) => _setState(() {
selectedPaymentMethod = value;
})),
],
);
},
),