[SOLVED] create button that pops up on hover


This Content is from Stack Overflow. Question asked by agingcabbage32

I’ve spent a lot of time trying to implement one thing, which is
a button that pops up in the center of a picture when user’s mouse enters the picture block and when mouse exits, button should disappear
I’ve tried using many different widgets, such as InkWell and MouseArea but sadly didn’t get expected behavior, my code currently is a total mess but maybe it’ll help you understand what I want to achieve:

Widget _getQRWidget(double height, double width) {
    var sizeCoefficient = 0.75;
    var size = width < height ? width * sizeCoefficient : height * sizeCoefficient;
    return ConstrainedBox(
      constraints: const BoxConstraints(
        minHeight: 300.0,
        minWidth: 300.0,
        maxHeight: 550.0,
        maxWidth: 550.0
      child: AnimatedContainer(
        duration: const Duration(seconds: 1),
        height: size,
        width: size,
              child: Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: _qrImage)),
                child: TextButton(
                  style: TextButton.styleFrom(
                    backgroundColor: Colors.grey,
                    padding: const EdgeInsets.all(16.0),
                    textStyle: const TextStyle(fontSize: 20),
                  child: InkWell(
                    child: const Text("press me"),
                    onHover: (isHovering) {
                      if (isHovering) {
                      } else {
                  onPressed: copyQrImage,
              ), // onTap: copyQrImage,

  void copyQrImage() {
    Pasteboard.writeImage(const Base64Decoder().convert(_payment.qrImage!));

  void getQrCopyButton() {
    //code to show the button

  void hideQrCopyButton() {
    //code to hide copy button


Instead of using AnimatedContainer, you could use AnimatedSwitcher which switches between a Button and a non-visible SizedBox whenever the onEnter or onExit event is called.

Just change the container to the picture you want to show.

Code example with DartPad:

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  State<MyApp> createState() => _MyAppState();

class _MyAppState extends State<MyApp> {
  bool showButton = false;

  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Material App Bar'),
        body: Center(
          child: MouseRegion(
            onEnter: (_) => setState(() {
              showButton = true;
            onExit: (_) => setState(() {
              showButton = false;
            child: Stack(
              alignment: Alignment.center,
              children: [
                  color: Colors.red[200],
                  width: 300,
                  height: 300,
                  duration: const Duration(milliseconds: 250),
                  child: showButton ? ElevatedButton(
                    onPressed: () => print("Hi!"),
                    child: const Text("Hello")
                  ) : const SizedBox.shrink(),

This Question was asked in StackOverflow by agingcabbage32 and Answered by TripleNine It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?