Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter web - On hover how to change Flatbutton TEXT color

Hi im working on Flutter web and when i hover flatbutton i wanna change the text color. Its on hover not on pressed. But how do i detect/know its been hovered, so i can manage the state color. Thanks

FlatButton(
              color: Colors.white, 
              textColor: Colors.teal[700], //when hovered text color change
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5),
                side: BorderSide(
                  color: Colors.teal[700],
                ),
              ),
              onPressed: () {},
              child: Text("Log in"),
            ),
like image 329
Raine Dale Holgado Avatar asked Aug 12 '20 07:08

Raine Dale Holgado


3 Answers

You can copy paste run full code below
You can use MouseRegion's onHover attribute

code snippet

void _incrementExit(PointerEvent details) {
    setState(() {
      textColor = Colors.blue;
      _exitCounter++;
    });
  }

void _updateLocation(PointerEvent details) {
    setState(() {
      textColor = Colors.red;
      x = details.position.dx;
      y = details.position.dy;
    });
  }
  
return MouseRegion(
      onEnter: _incrementEnter,
      onHover: _updateLocation,
      onExit: _incrementExit,
      child: FlatButton(
        color: Colors.white,
        textColor: Colors.teal[700], //when hovered text color change
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5),

working demo

enter image description here

full code

import 'package:flutter/material.dart';

import 'package:flutter/widgets.dart';

void main() => runApp(MyApp());

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  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: Center(
          child: MyStatefulWidget(),
        ),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  Color textColor = Colors.blue;
  int _enterCounter = 0;
  int _exitCounter = 0;
  double x = 0.0;
  double y = 0.0;

  void _incrementEnter(PointerEvent details) {
    setState(() {
      _enterCounter++;
    });
  }

  void _incrementExit(PointerEvent details) {
    setState(() {
      textColor = Colors.blue;
      _exitCounter++;
    });
  }

  void _updateLocation(PointerEvent details) {
    setState(() {
      textColor = Colors.red;
      x = details.position.dx;
      y = details.position.dy;
    });
  }


  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      onEnter: _incrementEnter,
      onHover: _updateLocation,
      onExit: _incrementExit,
      child: FlatButton(
        color: Colors.white,
        textColor: Colors.teal[700], //when hovered text color change
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5),
          side: BorderSide(
            color: Colors.teal[700],
          ),
        ),
        onPressed: () {},
        child: Text("Log in", style: TextStyle(color: textColor),),
      ),
    );
  }
}
like image 198
chunhunghan Avatar answered Nov 15 '22 11:11

chunhunghan


You can change foregroundColor property of Button Style like this:

ElevatedButton.styleFrom().copyWith(
      backgroundColor: MaterialStateProperty.resolveWith<Color?>(
        (states) {
          if (states.contains(MaterialState.hovered)) {
            return Colors.blue;
          } else if (states.contains(MaterialState.pressed)) {
            return Colors.yellow;
          }
          return Colors.red;
        },
      ),
      foregroundColor: MaterialStateProperty.resolveWith<Color?>(
        (states) {
          if (states.contains(MaterialState.hovered)) {
            return Colors.green;
          }
          return Colors.black;
        },
      ),
    );
like image 29
Adam Smaka Avatar answered Nov 15 '22 11:11

Adam Smaka


For the textButton we can use foregroundColor property of ButtonStyle.

TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.focused))
  return Colors.red;
if (states.contains(MaterialState.hovered))
    return Colors.green;
if (states.contains(MaterialState.pressed))
    return Colors.blue;
return Colors.yellow; // null throus error in flutter 2.2+.
}),
),
onPressed: () { },
child: Text('TextButton with custom overlay colors'),
)
like image 32
Deekshith Xetty Avatar answered Nov 15 '22 11:11

Deekshith Xetty