【问题标题】:Flutter - Custom button tap areaFlutter - 自定义按钮点击区域
【发布时间】:2019-01-13 19:15:23
【问题描述】:

我正在构建一个 Flutter 应用程序,其中大部分屏幕将被圆形按钮占据。

我已经尝试了几种不同的方法来创建一个圆形按钮,但我总是遇到同样的问题:“可点击”区域实际上不是圆形,而是矩形。

这是一个使用FloatingActionButton 获得的示例:

对于小尺寸按钮来说,这并不是一个真正的问题——我什至会说它在某种程度上很有帮助——但就我而言,这很烦人。

所以我的问题是:是否可以将“可点击”区域限制为一个圆圈?

提前致谢。

【问题讨论】:

    标签: dart flutter flutter-layout


    【解决方案1】:

    这似乎行得通,我不知道这样做是否正确,或者是否有更好的方法,但你去吧。

    class Test extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          body: Container(
            color: Colors.blue,
            child: Center(
              child: GestureDetector(
                onTap: () {
                  print('clicky');
                },
                child: ClipOval(
                  child: Container(
                    width: 200,
                    height: 200,
                    color: Colors.red,
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 谢谢,这正是我想要的。
    • 对于材质效果,将 GestureDetector 替换为 InkWell 与 onTap()。
    【解决方案2】:

    如果您希望在点击期间保持飞溅,您可以执行以下操作:

        Material(
                  color: Colors.green,
                  shape: CircleBorder(),
                  elevation: 5.0,
                  child: InkWell(
                    borderRadius: BorderRadius.circular(100.0),
                    onTap: () => print("here"),
                    child: Container(
                      height: 200.0,
                      width: 200.0,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                      ),
                      child: Icon(Icons.receipt),
                    ),
                  ),
                ),
    

    【讨论】:

      猜你喜欢
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      相关资源
      最近更新 更多