1

I'm new in Flutter. I want to make a simple example. I want to change color of the flat button while holding. Essentially I did but I'm not sure it's the right way. Isn't there an easier way? For example, could be a property like below;

higlightTextColor: Colors.white

In simple form the codes;

void _showAlertDialog(BuildContext context, Student item) {
showDialog(
  ...
  builder: (context) {
    bool _onHighlight = false;
    return StatefulBuilder(
      builder: (context, setState) {
        return AlertDialog(
          ...,
          content: Text(
            item._description + _onHighlight.toString(),
          ),
          actions: <Widget>[
            FlatButton(
              child: Text(
                'Okay',
              ),
              onPressed: () {},
              color: Colors.transparent,
              textColor: _onHighlight ? Colors.white : Colors.amber,
              splashColor: Colors.amber,
              shape: RoundedRectangleBorder(
                  side: BorderSide(
                      color: Colors.amber,
                      width: 1,
                      style: BorderStyle.solid)),
              onHighlightChanged: (value) {
                setState(() {
                  _onHighlight = value;
                });
              },
              // hoverColor: Colors.amber,
            ),
          ],
        );
      },
    );
  },
);

}

kursat sonmez
  • 818
  • 1
  • 12
  • 22
  • 1
    hey you can use [GestureDetector](https://api.flutter.dev/flutter/widgets/GestureDetector-class.html). On on hold or on long press event – Krish Bhanushali Aug 01 '20 at 07:43
  • Thanks but it not as I expected. So, if I use the GestureDetector, I have to use the `onLongPress` and `onLongPressEnd` methods like `onHighlightChanged` method on above. So I have to use a variable again. But, I wonder how to do it without variable. Also, has no background filling of GestureDetector like FlatButton ( or there is but I couldn't find :) ) – kursat sonmez Aug 01 '20 at 08:28
  • 1
    I arrived at the same conclusion, I don't think you can do it without a variable. Maybe you can use a variable of Color datatype but that's it. – Crazzygamerr Aug 01 '20 at 13:30
  • Hmm, I guess there is no other way than using variable. So, this example looks like the correct way. – kursat sonmez Aug 01 '20 at 21:20

0 Answers0