【问题标题】:How to Enable/Disable two Elevated Button in Flutter one after another?如何在 Flutter 中依次启用/禁用两个高架按钮?
【发布时间】:2022-09-23 14:08:45
【问题描述】:

如果单击 A 按钮,我有两个要禁用的提升按钮,如果单击 B 按钮则禁用 B 按钮,然后需要禁用按钮 A。

    标签: flutter dart button flutter-test dart-null-safety


    【解决方案1】:

    首先定义这些变量:

    bool isAactive = true;
    bool isBactive = true;
    

    然后像这样使用它:

    ElevatedButton(
                  onPressed: isAactive
                      ? () {
                          setState(() {
                            isBactive = false;
                          });
                        }
                      : null,
                  child: Text('A')),
    ElevatedButton(
                  onPressed: isBactive
                      ? () {
                          setState(() {
                            isAactive = false;
                          });
                        }
                      : null,
                  child: Text('B')),
    

    这是一个工作示例:

    class MyHomePage extends StatefulWidget {
      MyHomePage({
        Key? key,
      }) : super(key: key);
    
      @override
      _MyHomePageState createState() => new _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      bool isAactive = true;
      bool isBactive = true;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              ElevatedButton(
                  onPressed: isAactive
                      ? () {
                          setState(() {
                            isBactive = false;
                          });
                        }
                      : null,
                  child: Text('A')),
              ElevatedButton(
                  onPressed: isBactive
                      ? () {
                          setState(() {
                            isAactive = false;
                          });
                        }
                      : null,
                  child: Text('B')),
            ],
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-18
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 2019-06-13
      • 2015-05-05
      • 2021-05-18
      • 1970-01-01
      相关资源
      最近更新 更多