flutter - 按下按钮时颤动更改文本
问题描述
伙计们,我会尝试在单击按钮时更改文本...
我的代码:
bool pressGeoON = false;
bool cmbscritta = false;
RaisedButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
color: pressGeoON ? Colors.blue: Colors.red,
textColor: Colors.white,
child: cmbscritta ? Text("GeoOn"): Text("GeoOFF"),
// style: TextStyle(fontSize: 14)
onPressed: () {
setState(() => pressGeoON = !pressGeoON);
setState(() => cmbscritta = !cmbscritta);
},
)
dart Analisys 没有任何建议,但不起作用...帮助!
解决方案
你的班级必须是有状态的才能改变活动状态
变量也必须全局声明
class MyClass extends StatefulWidget {
@override
_MyClassState createState() => _MyClassState();
}
class _MyClassState extends State<MyClass> {
bool pressGeoON = false;
bool cmbscritta = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
color: pressGeoON ? Colors.blue : Colors.red,
textColor: Colors.white,
child: cmbscritta ? Text("GeoOn") : Text("GeoOFF"),
// style: TextStyle(fontSize: 14)
onPressed: () {
setState(() {
pressGeoON = !pressGeoON;
cmbscritta = !cmbscritta;
});
}
),
),
);
}
}
推荐阅读
- android - 添加 Kapt 插件后 - 执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障
- javascript - 如何在 v-Calendar 中格式化日期?
- javascript - 出现三个菜单图标而不是一个。(出现两个不成文的菜单图标 div)
- ruby-on-rails - Minimagick 不减少 png 文件大小而是增加
- php - Laravel - 获取根路径'/'以外的cookie
- networking - 在通过网络发送浮点数之前对浮点数进行位移的原因是什么
- npm - 维护两个主要版本的 NPM 包
- sql - 如何使用 HQL 替换 Hive 数据库中的行
- java - 可观察订阅未启动事件序列
- java - Java - 使用单个表情符号读取 UTF-8 文件