Flutter: disabling an IconButton with an asset as icon - flutter

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

Related

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

Unformatted BottomNavigationBar Flutter

I created a simple bottom navigation bar, (my code below)...
bottomNavigationBar: BottomNavigationBar(
backgroundColor: COLOR_WHITE,
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.account_circle_rounded),
label: 'Profile',
),
BottomNavigationBarItem(
label: 'something', icon: Icon(Icons.star)),
],
)
...but then I really wanted to click on the icons, so I tried adding an Icon Button for its onPressed() method.
bottomNavigationBar: BottomNavigationBar(
backgroundColor: COLOR_WHITE,
items: [
BottomNavigationBarItem(
icon: IconButton(
icon: const Icon(Icons.account_circle_rounded),
onPressed: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => ProfileScreen(userID :widget.userID)),
);
},
),
label: "Profile"
),
BottomNavigationBarItem(
label: 'something', icon: Icon(Icons.star)),
],
),
It gets all ugly, and I wanted the paddings and size all around to remain the same, but since I didn't add code to change those features, I don't get why it happened in the first place. Can someone help me solve it? Thanks!!
BottomNavigationBar has an onTap method you can use to trigger your callbacks so you don't need an IconButton. onTap gives you the index of the item tapped so you can map it to the appropriate page. You can also update the currentIndex property on the BottomNavigatorBar to highlight the appropriate item.
See this documentation for BottomNavigationBar for a good example: https://api.flutter.dev/flutter/material/BottomNavigationBar-class.html
the mistake you have made was in the first BottomNavigationBarItem you have IconButton Widget and in second Icon widget... both having different styles by default(flutter developers gave default values for padding size etc)... so below code will work. i implemented locally and checked as well..
BottomNavigationBar(
backgroundColor: Colors.white,
items: [
BottomNavigationBarItem(
icon: IconButton(
icon: const Icon(Icons.account_circle_rounded),
onPressed: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) =>
ProfileScreen(userID:widget.userID)),
);
},[enter image description here][1]
),
label: "Profile"
),
BottomNavigationBarItem(
label: 'something', icon: IconButton(
icon: const Icon(Icons.star),
onPressed: () {
},
),),
],
)
enter image description here

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 make IconButton bigger?

I'd like to make IconButton bigger, but this doesn't work.
How to make the size of the button bigger?
SizedBox(
height: 100,
width: 100,
child: IconButton(
tooltip: 'Refresh',
icon: const Icon(Icons.refresh),
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => Check()),
);
},
),
),
You can set the property iconSize.
Here is the official documentation.
Solution:
IconButton(
iconSize: 100,
tooltip: 'Refresh',
icon: const Icon(Icons.refresh),
onPressed: () {
//Your code goes here....
}
),
You can use the iconSize property...You dont need to use SizedBox when using iconSize
IconButton(
iconSize: 100,
icon: const Icon(Icons.refresh),
onPressed: () {}
),
You can use iconSize property of IconButton() widget, it will help to increase the size. This property increases the size of icon inside it too. So, put the size of Icon() as well so that you can have the desired size of IconButton and Icon both.
IconButton(
iconSize: 100,
tooltip: 'Refresh',
icon: const Icon(Icons.refresh, size:20),
onPressed: () {
},)
IconButton sets height and width internally, with nothing to do with any parent Widget.
IconButton(
iconSize: 100,
internally, IconButton sets the height and width of the icon as 100, and default iconSize is this.iconSize = 24.0,. That's important to make look icons good.

Remove click effect when IconButton() is clicked

I am currently using the following code to create an IconButton() in Flutter:
IconButton(
hoverColor: Colors.transparent,
color: _tweenButton.value,
icon: Icon(Icons.send),
onPressed: () => _isComposing ? _handleSubmitted(_textController.text) : null,
)
Whenever I click on the icon button, there is a splash/click effect as depicted in this screenshot. This widget is running inside MaterialApp() and Scaffold().
How can I deactivate this visual effect?
Use this code:
IconButton(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
color: _tweenButton.value,
icon: Icon(Icons.send),
onPressed: () => _isComposing ? _handleSubmitted(_textController.text) : null,
)