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

标签 flutter gesturedetector

例如:

// 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 时,它还会调用 Widget1onTap 回调。

更新: 所以我不想只调用传递到 Widget1GestureDetectoronTap 的函数,而是以编程方式点击 onTap Widget1 的 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'),
      ),
    );

创建一个按钮(或您想要使用的任何小部件)以在 GestureDetector gestureDetector.onTap() 上调用 onTap,就像在另一个小部件上调用方法一样。 (我在这里使用的是 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;
        });
      },
      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

关于flutter - 如何以编程方式模拟 Flutter 中按钮上的 onTap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63589071/

相关文章:

javascript - 如何离线运行 Flutter Web 应用程序?

user-interface - Flutter Gesture detector onTap 问题

android - 如何以不阻止滚动和点击它的子项目的方式在 Android ScrollView 中启用缩放?

Flutter error catch e.message 不工作

android - 工程路径中 "keystore.jks"文件的引用

flutter - 在小部件之间画一条线并使线可点击

android - Android 父子自定义 View 上的触摸事件处理

flutter - GestureDetector 在滚动时不接收事件

flutter - 哈希码可以与 <= => 符号进行比较吗?

flutter - 如何提供Future <String>以在Dart中流式传输需要String的流?