OutlinedButton exception when trying to set max size - flutter

I'm trying to create something like the below image:
I used two OutlinedButton in a row:
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
OutlinedButton(
child: const Icon(
Icons.arrow_back,
color: colorBackButton,
),
style: ButtonStyle(
fixedSize: MaterialStateProperty.resolveWith(
(states) => Size(65.0, 65.0)))),
Padding(padding: EdgeInsets.only(left: 14.0)),
OutlinedButton(
child: Text(
'Next',
style: GoogleFonts.inter(
textStyle: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.resolveWith((states) {
return colorPrimaryButton;
}),
}),
])
This has led me to this result:
I tried to wrap the Next button with Sizedbox.expand and also add a minimumSize to the button style:
minimumSize: MaterialStateProperty.resolveWith((states) => const Size.fromHeight(55.0))
Both results with a BoxConstraints forces an infinite width. These invalid constraints were provided to RenderPhysicalShape's layout() function by t
Digging into the exception, it's written:
The offending constraints were: BoxConstraints(w=Infinity, 55.0<=h<=Infinity)
How can I make the Next button fill the entire row's space (discluding the padding of course)?

You can wrap with Expanded widget to get maximum available space.
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
OutlinedButton(
onPressed: () {},
child: const Icon(
Icons.arrow_back,
color: Colors.amber,
),
style: ButtonStyle(
fixedSize: MaterialStateProperty.resolveWith(
(states) => const Size(65.0, 65.0),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 14.0),
child: OutlinedButton(
onPressed: () {},
child: const Text('Next',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.deepPurple),
fixedSize: MaterialStateProperty.resolveWith(
(states) => const Size(65.0, 65.0),
),
),
),
),
),
],
);
You can play with padding. Also, you can use LayoutBuilder.

try use expanded widget Wrap next button with expanded like that
Expanded(
child: OutlinedButton(
child: Text(
'Next',
style: GoogleFonts.inter(
textStyle: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.resolveWith((states) {
return colorPrimaryButton;
}),
}),
),

Related

How can I give my text a background color?

I want to give color to the text background like the picture below:
while mine is still like this:
And the code that I have written is like this:
Text(
'Last Activity',
style: TextStyle(
fontSize: 16,
color: ColorName.whitePrimary,
),
),
body: Center(
child: TextButton.icon(
style: TextButton.styleFrom(
textStyle: TextStyle(color: Colors.blue),
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6.0),
),
),
onPressed: () => {},
icon: Text('Last Activity'),
label: Icon(Icons.chevron_right),
),
),
Set the style parameter in your Text widget:
Text(
'Hello, World!',
style: TextStyle(fontSize: 32, backgroundColor: Colors.green),
);
See also
TextStyle (flutter.dev)
Try the following code:
Text(
'Last Activity',
style: TextStyle(
fontSize: 16,
color: ColorName.whitePrimary,
backgroundColor: Colors.indigo[400],
),
),
Refer below code hope its help to you, I have try 2 ways TextButton.icon and Row
1. Using TextButton Widget
TextButton.icon(
style: TextButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: const Color.fromRGBO(60, 75, 175, 1),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6.0),
),
),
onPressed: () => {},
icon: const Text('Last Activity'),
label: const Icon(Icons.chevron_right),
),
Result Using TextButton.icon->
2. Using Row
GestureDetector(
onTap: () {},
child: Container(
height: 30,//change on your need
width: 120,//change on your need
padding: const EdgeInsets.all(5),
alignment: Alignment.center,
color: const Color.fromRGBO(60, 75, 175, 1),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: const [
Text(
'Last Activity',
style: TextStyle(
color: Colors.white,
),
),
Icon(
Icons.chevron_right,
color: Colors.white,
),
],
),
),
),
Result Using Row->
Try the following code:
Text(
'Last Activity',
style: TextStyle(
fontSize: 16,
color: ColorName.whitePrimary,
),
),

How to use multiply rows with ResponsiveDatatable?

I want to create a second row after the icon "Bearbeiten", but this is not possible, because I need to use "title" for the first row. child: ResponsiveDatatable(title: Row(
I want to have a code like this below. How can I use ResponsiveDatatable, but although use a second row for my buttons?
This image shows, how the code looks at the moment
(without the second row). Because there is not enough space in one row, I want to create a second row.
How can I do this with a ResponsiveDatatable?
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
Container(
margin: EdgeInsets.all(10),
padding: EdgeInsets.all(0),
constraints: BoxConstraints(
maxHeight: 700,
),
child: Card(
elevation: 1,
shadowColor: Colors.black,
clipBehavior: Clip.none,
child: ResponsiveDatatable(
title: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//add user to the list
TextButton.icon(
onPressed: () => {
Navigator.of(context).pushNamed(RegristrationRoute)
},
icon: Icon(Icons.add,
color: Colors.black,
),
label: Text("Hinzufügen",
style: TextStyle(
color: Colors.black,
)
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.grey),
padding: MaterialStateProperty.all<EdgeInsets>(
EdgeInsets.all(10)),
),
),
SizedBox(width: 30,),
TextButton.icon(
onPressed: () => {
Navigator.of(context).pushNamed(RegristrationRoute)
//Pop-up Fenster für die Registration machen, wenn Zeit - ansonsten registrationsfenster weiterleiten
},
icon: Icon(
IconData(0xf00d, fontFamily: 'MaterialIcons'),
color: Colors.black,
),
label: Text("Bearbeiten",
style: TextStyle(
color: Colors.black,
)
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.grey),
padding: MaterialStateProperty.all<EdgeInsets>(
EdgeInsets.all(10)),
),
),]),
child: Row( //error, I am not allowed to use child because of title
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(width: 30,),
TextButton.icon(
onPressed: () => {},
icon: Icon(
IconData(0xe3b1, fontFamily: 'MaterialIcons'),
color: Colors.black,
),
label: Text("Sperren",
style: TextStyle(
color: Colors.black,
)
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.grey),
padding: MaterialStateProperty.all<EdgeInsets>(
EdgeInsets.all(10)),
),
),
SizedBox(width: 30,),
TextButton.icon(
onPressed: () => {},
icon: Icon(
IconData(0xe3b0, fontFamily: 'MaterialIcons'),
color: Colors.black,
),
label: Text("Freischalten",
style: TextStyle(
color: Colors.black,
)
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.grey),
padding: MaterialStateProperty.all<EdgeInsets>(
EdgeInsets.all(10)),
),
),
SizedBox(width: 30,),
TextButton.icon(
onPressed: () async => {
await AuthProvider.deleteUser(uid),
print(uid + 'user gelöscht')
},
icon: Icon(
IconData(0xe1bb, fontFamily: 'MaterialIcons'),
color: Colors.black,
),
label: Text("Löschen",
style: TextStyle(
color: Colors.black,
)
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.grey),
padding: MaterialStateProperty.all<EdgeInsets>(
EdgeInsets.all(10)),
),
),
],
),
What you are expecting is the functionality of the Column.
ResponsiveDatatable(
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: [
TextButton.icon(
onPressed: () => {
Navigator.of(context)
.pushNamed(RegristrationRoute)
},
icon: Icon(
Icons.add,
color: Colors.black,
),
label: Text("Hinzufügen",
style: TextStyle(
color: Colors.black,
)),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(
Colors.grey),
padding: MaterialStateProperty.all<EdgeInsets>(
EdgeInsets.all(10)),
),
),
SizedBox(width: 30,),
TextButton.icon(
onPressed: () => {
Navigator.of(context)
.pushNamed(RegristrationRoute)
//Pop-up Fenster für die Registration machen, wenn Zeit - ansonsten registrationsfenster weiterleiten
},
icon: Icon(
IconData(0xf00d, fontFamily: 'MaterialIcons'),
color: Colors.black,
),
label: Text("Bearbeiten",
style: TextStyle(
color: Colors.black,
)),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(
Colors.grey),
padding: MaterialStateProperty.all<EdgeInsets>(
EdgeInsets.all(10)),
),
),
],
),
SizedBox(height: 10,),
Row(
children: [
TextButton.icon(
onPressed: () => {},
icon: Icon(
IconData(0xe3b1, fontFamily: 'MaterialIcons'),
color: Colors.black,
),
label: Text("Sperren",
style: TextStyle(
color: Colors.black,
)),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(
Colors.grey),
padding: MaterialStateProperty.all<EdgeInsets>(
EdgeInsets.all(10)),
),
),
SizedBox(width: 30,),
TextButton.icon(
onPressed: () => {},
icon: Icon(
IconData(0xe3b0, fontFamily: 'MaterialIcons'),
color: Colors.black,
),
label: Text("Freischalten",
style: TextStyle(
color: Colors.black,
)),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(
Colors.grey),
padding: MaterialStateProperty.all<EdgeInsets>(
EdgeInsets.all(10)),
),
),
SizedBox(width: 30,),
TextButton.icon(
onPressed: () async => {
// await AuthProvider.deleteUser(uid),
// print(uid + 'user gelöscht')
},
icon: Icon(
IconData(0xe1bb, fontFamily: 'MaterialIcons'),
color: Colors.black,
),
label: Text("Löschen",
style: TextStyle(
color: Colors.black,
)),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(
Colors.grey),
padding: MaterialStateProperty.all<EdgeInsets>(
EdgeInsets.all(10)),
),
)
],
),
]),
source: [],
)))
And there is a minor error in the parenthesis closing also.
Note: It will solve your problem but still it will not meet responsiveness.
I had the similar problem.
Instead of TextButton, I made a button-UI in container and wrapped it with InkWell (A rectangular area of a Material that responds to touch).
InkWell(
// on Tap function used and call back function as defined here
onTap: () {
setState(() {
// state will be set when the inkwell area is tapped
inkwell='Inkwell Tapped';
});
},
onLongPress: () {
setState(() {
inkwell='InkWell Long Pressed';
});
},
child: Container(
// container displaying the text
color: Colors.green,
width: 120,
height: 70,
child: Center(
child: Text(
'Button text',
))),
),

Reducing the space between icon and text - Flutter OutlinedButton

Is there an easy way to reduce the space between the icon and text on an OutlinedButton?
Below is my code. Have made a few different attempts but no luck.
OutlinedButton.icon(
onPressed: () {},
icon: Icon(Icons.flash_on_outlined, size: 20.0),
label: Text(
'Surge',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
style: OutlinedButton.styleFrom(
padding: EdgeInsets.zero,
//fixedSize: Size(40, 25),
backgroundColor: Colors.blue[100],
side: BorderSide(
color: Colors.blue,
width: 1,
),
),
),
OutlinedButton.icon(
onPressed: () {},
icon: Wrap(
// mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(Icons.flash_on_outlined, size: 20.0),
Text(
'Surge',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
)
],
),
label: Text(""),
style: OutlinedButton.styleFrom(
padding: EdgeInsets.zero,
//fixedSize: Size(40, 25),
backgroundColor: Colors.blue[100],
side: BorderSide(
color: Colors.blue,
width: 1,
),
),
)
You are using OutlinedButton.icon. If you look into its source code, you'll see that it's nothing magical at all: it simply puts your icon and text in a Row and places a SizedBox in the middle as a gap, its source code is as follows:
// Flutter source code: `outlined_button.dart`, line 378.
final double gap = scale <= 1 ? 8 : lerpDouble(8, 4, math.min(scale - 1, 1))!;
return Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[icon, SizedBox(width: gap), Flexible(child: label)],
);
So, if you don't like this default 8-unit gap, simply don't use .icon constructor. Just use the normal constructor and pass in a Row as its child, with whatever gap you want:
OutlinedButton(
child: Row(
mainAxisSize: MainAxisSize.min,
children: const [
Icon(Icons.star),
SizedBox(width: 8),
Text('Add to bookmark'),
],
),
onPressed: () {},
)
The best way to implement such case I think is to replace icon button with button with RichText as a child:
OutlinedButton(
onPressed: () {},
child: RichText(
text: TextSpan(children: [
// Icon as a font character
TextSpan(
text: String.fromCharCode(Icons.flash_on_outlined.codePoint),
style: TextStyle(
color: Colors.amber,
fontFamily: Icons.flash_on_outlined.fontFamily,
package: Icons.flash_on_outlined.fontPackage,
),
),
// Button text
TextSpan(
text: 'Surge',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
]),
),
style: OutlinedButton.styleFrom(
backgroundColor: Colors.blue[100],
side: BorderSide(
color: Colors.blue,
width: 1,
),
),
)
This solution removes any margin from an icon.
your size, about the icon and the text, are block or you can modify it?

How to add icon from the assets/icons into my button components

this is my button component code :
Widget build(BuildContext context) {
return MaterialButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(40)),
side: BorderSide(color: kPrimaryColor)
),
padding: padding,
color: color,
minWidth: 280,
onPressed: press,
child: Text(
text,
style: TextStyle(
color: Colors.grey[900],
fontSize: 15,
),
),
);
}
Wrap your child with a Row and add the icon as the second element of your Row's children. It will turn out something like below:
child: Row(
children: [
Text(
text,
style: TextStyle(
color: Colors.grey[900],
fontSize: 15,
),
),
Image.asset('assets/icons/my_icon.png'),
],
),
You can use a TextButton.icon widget
And flip the label and icon (both accept a widget)
TextButton.icon(
icon: Text("MyText",
style: TextStyle(
fontSize: 11.0,
fontFamily: "Raleway"
),
),
label: Image.asset('assets/myImage.png', width: 20.0,height: 20.0,),
style: ButtonStyle(
// backgroundColor:
// MaterialStateProperty.all(Colors.green.shade200),
overlayColor: MaterialStateProperty.all(Colors.green),
side: MaterialStateProperty.resolveWith((states) {
return BorderSide(color: Colors.grey, width: 1);
})),
)
Inside the TextButton, I manage to add a row that contains icon and text
TextButton(
onPressed: (){},
child: Row(
mainAxisSize: MainAxisSize.min,
children: const [
Icon(
Icons.arrow_back_ios,
color: Themes.primaryColor,
),
Text(
'Back',
style: TextStyle(
fontSize: 17,
color: Themes.primaryColor,
),
),
],
),
),

Why is there so much space between Rows in my Flutter code?

Positioned(
bottom: 15,
left: 15,
child: SizedBox(
height: 100,
child: Column(
children: [
Row(
children: [
ElevatedButton(
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
minimumSize: Size(40, 20),
backgroundColor: colorSet.buttonForeground,
),
onPressed: () {},
child: Text(
'Once',
style: TextStyle(
color: colorSet.text,
fontSize: 12,
),
),
),
SizedBox(
width: 8,
),
ElevatedButton(
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
minimumSize: Size(40, 20),
backgroundColor: colorSet.buttonForeground,
),
onPressed: () {},
child: Text(
'Specific Days',
style: TextStyle(
color: colorSet.text,
fontSize: 12,
),
),
),
],
),
Row(
children: [
ElevatedButton(
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
minimumSize: Size(40, 20),
backgroundColor: colorSet.buttonForeground,
),
onPressed: () {},
child: Text(
'Daily',
style: TextStyle(
color: colorSet.text,
fontSize: 12,
),
),
),
SizedBox(
width: 3,
),
ElevatedButton(
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
minimumSize: Size(40, 20),
backgroundColor: colorSet.buttonForeground,
),
onPressed: () {},
child: Text(
'Weekly',
style: TextStyle(
color: colorSet.text,
fontSize: 12,
),
),
),
],
),
],
),
))
Hi, I am relatively new at flutter. I am trying to build a card for chores, and this particular part of the card is giving me problems.
I have no idea, why is there so much vertical space between each row (each row basically having two buttons). I've tried several things, to solve this, but I give up for now and come here for advice.
Thanks!
Try this, remove the Sizedbox(height:100)
I addition, you could remove the SizedBox(width : 8) inside your rows and try Row(mainAxisAlignment:MainAxisAlignment.spaceBetween, children:[]);
Positioned(
bottom: 15,
left: 15,
child: Column(
children: [
Row(
mainAxisAlignment:MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
minimumSize: Size(40, 20),
backgroundColor: colorSet.buttonForeground,
),
onPressed: () {},
child: Text(
'Once',
style: TextStyle(
color: colorSet.text,
fontSize: 12,
),
),
),
ElevatedButton(
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
minimumSize: Size(40, 20),
backgroundColor: colorSet.buttonForeground,
),
onPressed: () {},
child: Text(
'Specific Days',
style: TextStyle(
color: colorSet.text,
fontSize: 12,
),
),
),
],
),
Row(
mainAxisAlignment:MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
minimumSize: Size(40, 20),
backgroundColor: colorSet.buttonForeground,
),
onPressed: () {},
child: Text(
'Daily',
style: TextStyle(
color: colorSet.text,
fontSize: 12,
),
),
),
ElevatedButton(
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
minimumSize: Size(40, 20),
backgroundColor: colorSet.buttonForeground,
),
onPressed: () {},
child: Text(
'Weekly',
style: TextStyle(
color: colorSet.text,
fontSize: 12,
),
),
),
],
),
],
))