Flutter 如何以编程方式模拟颤振中按钮上的onTap?
例如:Flutter 如何以编程方式模拟颤振中按钮上的onTap?,flutter,gesturedetector,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
// 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
更新:
因此,我不想只调用传递到Widget1的GestureDetector的onTap
的onTap
中的函数,而是通过编程方式点击Widget1的GestureDetector的onTap
我该怎么做呢?只需单独设置第一个函数
void firstFunction(){
print('hey There!');
}
像这样,然后在第二个小部件中调用它
因此,您的代码将如下所示:
GestureDetector(
onTap: () => firstFunction(),
child: Widget1(),
)
// Then another place in the same screen
GestureDetector(
onDoubleTap: () {
firstFunction();
print('I'm Here');
}
child: Widget2(),
)
只需将第一个单独设置为函数
void firstFunction(){
print('hey There!');
}
像这样,然后在第二个小部件中调用它
因此,您的代码将如下所示:
GestureDetector(
onTap: () => firstFunction(),
child: Widget1(),
)
// Then another place in the same screen
GestureDetector(
onDoubleTap: () {
firstFunction();
print('I'm Here');
}
child: Widget2(),
)
更新:所以我不想只调用传递到Widget1的GestureDetector的onTap中的函数,而是通过编程方式点击Widget1的GestureDetector的onTap
onTap的目的是调用onTap内部的回调函数。所以我不知道为什么你只想点击按钮,而不想在点击按钮时调用应该调用的函数(你能详细说明一下吗?)
如果要模拟抽头进行测试,可以使用颤振驱动程序使用Driver.tap()
更新:所以我不想只调用传递到Widget1的GestureDetector的onTap中的函数,而是通过编程方式点击Widget1的GestureDetector的onTap
onTap的目的是调用onTap内部的回调函数。所以我不知道为什么你只想点击按钮,而不想在点击按钮时调用应该调用的函数(你能详细说明一下吗?)
如果要模拟抽头进行测试,可以使用颤振驱动程序使用Driver.tap()
这样做-
创建您的手势检测器-
GestureDetector gestureDetector = GestureDetector(
onTap: () {
setState(() {
_lights = !_lights;
});
},
child: Container(
color: Colors.yellow.shade600,
padding: const EdgeInsets.all(8),
child: const Text('TURN LIGHTS ON'),
),
);
在GestureDetectorGestureDetector.onTap()上创建一个按钮(或您想要使用的任何widget)来调用onTap
,就像在另一个小部件上调用方法一样。(我在这里使用的是FlatButton
)-
现在,您可以单击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"),
),
],
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
debugShowCheckedModeBanner:false,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“点击时的手势检测器”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
bool_lights=假;
@凌驾
小部件构建(构建上下文){
GestureDetector GestureDetector=GestureDetector(
onTap:(){
设置状态(){
_灯光=!\u灯光;
});
},
子:容器(
颜色:Colors.yellow.shade600,
填充:常量边集。全部(8),
子项:常量文本(“打开灯光”),
),
);
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:容器(
对齐:分馏loffset.center,
颜色:颜色,白色,
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
填充物(
填充:常数边集全部(8.0),
子:图标(
图标。灯泡的轮廓,
颜色:_lights?Colors.yellow.shade600:Colors.black,
尺码:60,
),
),
手势检测器,
尺寸箱(高度:50.0),
扁平按钮(
颜色:颜色,蓝色,
textColor:Colors.white,
禁用颜色:颜色。灰色,
disabledTextColor:Colors.black,
填充:边缘设置。全部(8.0),
已按下:(){
gestureDetector.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'),
),
);
在GestureDetectorGestureDetector.onTap()上创建一个按钮(或您想要使用的任何widget)来调用onTap
,就像在另一个小部件上调用方法一样。(我在这里使用的是FlatButton
)-
现在,您可以单击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"),
),
],
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
debugShowCheckedModeBanner:false,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“点击时的手势检测器”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
bool_lights=假;
@凌驾
小部件构建(构建上下文){
GestureDetector GestureDetector=GestureDetector(
onTap:(){
设置状态(){
_灯光=!\u灯光;
});
},
子:容器(
颜色:Colors.yellow.shade600,
填充:常量边集。全部(8),
子项:常量文本(“打开灯光”),
),
);