Flutter: Set button height to fill container - flutter

I've been trying to make my button to fill my container.
But as you can see from the picture above, the button (red) on the left clearly did not fill the entire container(green). I can solve this by adding height to MaterialButton, but IMHO this is not the best solution, because device's height might differ. Can you help me to solve this problem? Thanks.
Here is my code:
Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.green,
child: MaterialButton(
color: Colors.red,
child: Text("Button A"),
onPressed: () {},
),
),
),
Expanded(
child: MaterialButton(
color: Colors.blue,
child: Text("Button B"),
onPressed: () {},
),
),
],
),

Simply Add - materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, in MaterialButton
Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.green,
child: MaterialButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, // Add this
color: Colors.red,
child: Text("Button A"),
onPressed: () {},
),
),
),
Expanded(
child: MaterialButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, // Add this
color: Colors.blue,
child: Text("Button B"),
onPressed: () {},
),
),
],
),

You may can try
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
child: Container(
color: Colors.green,
child: SizedBox(
height: 48,
child: MaterialButton(
height: double.infinity,
color: Colors.red,
child: Text("Button A"),
onPressed: () {},
padding: EdgeInsets.all(0),
),
),
),
),
Expanded(
child: SizedBox(
height: 48,
child: MaterialButton(
height: double.infinity,
color: Colors.blue,
child: Text("Button B"),
onPressed: () {},
),
),
),
],
),

Related

Flutter How to align row widget children to left and center

I am trying to create social login buttons like below image. I want to align textbutton icon and name to left. also center the row they are in so that the icon and text align vertically when using multiple text buttons.
expected result:
My Result:
My Code for button:
TextButton(
onPressed: onTap,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Align(
alignment: Alignment.centerLeft,
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 20),
width: 30,
child: Image.asset(
icon,
fit: BoxFit.cover,
),
),
),
Align(
alignment: Alignment.centerLeft,
child: Text(
buttonName,
style: kBodyText2,
textAlign: TextAlign.left,
),
),
],
),
style: TextButton.styleFrom(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(SizeConfig.blockSizeH! * 3),
),
),
),
You should use ListTile()
instead.
Set title: "Continue with Google".
And leading: you can set the logo.
The positions will always stay the same no matter how long your title will be, you also have extra options like subtitles and trailing actions.
To make it act like a button use
onTap: (){}
You can achieve that by using the Expanded widget: Like so:
Padding(
padding: const EdgeInsets.all(8.0),
child: TextButton(
onPressed: (){},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
flex: 1,
child: Container(),),
Expanded(
flex: 2,
child: Align(
alignment: Alignment.centerLeft,
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 20),
width: 30,
child: Icon(Icons.access_alarm),
),
),
),
Expanded(
flex: 5,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
'test text test',
textAlign: TextAlign.left,
),
),
),
],
),
style: TextButton.styleFrom(
backgroundColor: Colors.grey,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
const SizedBox(height: 30),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextButton(
onPressed: (){},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(flex: 1, child: Container(),),
Expanded(
flex: 2,
child: Align(
alignment: Alignment.centerLeft,
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 20),
width: 30,
child: Icon(Icons.access_alarm),
),
),
),
Expanded(
flex: 5,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
'test text test test',
textAlign: TextAlign.left,
),
),
),
],
),
style: TextButton.styleFrom(
backgroundColor: Colors.grey,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
You can exploit the fact the these icons are the same size, to easily align them. Each of your TextButton already holds a Row, but instead of fancy layout stuff, you can just insert padding to the Icon by adding blank spaces using SizedBox, like so:
TextButton(
child: Row(
children: [
const SizedBox(width: 16), // padding in the beginning
Icon(Icons.download), // the icon, or image, or whatever
const SizedBox(width: 16), // padding in-between
Text('Button ' * i), // the text
],
),
onPressed: () {},
)
Result:
Code used to produce the above demo:
Column(
mainAxisSize: MainAxisSize.min,
children: [
for (int i = 1; i < 6; i++)
TextButton(
child: Row(
children: [
const SizedBox(width: 16),
Icon(Icons.download),
const SizedBox(width: 16),
Text('Button ' * i),
],
),
onPressed: () {},
style: TextButton.styleFrom(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
),
),
],
)

Issue with Row in Bottom App Bar that I'm not understanding

so I'm trying to implement a somewhat custom bottom navigation bar. I am trying to center my icons and after further investigation it is indeed working "correctly" in a way but the issue is that the bottom half of the bottom app bar gets completely ignored.
No matter what I do, the bottom half gets ignored. If I increase the size, it only makes it bigger at the top which is not what I want. How can I get the row to respect the entire bottom app bar?
Here's my code:
bottomNavigationBar: Padding(
padding: const EdgeInsets.only(left: 30, bottom: 33.0, right: 30.0),
child: ClipRRect(
borderRadius: BorderRadius.all(
Radius.circular(30.0),
),
child: BottomAppBar(
color: Color(0xFF222222),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
height: 60.0,
child: SvgPicture.asset(
'assets/images/home.svg',
),
),
Container(
height: 60.0,
child: SvgPicture.asset(
'assets/images/discover.svg',
),
),
Container(
height: 60.0,
child: SvgPicture.asset(
'assets/images/heart.svg',
),
),
Container(
height: 60.0,
child: SvgPicture.asset(
'assets/images/user.svg',
),
),
],
),
),
),
),
Solution was to wrap BottomAppBar with SafeArea. Shoutout to Flutter discord for help on this :)
bottomNavigationBar: SafeArea(
child: Padding(
padding: const EdgeInsets.only(left: 30.0, right: 30.0),
child: ClipRRect(
borderRadius: BorderRadius.all(
Radius.circular(30.0),
),
child: BottomAppBar(
color: Color(0xFF222222),
child: Container(
height: 88.0,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 30.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Stack(
children: [
IconButton(
onPressed: () {},
icon: SvgPicture.asset(
'assets/images/home.svg',
),
),
Positioned(
left: 23.0,
bottom: 0,
child: Image.asset('assets/images/Ellipse5.png'),
),
],
),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(
'assets/images/discover.svg',
),
),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(
'assets/images/heart.svg',
),
),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(
'assets/images/user.svg',
),
),
],
),
),
),
),
),
),
),

how to remove bottom space between bottom of the screen and bottom bar?

How to remove white space between bottom bar and bottom of the screen. I also try apply padding or margin in bottom container but its not working . please help me how to resolve this problem .
hey help me i am afraid about this error . it takes my most of the time but i cant solve it. please help me.
Its my code
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:provider/provider.dart';
import 'package:grk_001/Provider/products.dart';
class ProductDetails extends StatelessWidget {
static const String routename = 'ProductDetails';
#override
Widget build(BuildContext context) {
final devicesize = MediaQuery.of(context).size;
final Productid = ModalRoute.of(context).settings.arguments as String;
final loadedproduct = Provider.of<Products>(
context,
).findByid(Productid);
return SafeArea(
maintainBottomViewPadding: true,
child: Scaffold(
appBar: AppBar(
title: Text(loadedproduct.title == null ? '' : loadedproduct.title),
),
body: Container(
margin: EdgeInsets.zero,
padding: EdgeInsets.zero,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
width: double.infinity,
height: 300,
child: Image.network(
loadedproduct.imageUrl,
fit: BoxFit.cover,
),
),
SizedBox(
height: 15.0,
),
Text(
'₹${loadedproduct.price}',
style: TextStyle(fontSize: 30.0),
),
SizedBox(
height: 10.0,
),
Text(
loadedproduct.description,
softWrap: true,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20.0),
),
Row(
children: <Widget>[
Expanded(
child: FlatButton.icon(
onPressed: () {},
icon: Icon(
Icons.arrow_drop_down,
color: Colors.pink,
),
label: Text(
'Quantity',
style: TextStyle(color: Colors.pink),
)),
),
Expanded(
child: FlatButton.icon(
onPressed: () {},
icon: Icon(
Icons.arrow_drop_down,
color: Colors.pink,
),
label: Text(
'Color',
style: TextStyle(color: Colors.pink),
),
),
)
],
)
],
),
),
Container(
padding: EdgeInsets.zero,
margin: EdgeInsets.zero,
width: double.infinity,
decoration: BoxDecoration(),
child: Row(
children: <Widget>[
Expanded(
flex: 5,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0.0),
),
color: Colors.pink,
onPressed: () {},
child: Text('BUY NOW'),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {},
child: Container(
child: Icon(
Icons.favorite,
size: 37.0,
color: Colors.red,
),
),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {},
child: Container(
child: Icon(
Icons.shopping_cart,
size: 37.0,
color: Colors.red,
),
),
),
),
],
),
)
],
),
),
),
);
}
}
At last, found your problem.
The problem is with your 'BUY NOW' button! It occupies extra height so that it will visible to be high up from the bottom.
Since your icon's size is 37, a height greater than 37 would be preferred.
Set it for both parent Container and Button's Container.
Also, make a note of crossAxisAlignment of the Row.
Do something like this to your 'BUY NOW' button,
Container(
width: MediaQuery.of(context).size.width,
height: 40.0,
color: Colors.green,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Expanded(
flex: 5,
child: Container(
height: 40.0,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0.0),
),
color: Colors.pink,
onPressed: () {},
child: Text('BUY NOW'),
),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {},
child: Container(
child: Icon(
Icons.favorite,
size: 37.0,
color: Colors.red,
),
),
),
),
Expanded(
flex: 1,
child: InkWell(
onTap: () {},
child: Container(
child: Icon(
Icons.shopping_cart,
size: 37.0,
color: Colors.red,
),
),
),
),
],
),
)
Hope this solves your issue.

only static member be accessed in initializers in 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)),
],
),
),
),
],
),
),
),
);

UI Flutter about Column And Row

I have design like this
I tried adding 2 rows in the column and I got an error. that is, it exceeds the screen or the chart may not be rendered
this is my code
body: RefreshIndicator(
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
child: GestureDetector(
child: Container(
margin: EdgeInsets.only(bottom: 320),
child: chart,
),
onTap: (){
_showDialog();
},
),
),
Expanded(
child: Column(
children: <Widget>[
RaisedButton(
padding: const EdgeInsets.symmetric(vertical: 50, horizontal: 65),
textColor: Colors.white,
color: Colors.blue,
onPressed: (){},
child: new Text("LAPOR"),
),
RaisedButton(
padding: const EdgeInsets.symmetric(vertical: 50, horizontal: 36),
textColor: Colors.white,
color: Colors.redAccent,
onPressed: (){},
child: new Text("PENGUMPULAN"),
),
],
),
),
],
),
onRefresh: _showDialog
)
Try wrapping your widgets in a SafeArea class
https://api.flutter.dev/flutter/widgets/SafeArea-class.html
https://www.youtube.com/embed/lkF0TQJO0bA