按下按钮时 flutter 更改文本

标签 flutter dart

伙计们,我会尝试在单击按钮时更改按钮上的文本...
我的代码:

         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/

相关文章:

dart - 实现 BLoC 的小部件是否一定是 StatefulWidgets?

firebase - 在 Flutter Firebase 应用程序中存储 API key ?我需要隐藏它们吗?如何?

flutter - 在简单应用程序中调用Navigator.push()时,_debugLocked断言失败

ios - ML 套件人脸识别不适用于 IOS

android - 如何在运行时正确更改视频播放器源?

flutter - 如何使用包built_value与Map <String,dynamic>进行序列化

flutter - Flutter 中是否可以在没有 MaterialApp 的情况下使用 GridView?

dart - 在 TextFormField Flutter 中使用 FocusNode

Android 依赖项 'android.arch.lifecycle:runtime' 的编译和运行时版本不同

dart - 如何确定我的变量是否应该在扩展 StatefulWidget 或 Starte<> 的类中?