如何以编程方式模拟 Flutter 中按钮上的 onTap?

2023-12-09

例如:

// Update: This GestureDetector is embedded inside a third party package
// that will invoke a series of animation along with the onTap button
GestureDetector(
   onTap: () => print('Hey There!'),
   child: Widget1(),
)


// Then another place in the same screen
GestureDetector(
    onDoubleTap: () { 
           //Call the onTap of Widget1's GestureDetector
           print('I'm Here');
        }
    child: Widget2(),
)

我想要的是当用户双击时Widget2,它还会调用onTap回电Widget1.

更新: 所以我不想只调用传递给onTap of GestureDetector of Widget1,而是以编程方式点击onTapWidget1 的GestureDetector

我怎么做?


你可以做这样的事情 -

创建你的手势检测器 -

   GestureDetector gestureDetector = GestureDetector(
      onTap: () {
        setState(() {
          _lights = !_lights;
        });
      },
      child: Container(
        color: Colors.yellow.shade600,
        padding: const EdgeInsets.all(8),
        child: const Text('TURN LIGHTS ON'),
      ),
    );

创建一个按钮(或任何您想使用的小部件)来调用onTap在手势检测器上gestureDetector.onTap()就像您在另一个小部件上调用方法一样。 (我正在使用一个FlatButton here)-

          FlatButton(
            color: Colors.blue,
            textColor: Colors.white,
            disabledColor: Colors.grey,
            disabledTextColor: Colors.black,
            padding: EdgeInsets.all(8.0),
            onPressed: () {
              //Trigger the GestureDetector onTap event.
              gestureDetector.onTap();
            },
            child: Text("Click Here"),
          ),

现在您可以单击 FlatButton 来调用onTapGestureDetector 上的事件。

这是完整的示例 -

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Gesture Detector On Tap'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _lights = false;

  @override
  Widget build(BuildContext context) {
    GestureDetector gestureDetector = GestureDetector(
      onTap: () {
        setState(() {
          _lights = !_lights;
        });
      },
      child: Container(
        color: Colors.yellow.shade600,
        padding: const EdgeInsets.all(8),
        child: const Text('TURN LIGHTS ON'),
      ),
    );

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Container(
          alignment: FractionalOffset.center,
          color: Colors.white,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Icon(
                  Icons.lightbulb_outline,
                  color: _lights ? Colors.yellow.shade600 : Colors.black,
                  size: 60,
                ),
              ),
              gestureDetector,
              SizedBox(height: 50.0),
              FlatButton(
                color: Colors.blue,
                textColor: Colors.white,
                disabledColor: Colors.grey,
                disabledTextColor: Colors.black,
                padding: EdgeInsets.all(8.0),
                onPressed: () {
                  gestureDetector.onTap();
                },
                child: Text("Click Here"),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

你会得到这样的东西 -

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何以编程方式模拟 Flutter 中按钮上的 onTap? 的相关文章

随机推荐

  • 全栈 NestJS 应用程序中的 OAuth2 流程

    另一个 OAuth2 问题在其他地方没有完全涵盖 我使用 NestJS 后端 React 前端 Passport 和我自己的数据库进行身份验证 尝试添加一个 OAuth2 身份提供商 Google 我将 NestJS 应用程序配置为 OAu
  • Grails:更改 hibernate 方言时 SQL 语法错误

    我正在将 Grails 与 mySQL 数据库一起使用 并且尝试更改数据库引擎 据我研究 这可以最好地做到 dialect org hibernate dialect MyDialect 在 DataSource groovy 配置中 但是
  • 在 Regex/Javascript 中将一个字符交换为另一个字符

    我想做类似的事情 var a This is an A B pattern ABABA a replace A B a replace B A 并让它返回 gt 这是 B A 模式 BABAB 代替 gt 这是一个 A A 模式 AAAAA
  • ElasticSearch:我们可以在索引期间同时应用 n-gram 和语言分析器吗

    非常感谢 Random 我已将映射修改如下 为了进行测试 我使用 电影 作为索引类型 注意 我还添加了 search analyzer 如果没有这个 我就无法得到正确的结果 但是我对使用 search analyzer 有以下疑问 1 我们
  • Tapestry:字符编码问题

    我有一个 Tapestry 应用程序 它从表单中检索数据 将其写入数据库 然后显示结果 只要不使用特殊字符 一切都可以正常工作 例如 文本 Test 将导致 Test 我猜这个问题与错误的字符编码设置有关 Tapestry java 类 C
  • 删除多个实体上的表行拆分时出错

    我想删除拆分为两个实体的表行 如果我尝试删除主要实体 如果之前我没有使用以下命令加载相关的其他实体 则会收到错误消息context Entry Reference 当我要删除整行时 之前检索相关实体是不是有点愚蠢 如果我继续评论 我会收到以
  • 折叠表达式:替换特定类型但转发所有其他类型:如何实现这一点?

    我试图替换折叠表达式中的特定类型 同时简单地转发所有其他类型 但失败了 As std forward需要显式模板专门化我尝试提供另一组模板化重载 但是这些尚未考虑重载解决方案如果这有效的话 无论如何都会导致不明确的函数调用 第二次尝试是专门
  • 在 Python 3 中调用命令行参数

    我正在用 Python 3 编写一个程序 该程序将从 csv 文件中获取数据 对其进行调整并将新数据写入由用户命名的 csv 文件 我只是想了解命令行部分 我希望它能够以这种格式正确执行 program py input csv outpu
  • 如何在聚合物组件内部使用react?

    似乎可以在聚合物网络组件中使用 React 但我找不到工作示例 只能this 但似乎已经过时了 HTML
  • java 哪个布局管理器适合这个任务?

    我有一个 JPanel 父级 里面有 3 个 JPanel 子级 它们目前都使用 GridLayout 并共同表示一个 UML 类 问题是 当我添加新属性或方法时 所有 3 个 JPanel 都会增长到相同的大小 欲望行为是 无论何时添加方
  • 固定与绝对

    因此 我检查了该网站 发现一些问题与我正在寻找的内容很接近 但并不完全相同 我试图弄清楚如何定位元素 以便在滚动 向上 向下 时位置是固定的 但是当浏览器调整大小 向左 向右 时 位置是绝对或相对的 本质上 我想将一个元素放置在距浏览器窗口
  • C#,如何让图片背景透明?

    我有一个图片框 里面有一个png 然而 即使我将背景色设置为透明 它也不是透明的 有什么想法可能是错的吗 Thanks 我也遇到过有关透明图片的问题 你必须通过代码来绘制它 看我的问题图片框问题 EDIT In 绘画事件 包含背景图像的控件
  • 使用迭代器删除对象时出现 IllegalStateException

    我已经被这个错误困扰了一段时间 但我不知道问题出在哪里 我的代码是这样的 ArrayList
  • 使用 Gensim 提取短语时出错

    我正在尝试使用 Gensim 中的短语来获取句子中的二元组 如下所示 from gensim models import Phrases from gensim models phrases import Phraser documents
  • 用 javascript 获取真正的源代码?

    好吧 我对 js 的了解还不够 但是有没有办法用它来获取页面的真正源代码 例如 document body innerHTML 提供了某种 修复 版本 其中格式错误的标签已被删除 我猜想在原始页面上使用 XMLHttpRequest 可能会
  • 按钮背景颜色

    我需要改变background使用 C 代码 Visual Studio 2008 的按钮颜色 我看到有些人建议加入指令 using System Windows Media 我尝试了一下 它触发了此错误 Windows 不存在于names
  • 提取并列出匹配的单元格

    我试图比较包含公司名称的两列 A 和 B 找到任何完全匹配的名称 并将它们列在 C 列中 使用下面的代码 我没有收到错误 但什么也没有发生 如果有人能指出我正确的方向 我将不胜感激 Sub match Dim LastRow As Inte
  • Python——使用 beautifulsoup 抓取“展开”按钮中的内容

    我正在抓取一个黄页来获取一个城市所有物理治疗师的名字 通过该 url 我可以获得 50 名物理治疗师的列表 但是 当我展开页面时 该 url 不会改变 我如何获得完整的名单 这就是我获取罗斯托克市物理治疗师名单的方式 url https w
  • Io 异常:Oracle 云中的 Oracle 错误 ORA-12650

    我一直在为我的数据库计算机 Oracle 11g 使用 Oracle 云 PAAS Linux 服务器 并拥有可以运行所有 Java 应用程序的 Linux 应用程序服务器 假设我有基于 Spring 的 Web 应用程序 可以连接云数据库
  • 如何以编程方式模拟 Flutter 中按钮上的 onTap?

    例如 Update This GestureDetector is embedded inside a third party package that will invoke a series of animation along wit