only static member be accessed in initializers in flutter - flutter

I want to try to define below code but when I write it, show me the Error only static member be accessed in initializers, the problem is I can't define it anywhere, the code that I want to define is here, I need to use to calculate discount
double discount=(prod_old_priceprod_old_price-prod_price)/prod_old_price*100;
and this my project code:
class ProductDetails extends StatefulWidget {
final prod_fullName;
final prod_pic;
final prod_old_price;
final prod_price;
ProductDetails({
this.prod_fullName,
this.prod_pic,
this.prod_old_price,
this.prod_price,
});
children: <Widget>[
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.white,
textColor: Color(0xff989898),
elevation: 0.2,
child: Row(
children: <Widget>[
Expanded(child: new Text('Color')),
Expanded(child: new Icon(Icons.arrow_drop_down)),
],
),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.white,
textColor: Color(0xff989898),
elevation: 0.2,
child: Row(
children: <Widget>[
Expanded(child: new Text('Size')),
Expanded(child: new Icon(Icons.arrow_drop_down)),
],
),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.white,
textColor: Color(0xff989898),
elevation: 0.2,
child: Row(
children: <Widget>[
Expanded(child: new Text('Qty')),
Expanded(child: new Icon(Icons.arrow_drop_down)),
],
),
),
),
],
)
],
),
bottomNavigationBar: Material(
elevation: 7.0,
color: Colors.white,
child: Container(
height: 60.0,
width: MediaQuery.of(context).size.width,
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 10.0),
child: InkWell(
onTap: (){},
child: Container(
height: 40.0,
width: MediaQuery.of(context).size.width - 280.0,
decoration: BoxDecoration(
color: Color(0xffff5f60),
borderRadius: BorderRadius.circular(10),
),
child: Center(
child: Text(
'add to cart',
style: TextStyle(color: Colors.white,fontSize: 20.0,fontWeight: FontWeight.bold),
),
),
),
),
),
Container(
decoration: BoxDecoration(
color: Color(0xfff40725),
borderRadius: BorderRadius.circular(50),
),
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Text("----------------I WANT USE DISCOUNT HERE --------------------------",style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),),
),
),
Padding(
padding: const EdgeInsets.only(top:5.0,left:8.0),
child: Container(
child: Column(
children: <Widget>[
Text("\$${widget.prod_old_price}",textAlign: TextAlign.left,style: TextStyle(fontSize: 18.0,color: Color(0xff989898),decoration: TextDecoration.lineThrough),),
Text("\$${widget.prod_price}",style: TextStyle(fontSize: 18.0,fontWeight: FontWeight.bold)),
],
),
),
),
],
),
),
),
);

Related

Favourite list button on product

i want to make an this function on the product but i don't know how to since I'm too fresh with flutter...
so here is the picture of how I want it to be
The Sample
and here is my codes.
child: GridTile(
footer: Container(
color: Colors.white,
child: Row(
children: [
Expanded(
flex: 8,
child: Text(product_name),
),
Text("\$$product_price",
style: const TextStyle(fontWeight: FontWeight.w600)),
TextButton(
onPressed: (){
debugPrint('added to cart');
},
child: Text("ADD TO CARD"),
style: TextButton.styleFrom(
primary: Colors.lightBlue[800],
shadowColor: Colors.deepPurple,
// elevation: 5,
),
),
],
),
),
child: Image.asset(
product_picture,
fit: BoxFit.cover,
),
),
),
),
),
and it is how it looks
How it looks
I will appreciate any help!
Here is something similar to the sample
GridView.count(
shrinkWrap: true,
childAspectRatio: .8,
crossAxisCount: 2,
mainAxisSpacing: 4,
crossAxisSpacing: 4,
children: List.generate(10, (index) {
return Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
),
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(5),
child: Image.network('https://i.ytimg.com/vi/6OovMIjDtAM/maxresdefault.jpg',)
),
const SizedBox(height: 14),
const Text('My Drink 101',
style: TextStyle(color: Colors.black, fontSize: 16, fontWeight: FontWeight.w600),
),
const SizedBox(height: 14),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('500 ml', style: TextStyle(color: Colors.black, fontSize: 14, fontWeight: FontWeight.w300)),
Text('\$500 ml', style: TextStyle(color: Colors.black, fontSize: 14, fontWeight: FontWeight.w300)),
],
),
),
const SizedBox(height: 16),
Container(
alignment: Alignment.centerRight,
child: InkWell(
onTap: () {
// add to cart
},
child: Icon(Icons.shopping_cart_outlined, color: Colors.blue,),
),
),
],
),
Align(
alignment: Alignment.topLeft,
child: InkWell(
onTap: () {
// add to favourite
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Icon(
Icons.favorite_border,
color: Colors.white,
),
),
),
),
],
),
);
}),
)
Well, there is a bunch needed to make a card similar to the sample you provided. Take a look at this video to get some ideas. I would recommend you to read the flutter widgets documentation for a better understanding of how each property work.
This channel also has a lot of great flutter UI content that is worthy to have a look at.
To start I would consider using a Card instead of a Container

How to change the color of the cardview on button click in flutter?

I have a few buttons and I need to set the colour of the card view as per the button click.
Basically, I'm adding a theme to my post, so on the preview page, I need to allow the user to select the colour of their choice by clicking the button.
Screenshot:
CardView Page with Buttons
Button Code:
Padding(
padding: const EdgeInsets.only(top: 10),
child: Row(
children: [
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.yellow,
shape: CircleBorder(),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.orange,
shape: CircleBorder(),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.brown,
shape: CircleBorder(),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.blue,
shape: CircleBorder(),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.green,
shape: CircleBorder(),
),
),
Expanded(
child: MaterialButton(
onPressed: () {},
color: Colors.black,
shape: CircleBorder(),
),
),
],
),
),
Cardview Code:
Card(
child: Container(
decoration: BoxDecoration(
color: Colors.blue,
border: Border.all(
color: MyColors.black, width: 1.5),
borderRadius: BorderRadius.circular(10)),
child: Column(
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
backgroundImage:
CachedNetworkImageProvider(
_profileurl),
),
),
Text(_username),
],
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Obx(
() => Text(
pollDataController1.question.value,
style: TextType.boldHeading,
),
),
),
Obx(
() => ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.asset(
(pollImageController.imageDisplay.value),
width: 320,
height: 170,
fit: BoxFit.cover,
),
),
),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10.0),
child: Column(
children: [
Column(
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Obx(
() => Text(
pollDataController1
.op1.value +
" ",
style: TextStyle(
color: MyColors.offBlack,
fontSize: 16.0,
),
),
),
Obx(
() => Text(pollDataController1
.op1Emoji.value),
),
SizedBox(
width: 25.0,
),
Obx(
() => Text(
pollDataController1
.op2.value +
" ",
style: TextStyle(
color: MyColors.offBlack,
fontSize: 16.0,
),
),
),
Obx(
() => Text(pollDataController1
.op2Emoji.value),
),
],
),
],
),
SizedBox(height: 13.0),
Column(
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Obx(
() => Text(
pollDataController1
.op3.value +
" ",
style: TextStyle(
color: MyColors.offBlack,
fontSize: 16.0,
),
),
),
Obx(
() => Text(pollDataController1
.op3Emoji.value),
),
SizedBox(
width: 25.0,
),
Obx(
() => Text(
pollDataController1
.op4.value +
" ",
style: TextStyle(
color: MyColors.offBlack,
fontSize: 16.0,
),
),
),
Obx(
() => Text(pollDataController1
.op4Emoji.value),
),
],
),
],
),
],
),
),
],
),
),
),
You can use a global variable for color and a function to change the color on tapping each circle button.
So an example would be:
Color cardBackgroundColor = Colors.white;
void changeColor(Color changeToColor) {
setState(() {
cardBackgroundColor = changeToColor;
});
}
then in your button code use it like this:
Expanded(
child: MaterialButton(
onPressed: changeColor(Colors.yellow),
color: Colors.yellow,
shape: CircleBorder(),
),
),
and in the Cardview Code change it to:
Card(child: Container(
decoration: BoxDecoration(
color: cardBackgroundColor,
border: Border.all(
color: MyColors.black, width: 1.5),
borderRadius: BorderRadius.circular(10)),
This would be the quickest way to do it, although it might not be the cleanest.

How do I add a splash effect on a card using GestureDetector

I wan't to add splash effect over the card when tapped on it.
I referred some of the answers on stackoverflow but, nothing caught up in my brai. HELP!!
My Code -
body: Container(
padding: EdgeInsets.all(10),
child: ListView(
children: <Widget>[
Container(
height: 100,
child: GestureDetector(
onTap: () {
Navigator.pushNamed(context, '/page1');
},
child: GradientCard(
gradient: LinearGradient(
colors: [Colors.yellow, Colors.lightGreenAccent]),
elevation: 10,
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 10, horizontal: 8),
child: Center(
child: Row(
children: <Widget>[
SizedBox(width: 25),
Icon(
Icons.favorite,
color: Colors.redAccent,
size: 50,
),
SizedBox(width: 40),
Text(
"MY CARD",
style: TextStyle(fontSize: 28),
),
],
),
),
),
),
),
),
],
),
),
I also tried to wrap onTap: function with InkWell with an ancestor as Material, But it won't let me to do so. Actually, I am confused now and stucked. Any help will be apperiated!!
Try this and let me know if that works!
Container(
padding: EdgeInsets.all(10),
child: ListView(
children: <Widget>[
Container(
height: 100,
child: GradientCard(
gradient: LinearGradient(
colors: [Colors.yellow, Colors.lightGreenAccent]
),
elevation: 10,
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
Navigator.pushNamed(context, '/page1');
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 8),
child: Center(
child: Row(
children: <Widget>[
SizedBox(width: 25),
Icon(
Icons.favorite,
color: Colors.redAccent,
size: 50,
),
SizedBox(width: 40),
Text(
"MY CARD",
style: TextStyle(fontSize: 28),
),
],
),
),
),
)
)
)
),
],
),
);
Try this:
Container(
height: 100,
child: Material(
elevation: 10,
color: Theme.of(context).cardColor,
child: InkWell(
onTap: () {
Navigator.pushNamed(context, '/page1');
},
child: Center(
child: Row(
children: <Widget>[
SizedBox(width: 25),
Icon(
Icons.favorite,
color: Colors.redAccent,
size: 50,
),
SizedBox(width: 40),
Text(
"MY CARD",
style: TextStyle(fontSize: 28),
),
],
),
),
),
),
)

set an widget alignment in flutter

Hello everyone i want to use a bottom navigator in product details file but I have two containers inside my row widget. I want to set my first container(which is Old Price and New Price Column) at the left, and also want to set my second container(which is Add to cart btn) at the right. how can I achieve this?
Here is code:
bottomNavigationBar: Material(
elevation: 7.0,
color: Colors.white,
child: Container(
height: 60.0,
width: MediaQuery.of(context).size.width,
color: Colors.white,
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 10.0),
child: Container(
height: 40.0,
width: MediaQuery.of(context).size.width - 280.0,
decoration: BoxDecoration(
color: Color(0xfff40725),
borderRadius: BorderRadius.circular(10),
),
child: Center(
child: Text(
'Add to cart',
style: TextStyle(color: Colors.white,fontSize: 20.0,fontWeight: FontWeight.bold),
),
),
),
),
Padding(
padding: const EdgeInsets.only(left:8.0),
child: Container(
child: Column(
children: <Widget>[
Text("\$${widget.prod_old_price}",textAlign: TextAlign.left,style: TextStyle(fontSize: 18.0,color: Color(0xff989898),decoration: TextDecoration.lineThrough),),
Text("\$${widget.prod_price}",style: TextStyle(fontSize: 18.0,fontWeight: FontWeight.bold)),
],
),
),
),
],
),
),
),
Try this :
bottomNavigationBar: Material(
elevation: 7.0,
color: Colors.white,
child: Container(
height: 60.0,
width: MediaQuery.of(context).size.width,
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 10.0),
child: Container(
height: 40.0,
width: MediaQuery.of(context).size.width - 280.0,
decoration: BoxDecoration(
color: Color(0xfff40725),
borderRadius: BorderRadius.circular(10),
),
child: Center(
child: Text(
'Add to cart',
style: TextStyle(color: Colors.white,fontSize: 20.0,fontWeight: FontWeight.bold),
),
),
),
),
Padding(
padding: const EdgeInsets.only(left:8.0),
child: Container(
child: Column(
children: <Widget>[
Text("\$${widget.prod_old_price}",textAlign: TextAlign.left,style: TextStyle(fontSize: 18.0,color: Color(0xff989898),decoration: TextDecoration.lineThrough),),
Text("\$${widget.prod_price}",style: TextStyle(fontSize: 18.0,fontWeight: FontWeight.bold)),
],
),
),
),
],
),
),
),
Set proper mainAxisAlignment in the Row. You can refer this
I would have set it to mainAxisAlignment: MainAxisAlignment.spaceAround

flutter - vertically align button

Need to help to copy the layout on image below.
2 buttons with 50% width and padding/spacing in between.
I'm thinking also of using toggle button widget on it.
see my draft code below.
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Row(
children: <Widget>[
Container(
color: Colors.green,
width: (MediaQuery.of(context).size.width) / 2.5,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
MdiIcons.account,
size: 18.0,
),
SizedBox(
width: 4.0,
),
Text(
"Client",
style: TextStyle(fontSize: 16),
)
],
)),
Container(
color: Colors.green,
width: (MediaQuery.of(context).size.width) / 2.5,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
MdiIcons.accountTie,
size: 18.0,
),
SizedBox(
width: 4.0,
),
Text(
"Host",
style: TextStyle(fontSize: 16),
)
],
)),
],
),
),
You can customize it,
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Row(
children: <Widget>[
Expanded(
child: RaisedButton(
onPressed: () {},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
elevation: 1.5,
color: Colors.white,
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Icon(Icons.developer_board),
SizedBox(height: 10),
Text("Experiences"),
],
),
),
),
SizedBox(width: 24),
Expanded(
child: RaisedButton(
onPressed: () {},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
elevation: 1.5,
color: Colors.white,
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Icon(Icons.event),
SizedBox(height: 10),
Text("Events"),
],
),
),
),
],
),
),