Flutter-Web: Mouse hover -> Change cursor to pointer

前端 未结 8 1545
走了就别回头了
走了就别回头了 2021-02-05 06:59

How can the cursor appearance be changed within Flutter? I know that with the Listener() Widget we can listen for Mouse-Events, but I haven\'t found any information regarding h

8条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 07:09

    Adapted answer by Constantin Stan

    For those who want to have the click effect similar to InkWell widget and with border radius option:

    Add to your pubspec.yaml file

    dependencies:
      universal_html: ^1.1.4
    

    Then add to the index.html file the following the tag as below:

    
    
    
      
      Your App Title
    
    
      
    
    
    

    Finally create the following widget and use encapsulated all the necessary widgets:

    import 'package:flutter/foundation.dart';
    import 'package:flutter/gestures.dart';
    import 'package:flutter/material.dart';
    import 'package:universal_html/prefer_sdk/html.dart' as html;
    
    class InkWellMouseRegion extends InkWell {
      InkWellMouseRegion({
        Key key,
        @required Widget child,
        @required GestureTapCallback onTap,
        double borderRadius = 0,
      }) : super(
              key: key,
              child: !kIsWeb ? child : HoverAware(child: child),
              onTap: onTap,
              borderRadius: BorderRadius.circular(borderRadius),
            );
    }
    
    class HoverAware extends MouseRegion {
    
      // get a reference to the body element that we previously altered 
      static final appContainer = html.window.document.getElementById('app-container');
    
      HoverAware({Widget child}) : super(
        onHover: (PointerHoverEvent evt) {
          appContainer.style.cursor='pointer';
          // you can use any of these: 
          // 'help', 'wait', 'move', 'crosshair', 'text' or 'pointer'
          // more options/details here: http://www.javascripter.net/faq/stylesc.htm
        },
        onExit: (PointerExitEvent evt) {
          // set cursor's style 'default' to return it to the original state
          appContainer.style.cursor='default';
        },
        child: child
      );
    
    }
    

提交回复
热议问题