简体   繁体   中英

FloatingActionButton onPressed not triggering

hopefully a simple one here.

I have floating buttons that are animated. They do not seem to trigger my onPressed code, just seems to do nothing although I do get the "ViewPostImeInputStage processPointer 0" and 1 type events in the logs.

A button:

Widget dice() {
  return Container(
    child: FloatingActionButton(
      elevation: 5.0,
      backgroundColor: Theme.of(context).accentColor,
      heroTag: null,
      onPressed: _rollDice,
      tooltip: 'Inbox',
      child: SizedBox(
        height: 32,
        width: 32,
        child: Image.asset('lib/images/dice_button.png'),
      ),
    ),
  );
}

The method it is supposed to trigger:

_rollDice() {
  print('Dice pressed');
  int rng = new Random().nextInt(20);
  print(rng.toString());
}

The output from pressing the button three times:

D/ViewRootImpl@b9525ac[MainActivity](11404): ViewPostImeInputStage processPointer 0
D/ViewRootImpl@b9525ac[MainActivity](11404): ViewPostImeInputStage processPointer 1
D/ViewRootImpl@b9525ac[MainActivity](11404): ViewPostImeInputStage processPointer 0
D/ViewRootImpl@b9525ac[MainActivity](11404): ViewPostImeInputStage processPointer 1
D/ViewRootImpl@b9525ac[MainActivity](11404): ViewPostImeInputStage processPointer 0
D/ViewRootImpl@b9525ac[MainActivity](11404): ViewPostImeInputStage processPointer 1

The expected output:

Dice pressed
11
Dice pressed
2
Dice pressed
18

Anything silly I am doing here?

The issue here was that it was a button that had been transformed. While this should make absolutely no difference, there are a massive amount of threads about this exact issue. If you move a button by transforming it to a new location, the clickable region of the button remains in its ORIGINAL location.

Yes, I said that correctly. If you have a button in the top left of the screen, and you transform it to the bottom right, you will no longer be able to click that button in it's new location because the button thinks it is still in the top left.

This is absolutely retarded and unworkable.

The fix for me was to overlay some opacity widgets in the target location that are invisible, and have the onclick events trigger on these widgets instead. I have seen a post with a developer call this massive bug "working as intended", so don't expect this to be fixed any time soon even though it seems the entire community is calling for it.

If you are using a Stack in AnimatedBuider then wrap it with a big Container . A 200x200 container worked in my case. The size depends on your transitions. Also, You can place your button anywhere by changing the alignment property of the stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM