Cards as items in the DropdownMenuItem - flutter

I would like to know if it is possible to have card widgets as the items in a dropdownmenu button in flutter.
I have tried, but I haven't managed to make the cards visible in the dropdown.
Below are the codes
The class for the cards
class AccountContainer extends StatefulWidget {
#override
_AccountContainerState createState() => _AccountContainerState();
}
class _AccountContainerState extends State<AccountContainer> {
#override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(15.0),
margin: EdgeInsets.symmetric(vertical: 15.0),
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey[300],
),
borderRadius: BorderRadius.circular(25.0)
),
child: Column(
children: <Widget>[
Card(
margin: EdgeInsets.symmetric(vertical: 20.0, horizontal: 20.0),
child: Container(
padding: EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Saving Account',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),),
SizedBox(height: 5.0,),
Text('Savings XXX-X-XX563-9',
style: TextStyle(
fontSize: 10.0,
color: Colors.grey[900],
),),
SizedBox(height: 15.0,),
Container(
child: Row(
children: <Widget>[
RichText(
text: TextSpan(
text: '56,302.56',
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
children: <TextSpan> [
TextSpan(text: 'THB',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold
),),
],
),
),
],
),
),
],
),
),
),
Card(
margin: EdgeInsets.symmetric(vertical: 20.0, horizontal: 20.0),
child: Container(
padding: EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Salary Account 2',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),),
SizedBox(height: 5.0,),
Text('Savings XXX-X-XX563-9',
style: TextStyle(
fontSize: 10.0,
color: Colors.grey[900],
),),
SizedBox(height: 15.0,),
Container(
child: Row(
children: <Widget>[
RichText(
text: TextSpan(
text: '89,302.56',
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
children: <TextSpan> [
TextSpan(text: 'THB',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold
),),
],
),
),
],
),
),
],
),
),
),
],
),
);
}
}
Calling the card class
class AccountSelections extends StatefulWidget {
#override
_AccountSelectionsState createState() => _AccountSelectionsState();
}
class _AccountSelectionsState extends State<AccountSelections> {
int accountSelected = 0;
var accountNames;
#override
Widget build(BuildContext context) {
return Container(
constraints: BoxConstraints.expand(width: 419.0, height: 150.0),
child:
DropdownButton(
icon: Icon(Icons.keyboard_arrow_down,
color: Colors.blue[500],),
isExpanded: true,
value: accountSelected,
onChanged: (value) {
setState(() {
accountSelected = value;
});
},
items: accountNames.map((value) {
return DropdownMenuItem(
value: AccountContainer(),
child: Text(value),
);
})?.toList(),
),
);
}
}

Wrap you Text(value) widget with card and give elevation to card

Related

Flutter - Align Items inside a Row - Multiple tiles

I have the following List.
I would like the scale Icons (on the right) to be aligned.
The top should be in the same spot as the one underneath.
#override
Widget build(BuildContext context) {
return Padding(
padding:
const EdgeInsets.only(left: 0.0, right: 0.0, top: 8.0, bottom: 8.0),
child: Slidable(
endActionPane: ActionPane(motion: StretchMotion(), children: [
//delete option
SlidableAction(
onPressed: deleteTapped,
backgroundColor: Colors.red.shade400,
icon: Icons.delete,
borderRadius: BorderRadius.circular(8),
)
]),
child: Container(
padding: const EdgeInsets.all(16.0),
decoration: BoxDecoration(
color: Color(0xFFD9F0FF),
borderRadius: BorderRadius.circular(8),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
const Icon(
Icons.date_range_outlined,
color: Color(0xFF006B8F),
),
const SizedBox(width: 8),
Text(date,
style: const TextStyle(
fontWeight: FontWeight.w400,
fontSize: 18,
color: Color(0xFF006B8F))),
const Spacer(),
const Text(
"|",
style: TextStyle(
fontSize: 18,
color: Color(0xFF006B8F),
fontWeight: FontWeight.bold),
),
const Spacer(),
const Icon(
Icons.monitor_weight_outlined,
color: Color(0xFF006B8F),
),
const SizedBox(width: 8),
Text(weight + " kg",
style: const TextStyle(
fontWeight: FontWeight.w400,
fontSize: 18,
color: Color(0xFF006B8F))),
],
),
),
),
);
}
}
I have tried several things, like putting the Icon and the text in a container but with no luck.
Any help appreciate it!
While you are already using Spacer on both Size, you can use
textAlign: TextAlign.center,
const Text(
"|",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
color: Color(0xFF006B8F),
fontWeight: FontWeight.bold),
),
Without spacer, it can be
Expanded(
child: const Text(
"|",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
color: Color(0xFF006B8F),
fontWeight: FontWeight.bold),
),
),
try this:
Column(
children: [
IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
children: [
//left part
],
),
VerticalDivider(),
Row(
children: [
//right part
],
)
],
),
),
....
],
)

Flutter how to add items in list

Here is my code
class _VariantsState extends State<Variants> {
List<VariantDetails> variantsList = [];
#override
void initState() {
super.initState();
variantsList.clear();
}
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
color: Colors.white,
elevation: 5,
margin:
EdgeInsets.only(top: 40.0, bottom: 40.0, left: 20.0, right: 20.0),
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(
top: 10, bottom: 5.0, left: 20.0, right: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Variants',
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.black),
),
IconButton(
onPressed: () {
setUpData();
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (BuildContext context) {
return SingleChildScrollView(
child: Container(
color: Colors.white,
margin: EdgeInsets.all(10.0),
child: Flexible(
child: Padding(
padding: EdgeInsets.only(
left: 10.0, right: 10.0),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment:
MainAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Image.asset(
'assets/svg/barcode_reader.png',
height: 20.0,
width: 20.0,
),
Padding(
padding: EdgeInsets.only(
left: 20.0)),
Text(
'Add Goods',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
fontSize: 17.0),
)
],
),
Form(
key: _formKey,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(
bottom: 20.0,
top: 40.0,
),
child: TextFormField(
// initialValue:
// variantDetails.name,
validator: (value) {
if (value
.trim()
.isEmpty) {
return 'Enter Name';
}
return null;
},
onChanged: (value) =>
variantDetails.name =
value,
decoration:
InputDecoration(
border:
OutlineInputBorder(),
hintText: 'Name',
hintStyle: TextStyle(
fontSize: 13.0),
hintMaxLines: 1,
contentPadding:
EdgeInsets.fromLTRB(
20.0,
8.0,
20.0,
8.0),
),
),
),
Padding(
padding: EdgeInsets.only(
bottom: 10.0,
),
child: TextFormField(
// initialValue:
// variantDetails
// .description,
onChanged: (value) =>
variantDetails
.description =
value,
validator: (value) {
if (value
.trim()
.isEmpty) {
return 'Enter Description';
}
return null;
},
decoration:
InputDecoration(
border:
OutlineInputBorder(),
hintText: 'Description',
hintStyle: TextStyle(
fontSize: 13.0),
hintMaxLines: 1,
contentPadding:
EdgeInsets.fromLTRB(
20.0,
15.0,
20.0,
15.0),
),
),
),
Row(
mainAxisAlignment:
MainAxisAlignment
.spaceAround,
children: <Widget>[
Expanded(
child: TextFormField(
// initialValue:
// variantDetails
// .price,
onChanged: (value) =>
variantDetails
.price =
value,
validator: (value) {
if (value
.trim()
.isEmpty) {
return 'Enter Price';
}
return null;
},
keyboardType:
TextInputType
.phone,
decoration:
InputDecoration(
border:
OutlineInputBorder(),
hintText: 'Price',
hintStyle:
TextStyle(
fontSize:
13.0),
hintMaxLines: 1,
contentPadding:
EdgeInsets
.fromLTRB(
20.0,
15.0,
20.0,
15.0),
),
),
),
Padding(
padding:
EdgeInsets.only(
left: 10.0)),
Expanded(
child: TextFormField(
// initialValue:
// variantDetails
// .nonTax,
decoration:
InputDecoration(
border:
OutlineInputBorder(),
hintText:
'Inclusive of GST',
hintStyle:
TextStyle(
fontSize:
13.0),
hintMaxLines: 1,
suffixIcon: Icon(Icons
.arrow_drop_down),
contentPadding:
EdgeInsets
.fromLTRB(
20.0,
15.0,
20.0,
15.0),
),
),
)
],
),
],
)),
Padding(
padding:
EdgeInsets.only(top: 10.0)),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Net Price',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
fontSize: 17.0),
),
Text(
'0',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
fontSize: 17.0),
)
],
),
Padding(
padding:
EdgeInsets.only(top: 10.0)),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Total',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
fontSize: 17.0),
),
Text(
'0',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
fontSize: 17.0),
)
],
),
Padding(
padding:
EdgeInsets.only(top: 10.0)),
ElevatedButton(
onPressed: () {
trySubmit(context);
},
child: Text('UPDATE'))
],
),
),
),
));
});
},
icon: Icon(Icons.add_circle_outline)),
],
),
),
Visibility(
visible: isVisible,
child: new ListView.builder(
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return new ExpandableListView('Variant $index', ItemList);
},
itemCount: variantsList.length,
),
)
],
),
));
//Here is the expandable list where i am showing my listview
class ExpandableListView extends StatefulWidget {
final String title;
final List<VariantDetails> variantList;
ExpandableListView(this.title, this.variantList);
#override
_ExpandableListViewState createState() =>
new _ExpandableListViewState(this.title, this.variantList);
}
class _ExpandableListViewState extends State<ExpandableListView> {
bool expandFlag = false;
final String title;
final List<VariantDetails> variantList;
_ExpandableListViewState(this.title, this.variantList);
#override
Widget build(BuildContext context) {
return new Container(
margin: new EdgeInsets.symmetric(vertical: 1.0),
child: new Column(
children: <Widget>[
new Container(
color: Colors.white,
padding: new EdgeInsets.symmetric(horizontal: 20.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(
widget.title,
style: new TextStyle(
fontWeight: FontWeight.bold, color: Colors.black),
),
new IconButton(
icon: new Container(
height: 20.0,
width: 20.0,
child: new Center(
child: new Icon(
expandFlag
? Icons.keyboard_arrow_up
: Icons.keyboard_arrow_down,
color: Colors.black,
size: 30.0,
),
),
),
onPressed: () {
setState(() {
expandFlag = !expandFlag;
});
}),
],
),
),
new ExpandableContainer(
expanded: expandFlag,
child: new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new Container(
child: Padding(
padding:
EdgeInsets.only(left: 20.0, right: 20.0, top: 10.0),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Text(
'Edit Variants',
style: TextStyle(
fontSize: ScreenUtil().setSp(14),
fontFamily: 'poppins',
color: Colors.black),
),
Row(
children: <Widget>[
IconButton(
onPressed: () {
// Dismissible(key: Key(variantList[index]), child: child)
},
icon: Icon(Icons.delete)),
Icon(Icons.edit),
],
)
],
),
Padding(padding: EdgeInsets.only(top: 20)),
Row(
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Name',
style: new TextStyle(
fontFamily: 'poppins',
fontSize: ScreenUtil().setSp(12),
color: Colors.black)),
Text('Price',
style: new TextStyle(
fontFamily: 'poppins',
fontSize: ScreenUtil().setSp(12),
color: Colors.black)),
Text('Description',
style: new TextStyle(
fontFamily: 'poppins',
fontSize: ScreenUtil().setSp(12),
color: Colors.black)),
Text('Price Type',
style: new TextStyle(
fontFamily: 'poppins',
fontSize: ScreenUtil().setSp(12),
color: Colors.black)),
Text('Non-taxable',
style: new TextStyle(
fontFamily: 'poppins',
fontSize: ScreenUtil().setSp(12),
color: Colors.black)),
Text('Code Type',
style: new TextStyle(
fontFamily: 'poppins',
fontSize: ScreenUtil().setSp(12),
color: Colors.black)),
Text('Stock Keeping Unit',
style: new TextStyle(
fontFamily: 'poppins',
fontSize: ScreenUtil().setSp(12),
color: Colors.black)),
Text('Net Price',
style: new TextStyle(
fontFamily: 'poppins',
fontSize: ScreenUtil().setSp(12),
color: Colors.black)),
Text('Tax',
style: new TextStyle(
fontFamily: 'poppins',
fontSize: ScreenUtil().setSp(12),
color: Colors.black)),
],
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(variantList[index].description),
Text(variantList[index].description),
Text(variantList[index].description),
Text(variantList[index].description),
Text(variantList[index].description),
Text(variantList[index].description),
Text(variantList[index].description),
Text(variantList[index].description),
Text(variantList[index].description),
],
))
],
),
],
),
));
},
itemCount: variantList.length,
))
],
),
);
}
}
I am implementing listview in my flutter application and whenever i add items in variantsList the new added item is reflecting in all the index of a list.
for example i am adding A
variantsList[0]='A'
Now i am adding another value B
The result which i am getting is
variantsList[0]='B',
variantsList[1]='B',
Expecting result
variantsList[0]='A',
variantsList[1]='B',
Thanks in advance
According to my understanding, you mean deleting similar list data?
you have two way:-
check data befor add to list if found use :-
if(!variantsList.contains('B'))
{//not found add data to list
variantsList.add('B');
}
2.Use end result toSet and then toList
variantsList = variantsList.toSet().toList();
Hope hlep you.

Flutter how can i map a list and pass it between screens

I am stuck at passing my data list through pages. I have a list of data which i am showing by ListViewbuilder and i need to pass the data of a list in next page. But its showing error of <String, dynamic> is not a subtype of 'List<dynamic> I know error is because the second page expect a list or something but i am passing a list so why its showing ? or i need to map or something ?
My code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class OrderDetails extends StatefulWidget {
final List text;
// receive data from the FirstScreen as a parameter
OrderDetails({Key key, #required this.text}) : super(key: key);
#override
_OrderDetailsState createState() => _OrderDetailsState();
}
class _OrderDetailsState extends State<OrderDetails> {
#override
Widget build(BuildContext context) {
print(widget.text);
double stackWidth = MediaQuery.of(context).size.width;
double stackHeight = MediaQuery.of(context).size.height;
return Scaffold(
appBar: AppBar( centerTitle: true,backgroundColor: Colors.white, leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.of(context).pop(),
), title: Text('Order#314313', style: TextStyle(color: Colors.black),),),
body: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Custom Information', style: TextStyle(fontFamily: 'SFPROBOLD', fontWeight: FontWeight.bold, fontSize: 15),),
Text('Delivery', style: TextStyle(fontFamily: 'SFPROBOLD', fontSize: 14, color: Color(0xffea6c7b), fontWeight: FontWeight.bold),)
],
),
),
Container(
width: double.infinity,
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Mohammad Bilal', style: TextStyle(fontFamily: 'SFPROBOLD', fontWeight: FontWeight.bold, fontSize: 17),),
SizedBox(height: stackHeight * 0.004,),
Text('+966 59 11111', style: TextStyle( fontSize: 14, color: Colors.grey),),
SizedBox(height: stackHeight * 0.01,),
Container(
height: stackHeight * 0.001,
color: Color(0xffeaeaea),
),
SizedBox(height: stackHeight * 0.01,),
Text('Home | Eastern Ring Road', style: TextStyle(fontFamily: 'SFPROBOLD', fontWeight: FontWeight.bold, fontSize: 17),),
SizedBox(height: stackHeight * 0.004,),
Text('6, Building 9, Second floor, Lulu', style: TextStyle( fontSize: 14, color: Colors.grey),),
],
),
),
),
Padding(
padding: const EdgeInsets.all(15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Items Ordered', style: TextStyle( fontSize: 15),),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: stackWidth * 0.1,
decoration: BoxDecoration(
color: Color(0xff008ac7),
borderRadius: BorderRadius.all(Radius.circular(5))
),
child: Padding(
padding: const EdgeInsets.all(4.0),
child: Center(child: Text('PAID', style: TextStyle(color: Colors.white, fontSize: 9),)),
),
),
) ],
),
),
Container(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Text('2x', style: TextStyle(color: Color(0xff17b39e), fontSize: 15, fontWeight: FontWeight.bold),),
SizedBox(width: stackWidth * 0.04,),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Chicken biryani', style: TextStyle(fontFamily: 'SFPROBOLD', fontWeight: FontWeight.bold, fontSize: 17),),
Text('Leg peice', style: TextStyle( fontSize: 14, color: Colors.grey),),
],
),
],
),
Text('450 SAR', style: TextStyle(fontFamily: 'SFPROBOLD', fontWeight: FontWeight.bold, fontSize: 17),),
],
),
),
),
Container(
height: stackHeight * 0.001,
color: Color(0xffeaeaea),
),
Container(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Text('2x', style: TextStyle(color: Color(0xff17b39e), fontSize: 15, fontWeight: FontWeight.bold),),
SizedBox(width: stackWidth * 0.04,),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Chicken karahi', style: TextStyle(fontFamily: 'SFPROBOLD', fontWeight: FontWeight.bold, fontSize: 17),),
Text('Extra spicy', style: TextStyle( fontSize: 14, color: Colors.grey),),
],
),
],
),
Text('450 SAR', style: TextStyle(fontFamily: 'SFPROBOLD', fontWeight: FontWeight.bold, fontSize: 17),),
],
),
),
),
Container(
height: stackHeight * 0.001,
color: Color(0xffeaeaea),
),
Padding(
padding: const EdgeInsets.all(15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Total (incl, Tax)', style: TextStyle(fontSize: 15,),),
Text('65 SR', style: TextStyle(fontFamily: 'SFPROBOLD', fontSize: 15, fontWeight: FontWeight.bold),)
],
),
),
Padding(
padding: const EdgeInsets.all(15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly ,
children: <Widget>[
Container(
width: stackWidth * 0.35,
height: stackHeight * 0.07,
decoration: BoxDecoration(
color: Color(0xff17b39e),
borderRadius: BorderRadius.all(Radius.circular(5))
),
child: Center(child: Text('Accept', style: TextStyle(fontSize: 18, color: Colors.white, fontWeight: FontWeight.bold),))
),
Container(
width: stackWidth * 0.35,
height: stackHeight * 0.07,
decoration: BoxDecoration(
color: Color(0xfffe6c6c),
borderRadius: BorderRadius.all(Radius.circular(5))
),
child: Center(child: Text('Cancel', style: TextStyle(fontSize: 18, color: Colors.white, fontWeight: FontWeight.bold),))
)
],
),
),
Padding(
padding: const EdgeInsets.all(15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly ,
children: <Widget>[
Container(
width: stackWidth * 0.35,
height: stackHeight * 0.07,
decoration: BoxDecoration(
color: Color(0xff7d5f93),
borderRadius: BorderRadius.all(Radius.circular(5))
),
child: Center(child: Text('Ready', style: TextStyle(fontSize: 18, color: Colors.white, fontWeight: FontWeight.bold),))
),
Container(
width: stackWidth * 0.35,
height: stackHeight * 0.07,
decoration: BoxDecoration(
color: Color(0xfffe6c6c),
borderRadius: BorderRadius.all(Radius.circular(5))
),
child: Center(child: Text('Cancel', style: TextStyle(fontSize: 18, color: Colors.white, fontWeight: FontWeight.bold),))
)
],
),
)
],
),
);
}
}
Second page code where i am trying to get data
class OrderDetails extends StatefulWidget {
final List text;
// receive data from the FirstScreen as a parameter
OrderDetails({Key key, #required this.text}) : super(key: key);
#override
_OrderDetailsState createState() => _OrderDetailsState();
}
I need help to print the list in second page.
You should set the generic type of the List in the receiving widget.
class OrderDetails extends StatefulWidget {
final List<String, dynamic> text;
// receive data from the FirstScreen as a parameter
OrderDetails({Key key, #required this.text}) : super(key: key);
#override
_OrderDetailsState createState() => _OrderDetailsState();
}

Change color of button on press and revert back to original color when press released flutter

I have two radio buttons in a row. I want to change the colour of the first button when it is pressed to something else and if the second button is pressed the first button should go back to the default colour and the second button should go to the new colour. I am doing this by writing the following code. But both buttons are changing colour together. Both are becoming green on press even if I press a single button.
Button 1:
bool isButtonPressed = false;
Widget startupButton() {
return ClipRRect(
borderRadius: BorderRadius.circular(15.0),
child: SizedBox(
height: 150,
width: MediaQuery
.of(context)
.size
.width * 0.45,
child: RaisedButton(
color: isButtonPressed ? Colors.green : Colors.red,
onPressed: () {
setState(() {
isButtonPressed =!isButtonPressed;
});
},
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Text('Startup Pack',
style: TextStyle(fontSize: 15, color: Colors.black)),
),
Text('Introductory Offer',
style: TextStyle(fontSize: 12, color: Colors.black)),
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"\$$dollars",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
decoration: TextDecoration.lineThrough),
),
SizedBox(width: 5),
Text(
"\$$dollars2",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: primaryColor),
),
]),
),
Text(
"Per year",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 13,
color: blackColor),
),
Text(
"Billed anually",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 13,
color: blackColor),
),
],
),
),
),
);
}
Button 2:
bool isButtonPressed = false;
Widget freeButton() {
return
ClipRRect(
borderRadius: BorderRadius.circular(15.0),
child: SizedBox(
height: 150,
width: MediaQuery.of(context).size.width * 0.45,
child: RaisedButton(
color: isButtonPressed ? Colors.green : Colors.red,
onPressed: () {
setState(() {
isButtonPressed =!isButtonPressed;
});
},
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Text('Stunning Solo', style: TextStyle(fontSize: 15,color: Colors.black)),
),
Text('Free Forever', style: TextStyle(fontSize: 12,color: Colors.black)),
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"\$$dollars3",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: blackColor),
),
SizedBox(width: 5),
]),
),
Text(
"Free forever",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 13,
color: blackColor),
),
],
),
),
),
);
}
You can use this code.
import 'package:flutter/material.dart';
bool button1 = false;
bool button2 = false;
class Page0 extends StatefulWidget {
#override
_Page0State createState() => _Page0State();
}
class _Page0State extends State {
#override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
startupButton(context, setState),
freeButton(context, setState)
],
);
}
}
Widget startupButton(context, setState) {
return ClipRRect(
borderRadius: BorderRadius.circular(15.0),
child: SizedBox(
height: 150,
width: MediaQuery.of(context).size.width * 0.45,
child: RaisedButton(
color: button1 ? Colors.green : Colors.red,
onPressed: () {
setState(() {
button1 = true;
button2 = false;
});
},
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Text('Startup Pack',
style: TextStyle(
fontSize: 15,
color: button1 ? Colors.white : Colors.black)),
),
Text('Introductory Offer',
style: TextStyle(
fontSize: 12,
color: button1 ? Colors.white : Colors.black)),
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"\$dollars",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
decoration: TextDecoration.lineThrough,
color: button1 ? Colors.white : Colors.black),
),
SizedBox(width: 5),
Text(
"\$dollars2",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: button1 ? Colors.white : Colors.black),
),
]),
),
Text(
"Per year",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 13,
color: button1 ? Colors.white : Colors.black),
),
Text(
"Billed anually",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 13,
color: button1 ? Colors.white : Colors.black),
),
],
),
),
),
);
}
Widget freeButton(context, setState) {
return ClipRRect(
borderRadius: BorderRadius.circular(15.0),
child: SizedBox(
height: 150,
width: MediaQuery.of(context).size.width * 0.45,
child: RaisedButton(
color: button2 ? Colors.green : Colors.red,
onPressed: () {
setState(() {
button1 = false;
button2 = true;
});
},
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Text('Stunning Solo',
style: TextStyle(
fontSize: 15,
color: button2 ? Colors.white : Colors.black)),
),
Text('Free Forever',
style: TextStyle(
fontSize: 12,
color: button2 ? Colors.white : Colors.black)),
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"\$dollars3",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: button2 ? Colors.white : Colors.black),
),
SizedBox(width: 5),
]),
),
Text(
"Free forever",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 13,
color: button2 ? Colors.white : Colors.black),
),
],
),
),
),
);
}

flutter Error when using color swatch inside PopupMenuItem

I have the following code:
class _StandardCardState extends State<StandardCard> {
#override
Widget build(BuildContext context) {
return Container(
child: Card(
margin: EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0.0),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
children: <Widget>[
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
children: <Widget>[
Text(
'L*: ',
style: TextStyle(
fontSize: 18.0, color: Colors.grey[600]),
),
Text(
widget.standard.L.toString(),
style: TextStyle(
fontSize: 18.0, color: Colors.grey[600]),
),
],
),
SizedBox(height: 6.0),
Row(
children: <Widget>[
//some children
],
),
SizedBox(height: 6.0),
Row(
children: <Widget>[
//some children
],
),
],
),
),
PopupMenuButton<WhyFarther>(
onSelected: (WhyFarther result) {
setState(() {
var _selection = result;
});
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<WhyFarther>>[
const PopupMenuItem<WhyFarther>(
value: WhyFarther.harder,
child: Text(
'Working a lot harder',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.smarter,
child: Text(
'Being a lot smarter',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.selfStarter,
child: Text(
'Being a self-starter',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.tradingCharter,
child: Text(
'Placed in charge of trading charter',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
],
)
],
),
),
),
);
}
}
which is then still called through another function by a ListView.builder. when I try to use Swatches to change the Text Style inside a popupMenuItem, I get the error ´'Invalid constant value'´.
Why does this throw an error while the text widget above doesn't? And how can I avoid that and how do I use swatches inside PopupMenuButtons then?
i only had to remove the ´const´ before the PopupMenuItem... Sometimes reading when copycoding helps a lot.