[在此处输入图片描述] [1]我正在尝试创建一个屏幕测试应用,该应用可以显示屏幕的哪些部分对触摸没有响应。 这应该是一个非常简单的单屏幕应用程序,其中包含可以单击以测试屏幕的符号表。
我有可用的符号表,但是找不到单击时如何使符号切换。 如何在不构建几乎100个单独的变量(或数组)的情况下使每个图标具有其自己的切换。 图标可以具有属性吗?
尝试使用onTap时出现以下错误(这是我从Flutter演示应用程序获得的,但在这种情况下无法正常工作。)
编译器消息: lib / main.dart:33:32:错误:在此之前应为']'。 onTap:(){ ^ lib / main.dart:33:27:错误:未为类“ _MyAppState”定义吸气剂“ onTap”。
'_ MyAppState'来自'package:app1 / main.dart'('lib / main.dart')。 尝试将名称更正为现有吸气剂的名称,或定义一个名为“ onTap”的吸气剂或字段。 onTap:(){ ^^^^^
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
double iconSize = 40;
@override
Widget build(BuildContext context) {
var toggledOn = false;
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Screen Tester'),
),
body: Center(
child: Column(children: <Widget>[
Container(
margin: EdgeInsets.all(10),
child: Table(
border: TableBorder.all(),
children: [
TableRow( children: [
Column(children:[
Icon(Icons.adjust, size: iconSize,
color: toggledOn ? Colors.green : Colors.blue),
onTap: () {
setState(() {
if (toggledOn = false) {
toggledOn = true;
} else {
toggledOn = false;
}
});
}
]),
Column(children:[
Icon(Icons.adjust, size: iconSize,
color: toggledOn ? Colors.green : Colors.red),
]),
Column(children:[
Icon(Icons.adjust, size: iconSize,
color: toggledOn ? Colors.green : Colors.red),
]),
Column(children:[
Icon(Icons.adjust, size: iconSize,
color: toggledOn ? Colors.green : Colors.red),
]),
Column(children:[
Icon(Icons.adjust, size: iconSize,
color: toggledOn ? Colors.green : Colors.red),
])
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,
color: toggledOn ? Colors.green : Colors.red),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
TableRow( children: [
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
Icon(Icons.adjust, size: iconSize,),
]),
],
),
),
]))),
);
}
}```
[1]: https://i.stack.imgur.com/O019z.png
答案 0 :(得分:0)
您希望IconButton
能够使用与onTap
,onPressed
等效的名称。 onTap
参数仅在窗口小部件可用时才能传递,不能按需使用。
IconButton
将采用您现有的Icon
,并允许您添加onPressed
回调。
例如。
IconButton(
icon: Icon(Icons.adjust, size: iconSize, color: toggledOn ? Colors.green : Colors.blue),
onPressed: () {
setState(() {
if (!toggledOn) {
toggledOn = true;
} else {
toggledOn = false;
}
});
},
)
简单地跟踪编译器错误将比在SO上询问要快得多。它们通常很容易修复,尤其是与运行时错误相比。
编辑:
此外,您不能在toggledOn
方法内声明build
。将其移至build
之外以防止其返回到每个build
的默认值。将其右移到iconSize
旁边。