I'm tiring for half a day now to change the foregroundColor and remove the backgroundColor of the button on hover, my code:
final ThemeData lightTheme = ThemeData(
menuButtonTheme: MenuButtonThemeData(style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith<Color?>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.hovered)) {
print("this never printed!!!");
return Colors.red;
}
return Colors.blue;
},
),
))
);
class AppScreen extends StatelessWidget {
final Widget? child;
AppScreen({Key? key, this.child}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AppBar Demo'),
actions: <Widget>[
PopupMenuButton(
itemBuilder: (context) => [
PopupMenuItem<int>(
value: 0,
child: Text("My Emails"),
),
PopupMenuItem<int>(
value: 1, child: Text("Sign Out"),
),
],
onSelected: (item) {},
child: TextButton.icon(
style: Theme.of(context).menuButtonTheme.style,
icon: Icon(Icons.account_circle),
label: Text(
'Maya Amor'
), onPressed: null,
),
)
],
),
body: child,
);
}
}
The color of the button never change, and when I hover on the button I can see the background of the button changing in a way I can not change, no matter what I did.
To change the
color
of aPopupMenuItem
when hovered , you can use theMouseRegion
widgetIn the
onHover
andonExit
functions, you can implement the logic to change thecolor
the
hover color
of a widget is often controlled by thetheme
of the app. If you want to disable thehover color
for a specific widget, you can wrap it in aTheme
widget and set thehoverColor
toColors.transparent
in theThemeData
Full Code :
OUTPUT :