如何在Flutter中以编程方式模拟onTap?

问题描述

例如:

// 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的{​​{1}}回调。

更新: 因此,我不想只是调用传递到Widget1的{​​{1}}的{​​{1}}的函数,而是以编程方式点击Widget1的onTap

我该怎么做?

解决方法

您可以执行以下操作-

创建手势检测器-

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

创建一个按钮(或您要使用的任何小部件)以在GestureDetector onTap上调用gestureDetector.onTap(),就像在另一个小部件上调用method一样。 (我在这里使用FlatButton)-

          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来调用GestureDetector上的onTap事件。

这是完整的示例-

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;
        });
      },);

    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,onPressed: () {
                  gestureDetector.onTap();
                },],);
  }
}

您将获得类似的内容-

enter image description here

,

只需单独制作第一个功能

for (i in seq_along(files)) {

像这样,然后在第二个小部件中调用它 因此您的代码将如下所示:

WKWebView
,

更新:因此,我不想仅调用传递到Widget1的GestureDetector的onTap中的函数,而是以编程方式点击Widget1的GestureDetector的onTap

onTap的目的是在onTap内部调用回调函数。因此,我不确定为什么除了调用该按钮时应调用的功能之外,为什么只想单击该按钮(您能对此进行详细说明吗?)。

如果要模拟抽头进行测试,可以使用Flutter Driver使用driver.tap()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...