I have a raised button in Flutter that is overflown - flutter

I have a card and a container. Inside the card, I have a container and the conatiner has rows and columns. On the last column, I have a button that is overflown from the card.
Please check the image, I have too much logic in the code, that is why I am unable to share my complete code. That will be too hard for anyone to understand.
Here is my code:
Spacer(),
Container(
child: RaisedButton(
color: MyColors.primaryColor,
child: subonoff(subcatextraproduct[index]) && visbutton ? Text(" Get Once ",style: TextStyle(color: Colors.white,fontSize: 10)) : Text("+ Add to Cart",style: TextStyle(color: Colors.white,fontSize: 10)),
onPressed: (){
subtotal=subtotal+subcatextraproduct[index].price.round();
grandtotal=subtotal+deliverycharges;
setState(() {
//change="Once";
// print("sum setstate");
// sum++;
myfunc();
//this.widget.callback(sum);
});
makefalsbutton(subcatextraproduct[index].id);
maketruecountr(subcatextraproduct[index].id);
totalcsetone(subcatextraproduct[index].id);
// print("i am in add");
},
),
),

You should not use Container without any attributes but child and DO NOT use functions to make widgets
As for your question – try to wrap your button into Expanded
I'd recommend you to read this article https://flutter.dev/docs/development/ui/layout/constraints
Try this code:
class ProductRow extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Row(
children: [
Expanded(
child: Image.network(
'https://i.stack.imgur.com/vYOjL.png',
fit: BoxFit.contain,
),
),
Expanded(
child: Column(
children: [
Text('Yougurt'),
Text('1 kg'),
Text('9% OFF'),
Text('RS 110.0'),
],
),
),
RaisedButton.icon(
icon: const Icon(Icons.add),
label: const Text('Add to cart'),
onPressed: () {},
)
],
),
);
}
}

2 suggestions:
Remove container
If you can’t remove container, replace it with Flexible widget. Set fit: FlexFit.loose

Try
RaisedButton(
onPressed: ...,
child: FittedBox(
child: Text(
condition ? "Get Once" : "Add to cart",
style: TextStyle(color: Colors.white, fontSize: 10),
),
),
)
Or
[
Spacer(),
Flexible(child: RaisedButton()),
...
]

Related

How to Locate the Text() center when using expanded(flex:)?

Please see my code and scree shot below. I want locate Text("to be center") at center horizontally, but comparatively located right side as showen below.
I understand i'ts because I'm using expanded widget and set flex 1 & 4 and Text("to be center") are inclueded in latter block.
How can I located totally or relatively center but using expandede & flex property like this case ?
Also I understand I can adjust it by wrapping Text() with padding and set EdgeInsets.only(right:X) but I can not figure out if it is totally cenerized or not...
Row(
children: [
Expanded(
flex:1,
child:TextButton(
style: TextButton.styleFrom(
alignment: Alignment.centerLeft,
),
child: const Icon(
Icons.close,
color: MyStyle.textColor,
),
onPressed: () {
Navigator.of(context).pop();
},
),
),
Expanded(
flex:4,
child: Container(
alignment: Alignment.center,
decoration: const BoxDecoration(
),
child: Text("to be center",style: MyText(myFontSize: 15).style()),
)
),
],
),
Just use Spacer at the end inside Row
Row(
children: [
Expanded(
flex: 1,
child: TextButton(
style: TextButton.styleFrom(
alignment: Alignment.centerLeft,
),
child: const Icon(
Icons.close,
color: MyStyle.textColor,
),
onPressed: () {
Navigator.of(context).pop();
},
),
),
Expanded(
flex: 4,
child: Container(
alignment: Alignment.center,
decoration: const BoxDecoration(),
child: Text(
"to be center",
style: MyText(myFontSize: 15).style(),
),
),
),
// like this
const Spacer(),
],
),
Learn more about the Spacer widget
Try below code: refer layout
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: TextButton(
style: TextButton.styleFrom(
alignment: Alignment.centerLeft,
),
child: const Icon(
Icons.close,
),
onPressed: () {
Navigator.of(context).pop();
},
),
),
const Expanded(
child: Center(
child: Text(
'to be center',
),
),
),
const Expanded(child: SizedBox())
],
),
Result->
The simple solutions could be:
Use textAlign attribute of Text widget or
Wrap Text widget with Center widget inside Expanded widget or
You can even explore the widget IntrinsicWidth with Center widget.

Elevated button not working as widget of child in body clause

I am trying to have an ElevatedButton widget in this stateless widget. The hierarchy of the brackets is ok but it is not recognizing the ElevatedButton widget being a parameter of child
class buildProfileView extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.min,
children: [
Center(
child: CircleAvatar(
backgroundImage:
Image.network(controller.googleAccount.value?.photoUrl ?? '')
.image,
radius: 100,
),
),
Text(
controller.googleAccount.value?.displayName ?? '',
style: Get.textTheme.headline1,
),
Text(controller.googleAccount.value?.email ?? '',
style: Get.textTheme.bodyText1),
//),
],
child: ElevatedButton(
onPressed: () {},
child: Text('Continue to Main Menu'),
backgroundColor: Colors.blue,
),
),
);
}
}
The issue lies on code-strucure
//),
],
child: ElevatedButton(
onPressed: () {},
where ElevatedButton is out of the column scope.
It will be
body: Column(mainAxisSize: MainAxisSize.min, children: [
Center(
child: CircleAvatar(
backgroundImage:
Image.network(controller.googleAccount.value?.photoUrl ?? '')
.image,
radius: 100,
),
),
Text(
controller.googleAccount.value?.displayName ?? '',
style: Get.textTheme.headline1,
),
Text(controller.googleAccount.value?.email ?? '',
style: Get.textTheme.bodyText1),
ElevatedButton(
onPressed: () {},
child: Text('Continue to Main Menu'),
),
]),
More about Column widget.
Yeah you can't assign both child and children property to a Column. Column takes children so move ElevatedButton to children block inside [](square brackets).

Adding multiple rows or column in body

Hi trying to figure out why my hamburger is getting misaligned when i add another Expanded() code within the body.
I tried to follow this url to develop a hamburger menu and responsive for all platforms at once.
Flutter Blog
Below is the code that i tried where i added Expanded() below the Row()
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
#override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
return Scaffold(
key: _scaffoldKey,
drawer: AppDrawer(),
body: Row(
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.all(16),
child: IconButton(
icon: Icon(Icons.menu, size: 30),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
}),
),
],
),
Expanded(
child: Container(
width: width,
child: Column(
children: [
Expanded(
child: Lottie.network(
'https://assets2.lottiefiles.com/datafiles/6WfDdm3ooQTEs1L/data.json'),
),
Expanded(
child: Column(
children: [
Flexible(
child: Text(
"Multivariate Testing",
style: GoogleFonts.montserrat(
fontSize: 50,
),
),
),
SizedBox(
height: 10,
),
Flexible(
child: Text(
'Run experiments to make key flows more effective.',
style: GoogleFonts.montserrat(
fontSize: 25,
),
),
),
SizedBox(
height: 35,
),
// ignore: deprecated_member_use
TextButton(
onPressed: () {},
child: Text(
'Create Experiment',
style: GoogleFonts.montserrat(
fontSize: 20,
),
),
)
],
),
),
],
),
),
)
],
),
);
}
}
and the output that i am seeing from mobile view. Appreciate any help. Sorry for too big screenshot!
This is because of the default functionality of Row.
So change your first child for your top level Row to this,
Scaffold(
key: _scaffoldKey,
drawer: AppDrawer(),
body: Row(
children: [
Align(
alignment: Alignment.topCenter,
child: Padding(
padding: const EdgeInsets.all(16),
child: IconButton(
icon: Icon(Icons.menu, size: 30),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
})),
),
Expanded( ...... rest of your code
Here, we are using the Align widget with alignment: Alignment.topCenter.
Align widget takes the entire available space in the parent and then depending on the alignment property, aligns it't child inside it.
So, saying Alignment.topCenter will place it at the top vertically and middle horizontally.
Using an AppBar will unnecessarily complicate your situation since now, the AppBar is going to take up space a the top, but in your case, you want your Lottie view to be visible at the top, so it will overlap.
Because Expanded fills up the whole space and that's why your hamburger is getting misaligned.
Why don't you try using appbar for the hamburger?

arranging a scaffold with buttons

i wrote this code in flutter and i wanna know if i can make it better,
i did a lot of "work around" that i feel there's a better way to write it.
especially the buttons with the expanded between them, and the sizedbox.
the screen has text in the middle(getVerse), and and two buttons, one is bottom left and the other is bottom right.
the first expanded separate the text from the buttons, and the second expanded is to separate the two buttons
help is appreciated and thanks for your time.
Scaffold(
appBar: AppBar(
title: Text(
suras.elementAt(widget.index),
textAlign: TextAlign.right,
),
),
body: Column(
children: [
SizedBox(
height: 100,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
getVerse(widget.index + 1, currentVerse),
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 24,
fontFamily: 'KFGQPC BAZZI Uthmanic Script',
),
),
),
Expanded(child: Container()),
Row(
children: [
ElevatedButton(
onPressed: () {
setState(() {
currentVerse++;
});
},
child: Text('not sure'),
),
Expanded(child: Container()),
ElevatedButton(
onPressed: null,
child: Text('sure'),
),
],
),
],
),
);
If you want that those 2 buttons to stay at the bottom you can use floatingActionButton in the Scaffold property. You can change the location of those buttons with the floatingActionButtonLocation.
Scaffold(
appBar: AppBar(
title: Text('MyApp'),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () {},
child: Text('Not sure'),
),
ElevatedButton(
onPressed: () {},
child: Text('Sure'),
)
],
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'My Text',
textAlign: TextAlign.center,
),
),
),

renderflex over flow by 7px on the right in flutter app

Please help me with this error in the flutter app. I did not know what i am during wrong this is my code below: This is the code below.Pls help me
import 'package:flutter/material.dart';
class CartProducts extends StatefulWidget {
#override
_CartProductsState createState() => _CartProductsState();
}
class _CartProductsState extends State<CartProducts> {
var productsOnTheCart=[
{
"name":"Blazer",
"picture":"images/products/blazer1.jpeg",
"price":85,
"size":"M",
"color":"Black",
"quantity":1,
},
{
"name":"M Pant",
"picture":"images/products/pants2.jpeg",
"price": 80,
"size":"8",
"color":"Black",
"quantity":1,
},
{
"name":"Red Dress",
"picture":"images/products/dress1.jpeg",
"price":50,
"size":"7",
"color":"Red",
"quantity":2,
}
];
#override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: productsOnTheCart.length,
itemBuilder: (context, index){
return new SingleCartProduct(
cartProdName:productsOnTheCart[index]['name'],
cartProdColor:productsOnTheCart[index]['color'],
cartProdQty:productsOnTheCart[index]['quantity'],
cartProdSize:productsOnTheCart[index]['size'],
cartProdPrice:productsOnTheCart[index]['price'],
cartProdPicture:productsOnTheCart[index]['picture'],
);
}
);
}
}
class SingleCartProduct extends StatelessWidget {
final cartProdName;
final cartProdPicture;
final cartProdPrice;
final cartProdSize;
final cartProdColor;
final cartProdQty;
SingleCartProduct({
this.cartProdName,
this.cartProdPicture,
this.cartProdPrice,
this.cartProdSize,
this.cartProdColor,
this.cartProdQty});
#override
Widget build(BuildContext context) {
return Card(
child: ListTile(
//leading section image section here
leading: new Image.asset(cartProdPicture, height:80.0,width: 80.0,),
//title section here
title: new Text(cartProdName),
//subtitle section
subtitle: new Column(
children: <Widget>[
//Row Inside Column
new Row(
children: <Widget>[
//this section is for the size of the products
new Padding(padding:const EdgeInsets.all(4.0),
child: new Text("Size:"),
),
new Padding(padding: const EdgeInsets.all(4.0),
child: new Text(cartProdSize, style: TextStyle(color: Colors.red),),
),
//This Section is for Prodcut Color
new Padding(padding:const EdgeInsets.fromLTRB(20.0, 8.0, 8.0, 8.0),
child: new Text("Color:"),),
new Padding(padding:const EdgeInsets.all(4.0),
child: new Text(cartProdColor, style: TextStyle(color: Colors.red),),
),
],
),
new Container(
alignment: Alignment.topLeft,
child: new Text("\$${cartProdPrice}",
style: TextStyle(
fontSize: 17.0, fontWeight: FontWeight.bold, color: Colors.red
),
),
)
//This Section is for the product price
],
),
/* trailing:new Column(
children: <Widget>[
new IconButton(
icon: Icon(
Icons.arrow_drop_up,color: Colors.red),
iconSize: 38,onPressed: () {}),
new IconButton(
icon: Icon(
Icons.arrow_drop_down,color: Colors.red,),
iconSize: 38, onPressed: () {}),
],
)*/
trailing: FittedBox(
fit: BoxFit.fill,
child: Column(
children: <Widget>[
new IconButton(icon: Icon(Icons.arrow_drop_up), onPressed: (){}),
new Text("$cartProdQty", style: TextStyle(fontWeight: FontWeight.bold),),
new IconButton(icon: Icon(Icons.arrow_drop_down), onPressed: (){}),
],
),
),
),
);
}
}
If I remember currectly, applying shrinkWrap: true in the ListView.Builder
is supposed to force the views to fit inside it.
My guess is that the views inside the listview cause the overflow..
Edit:
The overflow could be caused by the column widget inside the single cart widget.
Maybe switching it to a Listview.builder instead may help?
I am guessing overflow is happening in the row widget.
Here is a fix. Use Flexible widget or Expanded Widget based on the requirement
Padding(
padding: const EdgeInsets.symmetric(horizontal: 22.0, vertical: 8.0),
child: Row(
children: <Widget>[
Flexible(
fit: FlexFit.loose, // It means that the child widget can expand to maximum size available thus taking up all space else it can shrink if there is no space available // Refer to the link for more info
child: Text(
'This is my first label:',
style: Theme.of(context).textTheme.subtitle,
),
),
SizedBox(width: 5,), // You can simply put some sized boxes like this to have some fixed space instead of wrapping every child in a padding widget
Flexible(
fit: FlexFit.loose,
child: Text(
'This is my first value',
style: Theme.of(context).textTheme.body1,
),
),
Flexible(
fit: FlexFit.loose,
child: Text(
'This is my second label:',
style: Theme.of(context).textTheme.subtitle,
),
),
SizedBox(width: 5,),
Flexible(
fit: FlexFit.loose,
child: Text(
'This is my second value',
style: Theme.of(context).textTheme.body1,
),
),
],
),
),
Okay so here is the result.
See how texts take multiple lines when enough space is not available. You can avoid overflow in smaller screens this way.
Hope this solves your problem :D