Flutter InkWell 悬停功能

Emi*_*gün 6 flutter

尝试使用 InkWell 函数(onHover)放大我的图片(用 ClipPath 剪辑),但是当我将鼠标悬停在图片上时没有任何反应。

InkWell(
                        onTap: () {},
                        onHover: (isHovering) {
                          if (isHovering) {
                            setState(() {
                              sizeBool = !sizeBool;
                            });
                          }
                        },
                        child: ClipPath(
                          child: AnimatedContainer(
                            duration: Duration(seconds: 1),
                            width: MediaQuery.of(context).size.width,
                            height: sizeBool ? 450 : 150,
                            child: Image.network(
                              'image.url',
                              fit: BoxFit.cover,
                            ),
                          ),
                          clipper: CustomClipPath(),
                        ),
                      )
                    ],
                  )
                ],
              ),
            ),
          ),
        );
      }
    }
Run Code Online (Sandbox Code Playgroud)

小智 21

定义onTap()onHover(). 它将起作用:

InkWell(
onTap: (){},
onHover: (val) {
setState(() {isHover = val;
      });
  },
),
Run Code Online (Sandbox Code Playgroud)


M.B*_*M.B 8

试试这个:dartpad 链接

import 'package:flutter/material.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> {
  double sideLength = 50;

  Widget build(BuildContext context) {
    return AnimatedContainer(
      height: sideLength,
      width: sideLength,
      duration: Duration(seconds: 2),
      curve: Curves.easeIn,
      child: Material(
        color: Colors.yellow,
        child: InkWell(
          onTap:(){},
            onHover: (value) {
              print(value);
            setState(() {
              sideLength = value?150 :50;
            });
          },
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 也许它不适用于模拟器,因为您无法将鼠标悬停在手机上,我的意思是您必须触摸屏幕才能逻辑地执行某些操作? (4认同)