flutter - 图标的悬停颜色
问题描述
我正在尝试在 Flutter 中实现一个最喜欢的 Button。单击按钮时我对属性感到满意,但对悬停动画不满意。
我希望只有图标颜色在悬停时发生变化。我不喜欢 IconButton 周围的气泡。这是我的方法:
MouseRegion FavoriteButton(int index) {
bool _favoriteHover = false;
return MouseRegion(
onHover: (e) {
setState(() {
_favoriteHover = true;
});
},
child: IconButton(
icon: Icon(
_projects[index].favorite
? Icons.favorite
: Icons.favorite_border_outlined,
color: _favoriteHover ? Colors.yellow : Colors.white,
),
onPressed: () {
setState(() {
_projects[index].favorite
? _projects[index].favorite = false
: _projects[index].favorite = true;
// TODO: Save Favorite state to config
});
},
color: Colors.transparent,
),
);
不幸的是,图标颜色在悬停时不会改变。有人可以帮忙吗?
解决方案
你试试:
/// Flutter code sample for MouseRegion
// This example makes a [Container] react to being entered by a mouse
// pointer, showing a count of the number of entries and exits.
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const Center(
child: MyStatefulWidget(),
),
),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
bool _favorite = false;
void _incrementEnter(PointerEvent details) {
setState(() {
_favorite = true;
});
}
void _incrementExit(PointerEvent details) {
setState(() {
_favorite = false;
});
}
Icon setIcon(){
if(_favorite){
return Icon(Icons.favorite);
}
return Icon(Icons.favorite_border_outlined);
}
Color setColor(){
if(_favorite){
return Colors.yellow;
}
return Colors.black;
}
@override
Widget build(BuildContext context) {
return Container(
child: MouseRegion(
onEnter: _incrementEnter,
onExit: _incrementExit,
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
icon: setIcon(),
color: setColor(),
onPressed: (){},
),
],
),
),
),
);
}
}
[1]: https://i.stack.imgur.com/UyW2j.png
推荐阅读
- html - 离子范围垃圾邮件和泛滥控制台无法正常工作
- javascript - 将动态操作添加到动态创建的按钮
- javascript - 在javascript中将字符串或字符转换为十六进制代码
- python - 列出函数指向最后一个内存地址的返回
- php - WordPress:如何使用两个元键在其存档页面上订购自定义帖子?
- angular - Microsoft Graph API 返回 200 ok 错误响应
- c++ - 如何使用 C++ 处理 excel 文件?
- linux - 为什么平台设备需要注册为另一个设备?
- latex - 启动winedt时如何保持以前打开的.tex文件保持打开状态
- react-native - 如何为 react native expo 设置 Constants.installationId?