How can I make image to active like button in flutter? - flutter

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

Related

How can I apply to image both ink well ripple effect during pressing and shimmer effect during loading?

I would like an image with shimmer effect applied while loading, similar to the fancy_shimmer_image package, to also receive ripple effect from an InkWell. Is there a way to do this?
The only way i found to do that is this "trick". Use a Stack for set a widget on top of your image. And then the inkwell effect works:
Stack(
children: [
// image with shimmer effect
FancyShimmerImage(
imageUrl:
'https://cdn.pixabay.com/photo/2014/06/03/19/38/board-361516_960_720.jpg',
shimmerBaseColor: Colors.amberAccent,
shimmerHighlightColor: Colors.redAccent,
shimmerBackColor: Colors.greenAccent,
),
//use this widget on top to do the effect
Positioned.fill(
child: Material(
color: Colors.transparent,
child: InkWell(
// splashColor: Colors.lightGreenAccent,
onTap: () {
print("on tap");
},
),
),
),
],
),
use the shimmer package and cached network image
and use it like that
CachedNetworkImage(
imageUrl: prov
.cart
.cartItems[index]
.image,
placeholder:
(context, url) =>
Shimmer.fromColors(
baseColor: Colors.grey.shade300,
highlightColor: Colors.white,
enabled: true, child: Image.asset(
'assets/images/product_placeholder.png',
),
)
),

How to fix "Exception caught by image resource service"

I have created a resources folder to store my images.
part of 'resources.dart';
class IconImages {
IconImages._();
static const String test1 = 'assets/images/test1.jpg';
static const String test2 = 'assets/images/test2.jpg';
static const String test3 = 'assets/images/test3.jpg';
}
I write them to the Map collection
final imagesIcon = <int, String>{
1: IconImages.test1,
2: IconImages.test2,
3: IconImages.test3,
};
and then pass the recorded images to the button.
icon: Image(image: AssetImage(imagesIcon.values.toString())),
// full button
Column(
children: [
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: Image(image: AssetImage(imagesIcon.values.toString())),
onPressed: () {},),
),
SizedBox(height: 40,),
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: Image(image: AssetImage(imagesIcon.values.toString())),
onPressed: () {},),
),
SizedBox(height: 40,),
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: Image(image: AssetImage(imagesIcon.values.toString())),
onPressed: () {},),
),
],
),
When I execute my code, I get the following exception
Everything works when I transmit the image in this form
icon: Image(image: AssetImage(IconImages.test2)),
But when I try to get a path from Map in this way, the above error is obtained
icon: Image(image: AssetImage(imagesIcon.values.toString())),
You are using your map wrong:
IconButton(
icon: Image(image: AssetImage(imagesIcon[1].toString())),
onPressed: () {},
),
I think it may be because you're passing icons as strings. Instead of using images with a class like:
ImageIcon(
AssetImage("images/icon.png"),
color: Colors.red,
size: 24,
),
Edit: after recreating the situation in my project I've found the reason behind that. You use imagesIcon.values.toString() to pass the value of an Icon. Did you check what this function returns? In my case it returned:
(assets/images/test1.jpg, assets/images/test2.jpg,
assets/images/test3.jpg)
No wonder the image is unreadable according to flutter because that is not a proper path or file.
Instead you can use your solution like this:
imagesIcon.values.elementAt(1) //0,1,2 whatever index you need from map

How to add search filter option like this image in flutter

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: () {},
)

How do I change a flatbutton.icon to a textbutton in Flutter?

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'),
]
)

How to update AppBar icon notification(State) from event implemented in different class?

My requirement is something like this:
Flutter - change appbar icon when receiving notification
But AppBar 'cart icon' which is to be notified on 'addToCart' button click implemented in another dart file. Am I doing something wrong by placing the AppBar and the rest in two different dart files? Let me know how to proceed.
You can use a global ValueNotifier variable. Then you can change it's content regardless the page in which you are.
Assuming this variable declaration ValueNotifier cartCounterNotifier = ValueNotifier(0);, you can update your AppBar action like this
appBar: AppBar(
// title and other
actions: [
ValueListenableBuilder(
valueListenable: cartCounterNotifier,
builder: (context, cartCounter, child) {
return Stack(
children: [
IconButton(
icon: Icon(Icons.shopping_cart_rounded),
onPressed: () {},
),
Positioned(
child: 0 < cartCounter ? Container(
decoration: ShapeDecoration(
color: Colors.red,
shape: CircleBorder(),
),
padding: EdgeInsets.all(2.0),
child: Text("$cartCounter", style: TextStyle(
color: Colors.white,
),),
) : SizedBox(),
),
],
);
}
)
],
),
Update:
So to update your action button, simply change the content of your variable like this
cartCounterNotifier.value = cartCounterNotifier.value + 1; // or another value