Skip to main content

Command Palette

Search for a command to run...

MouseRegion Widget and Attributes

Published
2 min read
MouseRegion Widget and Attributes
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

User interaction is a crucial aspect of any Flutter application, and it's essential to consider both touch and mouse inputs for a seamless experience across different platforms. The MouseRegion widget in Flutter provides a way to handle mouse-specific interactions, allowing you to enhance your application's responsiveness and user engagement. In this blog post, we'll explore the MouseRegion widget, its key attributes, and provide a comprehensive example to demonstrate its usage.

MouseRegion is a Flutter widget designed to handle mouse-specific interactions, such as hovering and entering. It allows developers to create responsive UIs that adapt to different input devices, providing a consistent experience for both mouse and touch users.

Attributes of MouseRegion:

  1. child (required):

    • The widget to which the mouse-related behaviors will be applied.
  2. cursor:

    • Defines the mouse cursor to be displayed when the pointer is over the region.
  3. onEnter:

    • A callback function that is triggered when the mouse pointer enters the region.
  4. onExit:

    • A callback function that is triggered when the mouse pointer exits the region.
  5. onHover:

    • A callback function that is triggered continuously while the mouse pointer is within the region.

Example:

// main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MouseRegion Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('MouseRegion Example'),
      ),
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            // First Container with MouseRegion
            MouseRegion(
              cursor: SystemMouseCursors.click,
              onEnter: (_) {
                print('Mouse entered Container 1');
              },
              onExit: (_) {
                print('Mouse exited Container 1');
              },
              child: Container(
                width: 150,
                height: 150,
                color: Colors.blue,
                child: Center(
                  child: Text('Container 1'),
                ),
              ),
            ),

            // Second Container with MouseRegion
            MouseRegion(
              cursor: SystemMouseCursors.click,
              onEnter: (_) {
                print('Mouse entered Container 2');
              },
              onExit: (_) {
                print('Mouse exited Container 2');
              },
              child: Container(
                width: 150,
                height: 150,
                color: Colors.green,
                child: Center(
                  child: Text('Container 2'),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Learn Flutter

Part 1 of 50

Explore Flutter's magic in crafting cross-platform apps effortlessly. Join the adventure!

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"