gpt4 book ai didi

Flutter GectureDetector onTap 仅适用于文本子项

转载 作者:行者123 更新时间:2023-12-05 09:11:55 27 4
gpt4 key购买 nike

我尝试创建一个应用程序,当用户点击屏幕上的任意位置时,它会将背景颜色更改为随机颜色,但 OnTap 功能仅在我点击文本时才有效。请帮我解决一下。

这是我的代码:

import 'package:flutter/material.dart';
import 'dart:math';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MainVidget();
}
}

class MainVidget extends StatefulWidget {
@override
MainVidgetState createState() => MainVidgetState();
}

class MainVidgetState extends State<MainVidget> {
Color mainColor = Colors.white;

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
backgroundColor: mainColor,
appBar: AppBar(
title: Text('Tap anywhere'),
),
body: GestureDetector(
onTap: () {
setState(() {
mainColor = Color.fromRGBO(Random().nextInt(254) + 1,
Random().nextInt(254) + 1, Random().nextInt(254) + 1, 1);
});
},
child: Center(
child: Text('Hey there', style: TextStyle(fontSize: 32.0)),
),
),
));
}
}

最佳答案

GestureDetector 中设置行为:HitTestBehavior.translucent

GestureDetector(
behavior: HitTestBehavior.translucent, //or HitTestBehavior.opaque
onTap: () {
setState(() {
mainColor = Color.fromRGBO(Random().nextInt(254) + 1,
Random().nextInt(254) + 1, Random().nextInt(254) + 1, 1);
});
},
child: Center(
child: Text('Hey there', style: TextStyle(fontSize: 32.0)),
),
),

关于Flutter GectureDetector onTap 仅适用于文本子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59426143/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com