【问题标题】:Flutter InkWell Hover FunctionFlutter InkWell 悬停功能
【发布时间】:2020-05-14 19:50:17
【问题描述】:

尝试使用 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(),
                        ),
                      )
                    ],
                  )
                ],
              ),
            ),
          ),
        );
      }
    }

【问题讨论】:

标签: flutter


【解决方案1】:

onHover() 定义onTap()。它会起作用的:

InkWell(
onTap: (){},
onHover: (val) {
setState(() {isHover = val;
      });
  },
),

【讨论】:

    【解决方案2】:

    试试这个:dartpad link

    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;
                });
              },
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 不工作。你认为是关于 Clipper 吗?,不打印值 btw
    • 不,刚刚删除了clipPath,代码和你的一样,不工作
    • 我把我的鳕鱼放在 dartpad 里就可以了,Link
    • 我可以看到你所有的小部件吗?
    • 也许它不适用于模拟器,因为你不能悬停在手机上,我的意思是你必须触摸屏才能合乎逻辑地做某事?
    【解决方案3】:

    尝试用带有颜色的“材料”小部件包装 InkWell:Colors.transparent

    Material(
      color: Colors.transparent,
      child: InkWell(
        ...
      )
    )
    

    【讨论】:

    • 没有打印 onHover: (val) { print('hover'); } ,, 这不起作用...
    猜你喜欢
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 2019-02-16
    • 2012-11-05
    相关资源
    最近更新 更多