伙计们,我会尝试在单击按钮时更改按钮上的文本...
我的代码:
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 分析的建议,但不起作用......帮助!
最佳答案
您的类必须是有状态的 更改状态 事件的
变量也必须全局声明
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;
});
}
),
),
);
}
}
关于按下按钮时 flutter 更改文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59334552/