如何在Flutter中设置容器之间的空间中的光标设置?

hrirmatl  于 2023-04-07  发布在  Flutter
关注(0)|答案(1)|浏览(132)

我试图使一个计算器应用程序.为此,我希望光标是一个指针上的按钮和一个正常的箭头无处不在.但一旦,我使光标是一个指针上的按钮,它仍然是一个指针在按钮之间的空间.
代码如下:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.black,
        appBar: AppBar(
          backgroundColor: Colors.green[400],
          title: Text('Calculator'),
          centerTitle: true,
          titleTextStyle:
              TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),
        ),
        body: Column(
          children: [
            Container(
              color: Colors.amber[600],
              margin: EdgeInsets.fromLTRB(10, 10, 10, 10),
              width: 435,
              height: 300,
            ),
            MouseRegion(
              child: Container(
                color: Colors.blue[600],
                width: 435,
                height: 350,
                child: GridView.count(
                  crossAxisCount: 4,
                  children: [
                    MouseRegion(
                      child: Container(
                        width: 10,
                        height: 10,
                        color: Colors.black,
                        margin: EdgeInsets.all(10) ,
                      ),
                      cursor: SystemMouseCursors.click,
                      onExit: (event) => {},
                    ),
                    MouseRegion(
                      child: Container(
                        width: 10,
                        height: 10,
                        color: Colors.black,
                        margin: EdgeInsets.all(10),
                      ),
                      cursor: SystemMouseCursors.click,
                    ),
                    MouseRegion(
                      child: Container(
                        width: 10,
                        height: 10,
                        color: Colors.black,
                        margin: EdgeInsets.all(10),
                      ),
                      cursor: SystemMouseCursors.click,
                      
                    ),
                    MouseRegion(
                      child: Container(
                        width: 10,
                        height: 10,
                        color: Colors.black,
                        margin: EdgeInsets.all(10),
                      ),
                      cursor: SystemMouseCursors.click,
                     
                    ),
                    MouseRegion(
                      child: Container(
                        width: 10,
                        height: 10,
                        color: Colors.black,
                        margin: EdgeInsets.all(10),
                      ),
                      cursor: SystemMouseCursors.click,
                      
                    ),
                    MouseRegion(
                      child: Container(
                        width: 10,
                        height: 10,
                        color: Colors.black,
                        margin: EdgeInsets.all(10),
                      ),
                      cursor: SystemMouseCursors.click,
                     
                    ),
                  ],
                ),
              ),
              cursor: SystemMouseCursors.text,
            ),
          ],
        ),
      ),
    );
  }
}

我得到了这个结果:

但光标也在这些绿色标记的区域中发生变化。

我希望光标是正常的箭头在这些地区不点击一样,在黑色区域。和systemMouseCursor.text也没有影响。

h6my8fg2

h6my8fg21#

要将光标更改为按钮之间的普通箭头,可以使用GestureDetector小部件 Package GridView.count小部件,并将其行为属性设置为HitTestBehavior.translucent。这允许GestureDetector捕获手势并仅在手势位于GridView.count小部件内时更改光标。
下面是修改后的代码:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.black,
        appBar: AppBar(
          backgroundColor: Colors.green[400],
          title: Text('Calculator'),
          centerTitle: true,
          titleTextStyle:
              TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),
        ),
        body: Column(
          children: [
            Container(
              color: Colors.amber[600],
              margin: EdgeInsets.fromLTRB(10, 10, 10, 10),
              width: 435,
              height: 300,
            ),
            MouseRegion(
              child: GestureDetector(
                behavior: HitTestBehavior.translucent,
                child: Container(
                  color: Colors.blue[600],
                  width: 435,
                  height: 350,
                  child: GridView.count(
                    crossAxisCount: 4,
                    children: [
                      MouseRegion(
                        child: Container(
                          width: 10,
                          height: 10,
                          color: Colors.black,
                          margin: EdgeInsets.all(10) ,
                        ),
                        cursor: SystemMouseCursors.click,
                        onExit: (event) => {},
                      ),
                      MouseRegion(
                        child: Container(
                          width: 10,
                          height: 10,
                          color: Colors.black,
                          margin: EdgeInsets.all(10),
                        ),
                        cursor: SystemMouseCursors.click,
                      ),
                      MouseRegion(
                        child: Container(
                          width: 10,
                          height: 10,
                          color: Colors.black,
                          margin: EdgeInsets.all(10),
                        ),
                        cursor: SystemMouseCursors.click,
                        
                      ),
                      MouseRegion(
                        child: Container(
                          width: 10,
                          height: 10,
                          color: Colors.black,
                          margin: EdgeInsets.all(10),
                        ),
                        cursor: SystemMouseCursors.click,
                       
                      ),
                      MouseRegion(
                        child: Container(
                          width: 10,
                          height: 10,
                          color: Colors.black,
                          margin: EdgeInsets.all(10),
                        ),
                        cursor: SystemMouseCursors.click,
                        
                      ),
                      MouseRegion(
                        child: Container(
                          width: 10,
                          height: 10,
                          color: Colors.black,
                          margin: EdgeInsets.all(10),
                        ),
                        cursor: SystemMouseCursors.click,
                       
                      ),
                    ],
                  ),
                ),
              ),
              cursor: SystemMouseCursors.text,
            ),
          ],
        ),
      ),
    );
  }
}

通过此修改,光标将是按钮之间空间中的普通箭头和按钮上的指针。

相关问题