Is there a way to use Expanded widget inside Wrap widget - flutter

What I wanted to achieve is that the size of the elevated button should fill the entire space. in my case if you look at the picture attached i want "user experience" and "engineering" fill the empty space in blue and in the same time aligned from left and right
I am trying to make the buttons size dynamic inside the warp widget, her my code below
Wrap(
spacing: 10,
alignment: WrapAlignment.spaceBetween,
children: [
for (InterestsClass item in interests)
InterestsButton(
text: item.interestsName,
)
],
),
InterestsButton is just an elevated button that is reusable, but if I add expanded as show below its Incorrect use of ParentDataWidget, since there is no flex parent
Widget build(BuildContext context) {
return Expanded(
child: ElevatedButton(
onPressed: () {
setState(() {
_flag = !_flag;
});
},
child: Text(
widget.text,
style: TextStyle(color: _flag ? kWhite : k34, fontSize: 13),
),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
shape: StadiumBorder(),
primary: _flag ? kBlue : Color(0xffFAFAFA)),
),
);
}

I think u can use an alternative way Chip/ ChoiceChip. These widget is available in Flutter very useful for creating size dynamic button with change style when tap.
See more here
https://api.flutter.dev/flutter/material/Chip-class.html
https://medium.com/flutterdevs/chip-widgets-in-flutter-7a2d3d34597c

Result One
I have it working now. Here is the code:
FilterChip(
label: Text("text"),
backgroundColor: Colors.transparent,
shape: StadiumBorder(side: BorderSide()),
onSelected: (bool value) {print("selected");},
),
Result Two
You can achieve it by this way
Chip(
shape: RoundedRectangleBorder(
side: BorderSide(color: colorThird, width: 1),
borderRadius: BorderRadius.circular(10),
),
backgroundColor: colorSecondary,
deleteIcon: Icon(
Icons.close,
color: Colors.white,
size: 15,
),
label: Text(searchController.recentSearchesList[index], style:
TextStyle(color: Colors.white),),
deleteButtonTooltipMessage: 'erase',
onDeleted: () {
print(index);
},
);

Related

Flutter TextButton is there but not displaying

I've been updating a Flutter app and replaced a FlatButton with the new TextButton. But now the button doesn't display on the Card. I can click it and it works, and if I long press you can see the button and it's caption.
The card widget code is below.
Card otherSwapCard(
List<FSRows?> data, int index, context, Function circularprogress) {
String? shiftDate = formatJsonDate(data[index]!.shiftDate!, 'dd/MM/yyyy');
//calculate time value string
String shiftTimes =
'${formatJsonTime24To12(data[index]!.startTime!)} - ${formatJsonTime24To12(data[index]!.finishTime!)}';
return Card(
color: Colors.white,
elevation: 3,
margin: EdgeInsets.fromLTRB(16, 4, 16, 12),
child: Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(
width: 2.0,
color: kMainColor40,
),
),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Expanded(
flex: 72,
child: Column(
children: <Widget>[
DataKeyRow(dkLabel: 'Job:', dkValue: data[index]!.jobName!),
SizedBox(height: 2),
DataKeyRow(dkLabel: 'Date:', dkValue: shiftDate!),
SizedBox(height: 2),
DataKeyRow(
dkLabel: 'Time:', dkValue: shiftTimes.toLowerCase()),
],
),
),
Expanded(
flex: 28,
child: Center(
child: TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
),
child: Text('Fill', style: TextStyle(color: Colors.white)),
onPressed: () { },
),
),
),
],
),
),
),
);
}
Card, TextButton, and Text three are in white color. So trying changing the color of the Button or Text.
To change the TextButton color
TextButton(
onPressed: () {},
style: TextButton.styleFrom(
primary: Colors.white,
backgroundColor: Colors.black, // Background Color
),
child: const Text(
'Text Button ',
style: TextStyle(fontSize: 24),
),
)
The backgroundColor will change the TextButton background color to black and the primary will change the font color to white, you can customize it accordingly.
Both your Card color and TextButton Text color are White, you just need to change one of them.
I copied your code and change the color and everything went fine.
Card(
color: Colors.white,
TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
),
child: Text('Fill', style: TextStyle(color: Colors.**white**)),
onPressed: () { },
),
Card and TextButton both are in white color , so try changing your code.
Change
child: Text('Fill', style: TextStyle(color: Colors.white)),
to
child: Text('Fill', style: TextStyle(color: Colors.black)),

Flutter transparent button elevation

I created a custom button that I want to give a color and an elevation. Works finde, except when I give the button the color Colors.transparent. Here is an example to showcase my problem:
Widget buildButton(void Function() onPressed, String label, double? elevation) {
return OutlinedButton(
onPressed: onPressed,
child: Text(label),
style: OutlinedButton.styleFrom(
backgroundColor: Colors.white,
side: BorderSide(
width: 2,
color: Colors.blue,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
elevation: elevation ?? 0
),
);
}
class ButtonsDemo extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
buildButton(() {}, "Button", null),
buildButton(() {}, "Button", 5),
],
),
),
);
}
}
The buttons look like this:
Here is how they should look like (I set the color of the buttons to the background color of the screen to showcase them, but I can't do that in my code):
I tried this answer but it only got me so far (I don't know if I did it completely wrong, but it looks so):
I also tried this answer and it got me there:
So, as you can see, both of them answers haven't helped me at all. Can anyone show me how I can add elevation to a transparent button?
The best solution to your problem is to use ElevatedButton instead of OutlinedButton and to give it a shadowColor. The code below is for the second button, please give it a try.
ElevatedButton(
onPressed: () {},
child: Text(
'Elevated',
style: TextStyle(color: Colors.black),
),
style: ElevatedButton.styleFrom(
elevation: 5,
primary: Colors.transparent,
shadowColor: Colors.transparent.withOpacity(0.1),
side: BorderSide(
width: 2,
color: Colors.blue,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),

how to add a button with icon in Flutter

I have a button in my app like
And below is the code for the same
Widget loginButtonChild = const Text(
"Log in",
style: TextStyle(
color: Colors.white,
fontFamily: "OpenSans-Regular",
),
);
FlatButton.icon(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
color: Colors.red,
label: loginButtonChild,
icon: Icon(Icons.arrow_forward ),
onPressed: () {
//some function
},
)
I am trying to create a button something like
Can anyone help in this or any suggestions?
First off, FlatButton and RaisedButton are deprecated. Use TextButton and ElevatedButton instead.
If you want to add an icon to a text button, use ElevatedButton.icon or TextButton.icon constructor. It will add the icon to the left of the text.
However if you want to add the icon to the right of the text. swap the icon with text and vice versa. This works because both icon and text params are Widget
// icon before text
ElevatedButton.icon(
icon: Icon(Icons.arrow_forward, size: 16),
label: Text('Icon Button'),
onPressed: () => {},
),
// icon after text
ElevatedButton.icon(
icon: Text('Icon Button'),
label: Icon(Icons.arrow_forward, size: 16),
onPressed: () => {},
),
Live Demo
Use that
RaisedButton(
onPressed: () {},
padding: const EdgeInsets.symmetric(horizontal: 24),
color: Colors.redAccent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Row(
children: <Widget>[
Text("Label", style: TextStyle(color: Colors.white)),
SizedBox(width: 6),
Icon(Icons.chevron_right, color: Colors.white),
],
),
),
FlatButton.icon puts the icon on the left, what you can do is use FlatButton and in the child put a Row with the label + icon, like this:
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
color: Colors.red,
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
loginButtonChild,
const SizedBox(width: 8.0),
Icon(Icons.arrow_forward),
],
),
onPressed: () {
//some function
},
)
If you want to have the button with elevation, just change FlatButton for RaisedButton

Keyboard Crushing TextFields

I'm learning to code in Flutter and and redoing one of my Android/iOS apps. This is roughly what the UI looks like so far:
When I tap a field to open the keyboard, it smashes all of the fields together.
I'm trying to decide what the best way of handling this is. The only way I can think of to fix it is to somehow not have all of the fields and the button pushed up when the keyboard opens. But if I do that, then the fields lower on the screen will not be visible when data is being entered into them. Is there a good way to handle this?
This is the code for this view:
class _CarbCalcState extends State<CarbCalc> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Carb Calculator'),
backgroundColor: Colors.purple.shade700,
leading: IconButton(
icon: Icon(Icons.settings),
onPressed: () {},
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.library_books),
onPressed: () {},
)
],
),
body: Container(
margin: EdgeInsets.only(top: 40.0, left: 20.0, right: 20.0),
child: Column(
children: <Widget>[
NutrientValue(
nutrientName: 'Protein',
onChanged: (value) {
print('Protein Changed');
},
),
NutrientValue(
nutrientName: 'Fat',
onChanged: (value) {},
),
NutrientValue(
nutrientName: 'Fiber',
onChanged: (value) {},
),
NutrientValue(
nutrientName: 'Moisture',
onChanged: (value) {},
),
NutrientValue(
nutrientName: 'Ash',
onChanged: (value) {},
),
SizedBox(
height: 30.0,
),
ButtonTheme(
minWidth: double.infinity,
height: 50,
child: FlatButton(
onPressed: () {},
color: Colors.blue.shade700,
child: Text(
'Calculate Carbs',
style: TextStyle(
fontSize: 20,
),
),
),
),
],
),
),
);
}
}
class NutrientValue extends StatelessWidget {
NutrientValue({#required this.nutrientName, #required this.onChanged});
final String nutrientName;
final Function onChanged;
#override
Widget build(BuildContext context) {
return Expanded(
child: Row(
children: <Widget>[
Container(
width: 90,
child: Text(
nutrientName,
style: TextStyle(fontSize: 18.0),
),
),
Expanded(
child: TextField(
keyboardType: TextInputType.numberWithOptions(
signed: false,
decimal: true,
),
style: TextStyle(
color: Colors.black,
fontSize: 18.0,
),
onChanged: this.onChanged,
decoration: InputDecoration(
hintText: 'Enter $nutrientName',
hintStyle: TextStyle(
color: Colors.grey,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(15.0),
),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Colors.white,
),
),
),
],
));
}
}
You are having a Column with several Expanded inside which take care that the space is evenly distributed. When the space shrinks by the keyboard, they move together. Basically it is a good idea to put input fields into a Scrollview because of the Keyboard. You can use SingleChildScrollView with your Column but you need to replace the Expanded widgets with a Container for example or just use the plain TextFields.

How to set background color for an icon button?

I want to apply background color for icon button but I don't see an explicit backgroundColor property for it. I want to achieve this:
Currently I was able to achieve till here:
Below is the code so far:
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
backgroundColor: Color(0xFF13212C),
appBar: AppBar(
title: Text('Demo'),
),
drawer: appDrawer(),
body: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Column(
children: <Widget>[
// new Flexible(
new TextField(
style: new TextStyle(
color: Colors.white,
fontSize: 16.0),
cursorColor: Colors.green,
decoration: new InputDecoration(
suffixIcon: new IconButton(
icon: new Image.asset('assets/search_icon_ivory.png'),onPressed: null),
fillColor: Colors.black,
contentPadding: new EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 20.0),
filled: true,
hintText: 'What Do You Need Help With?',
hintStyle: new TextStyle(
color: Colors.white
)
)
)
// )
]
),
You can use a Circular Avatar with the radius = text field's height/2 or whatever height you prefer.
To figure out text field specs you can visit material.io
So the chunk of code is going to be like the following:
CircleAvatar(
radius: 30,
backgroundColor: Color(0xff94d500),
child: IconButton(
icon: Icon(
Icons.search,
color: Colors.black,
),
onPressed: () {
...
},
),
),
This way you get an Icon button with background color.
I hope this can help you guys.
you can wrap your IconButton with Container and use color property to achieve desire output.
May Following Example help You.
suffixIcon: Container(
color: Colors.green,
child: new IconButton(
icon: new Icon(Icons.search,color: Colors.white,),onPressed: null),
),
You can achieve it with TextButton
TextButton(
style: TextButton.styleFrom(
backgroundColor: colorScheme.primary,
shape: CircleBorder(),
),
child: Icon(
MdiIcons.send,
color: colorScheme.onPrimary,
),
onPressed: () {},
),
The output will look like this:
From the official Flutter docs:
Adding a filled background
Icon buttons don't support specifying a
background color or other background decoration because typically the
icon is just displayed on top of the parent widget's background. Icon
buttons that appear in AppBar.actions are an example of this.
It's easy enough to create an icon button with a filled background
using the Ink widget. The Ink widget renders a decoration on the
underlying Material along with the splash and highlight InkResponse
contributed by descendant widgets.
Tl;dr: IconButton doesn't support background color out-of-the-box. Use this workaround to add the background color and the splash effect when clicking the button:
Ink(
decoration: ShapeDecoration(
color: Colors.blue,
shape: CircleBorder(),
),
child: IconButton(
icon: Icon(Icons.add),
color: Colors.white,
onPressed: () {},
),
),
Live Demo
This is now the officially recommended way to set background color on an IconButton, and the flutter docs have been updated to reflect this.
Hope, this will satisfied you
Ink(
decoration: ShapeDecoration(
color: Colors.red,
shape: CircleBorder(),
),
child: IconButton(
icon: Icon(Icons.delivery_dining),
onPressed: () {
print('pressed');
},
),
),
You can not do that with IconButton widget yet. Good news is that you may replace it with FlatButton like that:
suffixIcon: new FlatButton(
color: Colors.green,
disabledColor: Colors.green[100],
child: Icon(Icons.search)),
color will be used in case onPressed handler is defined, otherwise it will be rendered with disabledColor background.
IconButton does not support that, and RaisedButton recently is deprecated in favour of ElevatedButton, which supports changing background colours as well as shapes, but you cannot easily make it a perfect circle.
So to think out of the box, why not use a FloatingActionButton? They are just widgets too, so they can appear anywhere on the screen. For example, I'm using a FAB in a video chat demo app to toggle cameras.
Example code:
FloatingActionButton(
child: Icon(
Icons.flip_camera_ios_outlined,
color: Colors.white,
),
onPressed: () {
// do your thing here
},
)
And if you aren't happy about its default size, you can always wrap it with a SizedBox to change the width however you see fit.
With latest flutter and material-3 design system, this is just a piece a cake.
IconButton(
onPressed: () { ... },
icon: const Icon(Icons.search),
style: IconButton.styleFrom(backgroundColor: Colors.redAccent),
),
Add a Material
Material(
color:Colors.green
child:IconButton(
icon: Icon(Icons.add),
color: Colors.white,
onPressed: () {},
));
Official solution:
Depends on official documentation of flutter about IconButton Class:
Adding a filled background
Icon buttons don't support specifying a background color or other
background decoration because typically the icon is just displayed on
top of the parent widget's background. Icon buttons that appear in
[AppBar.actions] are an example of this.
It's easy enough to create an icon button with a filled background
using the [Ink] widget. The [Ink] widget renders a decoration on the
underlying [Material] along with the splash and highlight
[InkResponse] contributed by descendant widgets.
So the code will be like this:
Material(
color: Colors.transparent,
child: Ink(
decoration: ShapeDecoration(
color: Colors.white,
shape: CircleBorder(),
),
child: IconButton(
tooltip: "Some Text...",
icon: Icon(Icons.flash_off),
color: Colors.black,
onPressed: () {},
),
),
);
See official example code from flutter, click here ...
Note: Reason to wrap it with Material widget because Ink is drawn
on the underlying Material widget, if you remove it decoration will not appear.
I've used multiple colors to demonstrate You can do as you want. And I say that You put your IconButton into Material widget. This will also solve your Splash Color (which is slightly grey color with some transparency).
If you want it raised, you can use RaisedButton like this:
ConstrainedBox(
constraints: BoxConstraints(maxWidth: 50),
child: RaisedButton(
color: kColorLightGrey,
padding: EdgeInsets.symmetric(
vertical: 12,
),
shape: StadiumBorder(),
child: Icon(Icons.refresh),
onPressed: () {
},
),
),
SizedBox(
height: 38,
width: 38,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.grey,
onPrimary: Colors.red,
padding: EdgeInsets.zero,
shape: const CircleBorder(),
),
onPressed: () {},
child: const Icon(Icons.refresh),
);
You can use FloatingActionButton for such case But you cannot use FAB in a widget for multi use, it shows error. In such case use this code. It will work on images also.
Card(
color: Colors.blue,
shape: CircleBorder(),
child: IconButton(
icon: const Icon(
Icons.edit,
size: 20,
color: Colors.white,
),
onPressed: () {
print('tapped');
},
));
You can solve this with a GestureDetector and a container. I like using this solution because of the variety of controls it gives you from the combination.
Additionally, GestureDetector opens up more interaction events if you want to incorporate those across devices.
GestureDetector(
onTap: () {},
child: ClipOval(
child: Container(
//add extra margin to visual center icon if icon isn't balanced
color: backgroundColor,
width: size,
height: size,
child: Icon(
Icons.question_mark_sharp,
size: size * 0.6, //size to your preference
color: iconColor,
),
),
),
);
Alternatively you can do this from another question circle icon button
RawMaterialButton(
onPressed: () {},
elevation: 2.0,
fillColor: Colors.white,
child: Icon(
Icons.pause,
size: 35.0,
),
padding: EdgeInsets.all(15.0),
shape: CircleBorder(),
)
ElevatedButton with Theme.
ElevatedButton(
style: ButtonThemes.iconButton,
child: Icon(
icon,
color: color,
),
onPressed: () {},
)
static ButtonStyle get iconButton=> ButtonStyle(
backgroundColor: MaterialStateProperty.all(
backgroundColor,
),
...
);