ListTile covers button animation - flutter

Just noticed that ListTile cover button animation when wrapped inside a container with background color set to anything but transparent. Wrapping ListTile in a container with set color is the only way I know to change background color of ListTile. Is there any other way to change background color of the ListTile without loosing button animation?
Container(
color: Colors.green,
child: ListTile(
title: Text('Test'),
trailing: IconButton(
icon: Icon(Icons.add),
onPressed: () {},
),
),
)
OUTPUT

This is because of the way InkWell works (Some buttons, like the IconButton, use InkWell or InkResponse as their parent). You can read about it more on this github issue page.
In order to make that ripple effect to display on top of the decorated Container (the green one in your code) - it needs a Material widget above the Container in the widget display tree.
So you should edit the code and add a Material widget with transparency in your Container, so the widget display tree will look like Container -> Material -> Ink.
Container(
color: Colors.green,
child: Material(
type: MaterialType.transparency,
child: ListTile(
title: Text('Test'),
trailing: IconButton(
icon: Icon(Icons.add),
onPressed: () {},
),
),
),
),

Related

How to make a button in Flutter have the same shape as its icon?

I am trying to create a backspace button (using an ElevatedButton()). I don't want the button to be circular, but instead have the same shape as the icon. Also, when tapping the button, I want the splash effect to be the same size and shape as the button.
I've attached a reference image below of the shape I'm trying to replicate.
Extra challenge: I'm also trying to set the fill color of the button to be black and the color of the button to be grey (like the example).
You can use the IconButton() widget instead of ElevtaedButton() as well as defining the splashRadius to change the size of the splash effect:
IconButton(
splashRadius: 1, // Set the Size of the splash area
color: Colors.grey,
icon: Icon(Icons.backspace),
onPressed: () {},
),
Result:
Or, if you want to use ElevatedButton(), use the .icon constructor:
ElevatedButton.icon(
style: ElevatedButton.styleFrom(primary: Colors.grey.shade300),
label: Text('Hello World'),
icon: Icon(
Icons.backspace,
color: Colors.grey,
),
onPressed: () {},
)
Result:
There are many default icons in class Icons are not good-looking for your app. You can use some design platform, such as Figma, then download it as svg. Then the code could be:
InkWell(
onTap: () {},
child: SvgPicture.asset(path_to_svg_icon)
)
This way, you can edit color, shape, style... for your icon. Good luck!

Scrollable ListView bleeds background color to adjacent widgets

I'm looking to create a simple layout using a scrollable ListView, with an immovable header tile. I've placed the header tile and ListView into a Column, so the Header can be above the ListView without scrolling off the screen. However, when scrolling the ListView, my header tile seems to take on the color of whatever ListView tiles are scrolling "under" it.
On startup, the app looks like this:
However if we scroll half a tile down, the green color of the list tiles appears to push out the red from the header. The text from the green tiles does not have the same problem, and is properly occluded by the header tile
Minimal code for reconstruction
void main() => runApp(MyTestApp2());
class MyTestApp2 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("AppBar Header"),
),
body: Column(children: <Widget>[
ListTile(
title: Center(child: Text("This Header Should be Red")),
tileColor: Colors.redAccent,
),
Expanded(child: ListView(
children: List.generate(20, (int index) => "List Item $index")
.map((n) => ListTile(
title: Text(n.toString()),
tileColor: Colors.lightGreen))
.toList(),
)),
])),
);
}
}
What is happening here? I could probably achieve this layout using different Widgets, but I would like to know Why is this color bleed effect occurring? How does it fit with the box layout model Flutter uses?
EDIT: The immediate problem can be solved by wrapping the red ListTile in a Container widget, and setting the color property to be red on that container, like this:
Container(
color: Colors.redAccent,
child: ListTile(....
However I would still like to know what is going on in terms of the layout algorithm in the original code, if anybody knows. Shouldn't the existence of the header tile prevent our listview from pushing its elements into the area owned by the red ListTile?
Please try wrapping listTile with Material widget
I do not know why but the tileColor is keeping alive under the widgets while scrolling down the color of the tile went under the widgets above.
Wrapping the listTile with Material should fix that for some reason.
Material(
child: ListTile(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
tileColor:const Color.fromARGB(255, 247, 247, 247),
leading: Icon(
Icons.monetization_on_outlined,
color: Theme.of(context).primaryColor,
size: 50,
),
title: Text(
"Some Strings",
),
subtitle: Text("Some Strings"),
onLongPress: () {},
onTap: () {},
),
)

Flutter: IconButton() Being Partially Rendered Outside It's Container()

I'm trying to up my game when it comes to my Flutter layout / sizing knowledge and this post is something that I recently observed that confuses me.
I have a Container() that has an IconButton() having an icon from the stock Flutter icon set with a size that is passed as a parameter to the function.
Here is the code:
Widget buttonGallery(double size) {
return Container(
color: Colors.yellow,
child: IconButton(
icon: Icon(
Icons.photo,
size: size,
color: Colors.blue,
),
onPressed: (){},
},
),
);
}
With the size set to 75, I am noticing that the IconButton() is being partially rendered outside of its parent Container(). (This behavior is visible as I explicitly set the Container() color to yellow to be able to see the parent client area). See pic below:
Here are my questions:
Isn't the parent widget, which in this is a Container(), supposed to envelop it's child widget meaning that the child shouldn't overflow outside of it's parent? (I know that there's the OverflowBox() widget that supposed to allow this behavior, but I don't think that's the case here.)
In the case where the child is too big to fit its parent's client area, isn't Flutter supposed to clip the child and show those yellow hazzard lines indicating this scenario?
Having dealt a lot with Container(), I was under the impression that it was supposed to fit itself to the dimensions of its child. I'm confused why it's not doing this now.
All help/suggestions greatly appreciated.
/Joselito
The IconButton widget sizes itself according to the property iconSize. It defaults to 24, and that's the size being passed to your container.
This should do it for you:
Widget buttonGallery(double size) {
return Container(
color: Colors.yellow,
child: IconButton(
iconSize: size,
icon: Icon(
Icons.photo,
size: size,
color: Colors.blue,
),
onPressed: (){},
},
),
);
}

Flutter FloatingActionButton and IconButton

How can I create a FloatingActionButton like in the image?
I tried using FloatingActionButton, but it looks like a whole button with a circle.
I need something like an IconButton as shown in the image.
IconButton will actually deliver the result you are aiming for!
A FloatingActionButton, by guidelines, will always deliver the design you find on your "View in Your Room" titled button.
IconButton(
icon: Icon(Icons.back),
onPressed...
)
You need to use IconButton as below
// Icon Button
new IconButton(
icon: new Icon(Icons.favorite),
tooltip: 'Facorite icon',
color: Colors.blue, //set color which you want
onPressed: () {
// Do your work
},
),
For this, you can use actions ** property of appbar to put icons on the right side of title and **leading property to put a leading icon.
appbar: new AppBar(
leading: new Icon(Icons.search),
actions: <Widget>[
new Icon(Icons.search),
new Icon(Icons.search),
],
title: new Text("title"),
)
I suggest using an Icon inside a GestureDetector widget. This will give you the result you want, but also getting rid of the unnecessary padding around an IconButton.
GestureDetector(
onTap: () {},
child: Icon(Icons.arrow_back, color: Colors.black)
)

How do I remove Flutter IconButton big padding?

I want to have a row of IconButtons, all next to each other, but there seems to be pretty big padding between the actual icon, and the IconButton limits. I've already set the padding on the button to 0.
This is my component, pretty straightforward:
class ActionButtons extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.lightBlue,
margin: const EdgeInsets.all(0.0),
padding: const EdgeInsets.all(0.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
IconButton(
icon: new Icon(ScanrIcons.reg),
alignment: Alignment.center,
padding: new EdgeInsets.all(0.0),
onPressed: () {},
),
IconButton(
icon: new Icon(Icons.volume_up),
alignment: Alignment.center,
padding: new EdgeInsets.all(0.0),
onPressed: () {},
)
],
),
);
}
}
I want to get rid of most of the light blue space, have my icons start earlier on the left, and closer to each other, but I can't find the way to resize the IconButton itself.
I'm almost sure this space is taken by the button itself, 'cause if I change their alignments to centerRight and centerLeft they look like this:
Making the actual icons smaller doesn't help either, the button is still big:
thanks for the help
Simply pass an empty BoxConstrains to the constraints property and a padding of zero.
IconButton(
padding: EdgeInsets.zero,
constraints: BoxConstraints(),
)
You have to pass the empty constrains because, by default, the IconButton widget assumes a minimum size of 48px.
Two ways to workaround this issue.
Still Use IconButton
Wrap the IconButton inside a Container which has a width.
For example:
Container(
padding: const EdgeInsets.all(0.0),
width: 30.0, // you can adjust the width as you need
child: IconButton(
),
),
Use GestureDetector instead of IconButton
You can also use GestureDetector instead of IconButton, recommended by Shyju Madathil.
GestureDetector( onTap: () {}, child: Icon(Icons.volume_up) )
It's not so much that there's a padding there. IconButton is a Material Design widget which follows the spec that tappable objects need to be at least 48px on each side. You can click into the IconButton implementation from any IDEs.
You can also semi-trivially take the icon_button.dart source-code and make your own IconButton that doesn't follow the Material Design specs since the whole file is just composing other widgets and is just 200 lines that are mostly comments.
Wrapping the IconButton in a container simply wont work, instead use ClipRRect and add a material Widget with an Inkwell, just make sure to give the ClipRRect widget enough border Radius 😉.
ClipRRect(
borderRadius: BorderRadius.circular(50),
child : Material(
child : InkWell(
child : Padding(
padding : const EdgeInsets.all(5),
child : Icon(
Icons.favorite_border,
),
),
onTap : () {},
),
),
)
Instead of removing a padding around an IconButton you could simply use an Icon and wrap it with a GestureDetector or InkWell as
GestureDetector(
ontap:(){}
child:Icon(...)
);
Incase you want the ripple/Ink splash effect as the IconButton provides on click wrap it with an InkWell
InkWell(
splashColor: Colors.red,
child:Icon(...)
ontap:(){}
)
though the Ink thrown on the Icon in second approach wont be so accurate as for the IconButton, you may need to do some custom implementation for that.
Here's a solution to get rid of any extra padding, using InkWell in place of IconButton:
Widget backButtonContainer = InkWell(
child: Container(
child: const Icon(
Icons.arrow_upward,
color: Colors.white,
size: 35.0,
),
),
onTap: () {
Navigator.of(_context).pop();
});
I was facing a similar issue trying to render an Icon at the location the user touches the screen. Unfortunately, the Icon class wraps your chosen icon in a SizedBox.
Reading a little of the Icon class source it turns out that each Icon can be treated as text:
Widget iconWidget = RichText(
overflow: TextOverflow.visible,
textDirection: textDirection,
text: TextSpan(
text: String.fromCharCode(icon.codePoint),
style: TextStyle(
inherit: false,
color: iconColor,
fontSize: iconSize,
fontFamily: icon.fontFamily,
package: icon.fontPackage,
),
),
);
So, for instance, if I want to render Icons.details to indicate where my user just pointed, without any margin, I can do something like this:
Widget _pointer = Text(
String.fromCharCode(Icons.details.codePoint),
style: TextStyle(
fontFamily: Icons.details.fontFamily,
package: Icons.details.fontPackage,
fontSize: 24.0,
color: Colors.black
),
);
Dart/Flutter source code is remarkably approachable, I highly recommend digging in a little!
A better solution is to use Transform.scale like this:
Transform.scale(
scale: 0.5, // set your value here
child: IconButton(icon: Icon(Icons.smartphone), onPressed: () {}),
)
You can use ListTile it gives you a default space between text and Icons that would fit your needs
ListTile(
leading: Icon(Icons.add), //Here Is The Icon You Want To Use
title: Text('GFG title',textScaleFactor: 1.5,), //Here Is The Text Also
trailing: Icon(Icons.done),
),
I like the following way:
InkWell(
borderRadius: BorderRadius.circular(50),
onTap: () {},
child: Container(
padding: const EdgeInsets.all(8),
child: const Icon(Icons.favorite, color: Colors.red),
),
),
enter image description here
To show splash effect (ripple), use InkResponse:
InkResponse(
Icon(Icons.volume_up),
onTap: ...,
)
If needed, change icons size or add padding:
InkResponse(
child: Padding(
padding: ...,
child: Icon(Icons.volume_up, size: ...),
),
onTap: ...,
)