2

I'm trying to change the color of the icon itself onHover instead of the splash(?)Circle thing around it,

IconButton(
onPressed: () {},
hoverColor:
Colors.green,
icon: const Icon(
Icons.edit)),

is there a way to do it, I'm new to flutter, but I have background in java/python.

2 Answers 2

2

To change the color of the Icon, you have to rely on a parameter, _isHovered for instance, which will be updated when the mouse enter the Icon region.

I see 2 options here, either using a MouseRegion + IconButton, or you can use an InkWell + Icon as previously suggested.

Full example for option 1:

import 'package:flutter/material.dart';

void main() {  
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light(),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _isHovered = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter app'),
      ),
      body: Center(
        child: MouseRegion(
          onEnter: (_) {
            setState(() {
              _isHovered = true;
            });
          },
          onExit: (_) {
            setState(() {
              _isHovered = false;
            });
          },
          child: IconButton(
            onPressed: () {},
            icon: Icon(Icons.edit, color: _isHovered ? Colors.green[700] : null)
          ),
        ),
      ),
    );
  }
}

For option 2 using InkWell, replace the previous build method by:

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('Flutter app'),
    ),
    body: Center(
      child: InkWell(
        onTap: () {},
        onHover: (hovered) {
          setState(() {
            _isHovered = hovered;
          });
        },
        child: Icon(Icons.edit, color: _isHovered ? Colors.green[700] : null),
      ),
    ),
  );
}

The difference I see here is the appearance of the button when hovered, the Icon will be green but the hover regions are not the same size. But this can be easily tweaked.

Sign up to request clarification or add additional context in comments.

Comments

0

You can define isIconHovered like this:

 bool isIconHovered = false;

then instead of IconButton use InkWell :

InkWell(
          onTap: () {},
          onHover: (value) {
            setState(() {
              isIconHovered = value;
            });
          },
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Icon(
              Icons.edit,
              color: isIconHovered ? Colors.green : null,
            ),
          ),
        ),

Note: in order to make hover work in InkWell, you have to call onTap too.

enter image description here enter image description here

5 Comments

I tried this, but when I hover, it doesn't change the color
could you please update you question with my answer?
where are you testing this? on web?
on windows desktop application.
@B-DK I updated my answer, some how on desktop app you have to use onTap and onHover together in order to make it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.