MouseRegion class

A widget that tracks the movement of mice, even when no button is pressed.

It does not listen to events that can construct gestures, such as when the pointer is pressed, moved, then released or canceled. For these events, use Listener, or more preferably, GestureDetector.

Layout behavior

See BoxConstraints for an introduction to box layout models.

If it has a child, this widget defers to the child for sizing behavior. If it does not have a child, it grows to fit the parent instead.

This example makes a Container react to being entered by a mouse pointer, showing a count of the number of entries and exits.
import 'package:flutter/widgets.dart';

// ...

int _enterCounter = 0;
int _exitCounter = 0;
double x = 0.0;
double y = 0.0;

void _incrementEnter(PointerEvent details) {
  setState(() {
    _enterCounter++;
  });
}
void _incrementExit(PointerEvent details) {
  setState(() {
    _exitCounter++;
  });
}
void _updateLocation(PointerEvent details) {
  setState(() {
    x = details.position.dx;
    y = details.position.dy;
  });
}

@override
Widget build(BuildContext context) {
  return Center(
    child: ConstrainedBox(
      constraints: new BoxConstraints.tight(Size(300.0, 200.0)),
      child: MouseRegion(
        onEnter: _incrementEnter,
        onHover: _updateLocation,
        onExit: _incrementExit,
        child: Container(
          color: Colors.lightBlueAccent,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('You have entered or exited this box this many times:'),
              Text(
                '$_enterCounter Entries\n$_exitCounter Exits',
                style: Theme.of(context).textTheme.display1,
              ),
              Text(
                'The cursor is here: (${x.toStringAsFixed(2)}, ${y.toStringAsFixed(2)})',
              ),
            ],
          ),
        ),
      ),
    ),
  );
}

See also:

  • Listener, a similar widget that tracks pointer events when the pointer have buttons pressed.
Inheritance

Constructors

MouseRegion({Key key, PointerEnterEventListener onEnter, PointerExitEventListener onExit, PointerHoverEventListener onHover, Widget child })
Creates a widget that forwards mouse events to callbacks.
const

Properties

onEnter PointerEnterEventListener
Called when a mouse pointer (with or without buttons pressed) enters the region defined by this widget, or when the widget appears under the pointer.
final
onExit PointerExitEventListener
Called when a mouse pointer (with or without buttons pressed) leaves the region defined by this widget, or when the widget disappears from under the pointer.
final
onHover PointerHoverEventListener
Called when a mouse pointer (with or without buttons pressed) changes position, and the new position is within the region defined by this widget.
final
child Widget
The widget below this widget in the tree. [...]
final, inherited
hashCode int
The hash code for this object. [...]
read-only, inherited
key Key
Controls how one widget replaces another widget in the tree. [...]
final, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

createElement() → _ListenerElement
RenderObjectWidgets always inflate to a RenderObjectElement subclass.
override
createRenderObject(BuildContext context) RenderMouseRegion
Creates an instance of the RenderObject class that this RenderObjectWidget represents, using the configuration described by this RenderObjectWidget. [...]
override
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node. [...]
override
updateRenderObject(BuildContext context, covariant RenderMouseRegion renderObject) → void
Copies the configuration described by this RenderObjectWidget to the given RenderObject, which will be of the same type as returned by this object's createRenderObject. [...]
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children. [...]
@protected, inherited
didUnmountRenderObject(covariant RenderObject renderObject) → void
A render object previously associated with this widget has been removed from the tree. The given RenderObject will be of the same type as returned by this object's createRenderObject.
@protected, inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toDiagnosticsNode({String name, DiagnosticsTreeStyle style }) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep. [...]
inherited
toString({DiagnosticLevel minLevel: DiagnosticLevel.debug }) String
Returns a string representation of this object.
inherited
toStringDeep({String prefixLineOne: '', String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug }) String
Returns a string representation of this node and its descendants. [...]
inherited
toStringShallow({String joiner: ', ', DiagnosticLevel minLevel: DiagnosticLevel.debug }) String
Returns a one-line detailed description of the object. [...]
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(dynamic other) bool
The equality operator. [...]
inherited