Put this code in pubspec.yaml
flutter_svg: ^0.19.3
Even I put in assets these
assets:
- assets\icons\logo.jpg
- assets\icons\menuicon.svg
- assets\icons\searchIcon.svg
In code it looks like this
IconButton(
icon: Icon(Icons.searchicon.svg),
onPressed: () {},
The Icon parameter accept any kind of widget not only Icon So you can use any widget like below
IconButton(icon: SvgPicture.asset(
assetName,
color: Colors.red,
semanticsLabel: 'Label'
),onPressed: () {},
Related
Slidable(
groupTag: 0,
key: UniqueKey(),
endActionPane: ActionPane(
children: [
SlidableAction(
onPressed: (BuildContext context) {
//whatever I will do here
},
icon: Icons.delete
)// SvgPicture.asset(Assets.svg.trash.svg()),)
],
),
child: _makeColumnTile(
myCard,
),
);
This is just a basic slidable item, which I am doing for every list item I have. I understand, and can work with slidable and listTile. However, I want to put my own custom svg file I saved in my assets folder, not pre-built Icons. But the icon: is apparently only wanting IconData
I tried putting svg directly with SvgPicture.asset(Assets.svg.trash.svg()), but the error is
The argument type SvgPicture can't be assigned to the parameter type IconData?,
also when I want to assign Icon to icon, this error pops up.
The argument type 'Icon' can't be assigned to the parameter type 'IconData?.
I understand why I cannot assign those properties, but isn't there any way I could solve this problem?
The SlidableAction only accepts IconData in its icon property. A list of MaterialIcons is available here.
SlidableAction(
icon: Icons.archive,
),
For your use case, you can instead use a CustomSlidableAction (check the package's API reference here).
CustomSlidableAction (
child: //Your action's icon or label.
)
You can pass your SVG as a child to it.
Flutter can't generate IconData from svgs. You must convert it to a ttf file and use it as your icon source.
You can use an online converter like https://fluttericon.com or do it locally with a package like https://pub.dev/packages/icon_font_generator
Then import it as following in your pubspec.yaml:
fonts:
- family: MyIcons
fonts:
- asset: assets/fonts/MyIcons.ttf
Then pass the Icon to your widget:
SlidableAction(
onPressed: (_) {},
backgroundColor: Color(0xFFFE4A49),
foregroundColor: Colors.white,
icon: MyIcons.Delete,
label: 'Delete',
),
appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(Icons.menu, color: Colors.pink,),
onPressed: () {
print("menu button is clicked");
}
),
title: IconButton(
onPressed: () { print("run button"); },
icon: Image.asset('assets/images/Slimer_by_shinrider-dcqaiza.webp')),
),
In codes, there's a IconButton that I made for making image to button. but it doesn't appear image. Is there any incorrect thing in code? or Do I have to another method?
Check the assets path setting in pubspec.yaml. Also, check if the image is in the assets path that you set normally.
pubspec.yaml
assets:
- assets/images/
show documents
https://docs.flutter.dev/development/ui/assets-and-images
GestureDetector(
onTap: () {print('click on edit');},
child: Image(
image: AssetImage('assets/images/icons/edit-button.png'),
fit: BoxFit.cover,
height: 20,
)),
try to use with .png
I want to add a filter option button without adding a search bar.
ScreenShot
You can do like this
InkWell(
onTap: () =>
child: const Icon(
Icons.tune,
color: Colors.grey,
),
),
Make use of IconButton like below
IconButton(
icon: Icon(Icons.tune),
onPressed: () {},
)
I have made a flatbutton icon, but it seems this is depreciated in the current flutter version. I have tried to upgrade it to a text button, but it does not show the text next to the icon.
This is the original code that has to be adapted to become a text button. Should I have to define a class or function to make it so?
Row(
children: [
FlatButton.icon(
onPressed: () => print('Live'),
icon: const Icon(
icons.videocam,
color: Colors.red,
),
label: Text('Live'),
),
],
)
Thx! :)
you can use TextButton.icon() widget. here an example:
TextButton.icon(
onPressed: () => print('Live'),
icon: Icon(Icons.videocam_rounded),
label: Text('Live'),
),
and here the result:
You can use an IconButton widget:
Row(
children: [
IconButton(
onPressed: () => print('Live'),
icon: const Icon(icons.videocam, color: Colors.red),
),
Text('Live'),
]
)
I want to disable an IconButton inside a State<T> class based on a condition. The official documentation states that in order to disable a button, the onPressed callback should be null. This makes the icon greyed-out.
This is however NOT true for icons with a custom asset. E.g. when having the following two icons
IconButton(
icon: Image.asset(somePath),
iconSize: 24,
onPressed: condition?()=>{someCallback();}:null),
IconButton(
icon: Icon(
Icons.settings,
),
iconSize: 24,
onPressed: condition?()=>{someCallback();}:null)
Then the first is grayed out but the latter is not. So how could I disable an IconButton with an Image.asset as image provider?
Simply because IconThemeData can be applied on Text and Icon as their color can be changed but not on asset image, which does not have a color property.
Still, you can alter it like this
IconButton(
icon: Image.asset(somePath, color: condition ? null : Colors.grey,),
iconSize: 24,
onPressed: condition ? () {
someCallback();
} : null,
),
IconButton(
icon: Icon(
Icons.settings,
),
iconSize: iconSize,
onPressed: condition ? () {
someCallback();
} : null,
),