flutter - 颤振背景颜色
问题描述
我需要在点击时更改背景颜色,并在屏幕中间添加一些文本,但我不知道放置这些文本的最佳方式是什么,我这样做了:`
main() => runApp(
const Directionality(
textDirection: TextDirection.ltr,
child: MyApp(),
),
);
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Color color =
Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
@override
Widget build(BuildContext context) {
return Container(
color: color,
child: GestureDetector(
onTap: () {
setState(
() {
color = Color((Random().nextDouble() * 0xFFFFFF).toInt())
.withOpacity(1.0);
},
);
},
child: const Center(child: Text('tttt')),
),
);
}
}`
并且它在没有这条线的情况下改变了 bg child: const Center(child: Text('tttt'))
,但它只是停止工作,所以我该怎么做才能正确地做到这一点?
解决方案
发生这种情况是因为您使用手势检测器包装了文本,因此,当您单击文本时,它会更改背景颜色。
要解决这个问题,只需Container
用GestureDetector
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
setState(
() {
color = Color((Random().nextDouble() * 0xFFFFFF).toInt())
.withOpacity(1.0);
},
);
},
child: Container(color:color,child:child),
),
);
}
}`
推荐阅读
- sapui5 - 如何访问 TextArea 的文本和/或更改它?
- javascript - Promise 未处理异步异常
- scala - IntelliJ(或 sbt-structure)如何识别测试源?
- android - 如何使用 ObjectAnimator 使用 path.addCircle 在圆形路径中移动视图对象?
- ios - 查找多个窗口的快捷方式
- c++ - 铸造不起作用(在 gcc 更新之后?)
- excel - 找到 'TargetTable.Range.SpecialCells(xlCellTypeVisible).Copy _ ' 目的地:=Sheets("Sheet8").Range("A1")
- ruby - 仅当它们的值设置为 true 时,才将哈希键转换为数组的最佳方法
- windows - 使用 QSettings 在 Windows 上读取注册表值
- python - Python 语法错误与“