使用三元运算符启用按钮(&A) [英] Enable & Disable a button using ternary operators

查看:20
本文介绍了使用三元运算符启用按钮(&A)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在布尔值变为True时激活按钮。我有一个类,它持有被设置为FALSE的bool。 然后我有一个定制的小部件,在其中我有一个最终的bool,当我在主应用程序中调用小部件时,我将其设置为True。当最后一个布尔值为真时,我将类中的布尔值设置为真。然后我在按钮上使用三进制运算符。我正在尝试做的是,当用户回答最后一个问题时,应该启用提交按钮。 即使将类bool设置为True,按钮也不会激活。如果我刷新页面,按钮就会激活。我认为这是国家问题

附注:我删除了一些不必要的代码。

分数类

class Score {
  static int score = 0;
  
  static bool allQuestionsAnswered = false; //THIS VARIABLE WILL BE CHANGED BASED ON THE FINAL VARIABLE
}

带有提交按钮的主小工具

class Quiz extends StatefulWidget {
  const Quiz({Key? key}) : super(key: key);

  @override
  _QuizState createState() => _QuizState();
}

class _QuizState extends State<Quiz> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: Icon(Icons.arrow_back_rounded),
            onPressed: () {
              var route = ModalRoute.of(context);
              var name = route?.settings.name;

              if (name == null) {
                Score.score = 0;
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => Homepage()));
              }
            },
          ),
          title: const Text('CyberQuiz'),
        ),
        body: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: Column(
              children: [
                Text('Score: ' + Score.score.toString()),
                SizedBox(
                  height: 150,
                ),
                Text(
                  'Online Behaviour Questions',
                  style: TextStyle(
                    fontSize: 35,
                    color: Colors.white,
                  ),
                ),
                SizedBox(
                  height: 20,
                ),
                Text(
                  'Scale based answers, Strongly Disagree (1) to Agree (5)',
                  style: TextStyle(
                    fontSize: 20,
                    color: Colors.white,
                  ),
                ),
                SizedBox(
                  height: 50,
                ),
                OnlineBehaviour(
                    '6. I tend to use similar passwords for multiple accounts',
                    'Using the same password accross all online accounts, users give '
                        'hackers easy access to their whole digital life. It is like leaving '
                        'the keys under the doormat. If a hacker gains access to one user account,'
                        ' he or she can easily take over all online account and impersonate them',
                    false),
                SizedBox(
                  height: 80,
                ),
                OnlineBehaviour(
                    '7. I like posting stuff on social media to socialise with my friends',
                    'While it may seem like'
                        'the information is being share with your friends and family,'
                        'it can also be share with hackers and scammers who troll the social media sites',
                    false),
                SizedBox(
                  height: 80,
                ),
                OnlineBehaviour(
                    '8. I have a lot of accounts, thus I am using a notebook to write them down',
                    'Anyone can take your notebook '
                        'and access all of your personal data including bank account credentials'
                        '.There are many programs out there that act as a safe "journal for passwords"',
                    false),
                SizedBox(
                  height: 80,
                ),
                OnlineBehaviour(
                    '9. I tend to ignore requests from apps to access my location, files, camera etc.',
                    'Ignoring these requests might be very dangerous'
                        "as many online predators are in a continuous hunt of new people's location and their personal data."
                        'E.g. They can check whether your are home or not and they can schedule a potential robbery',
                    false),
                SizedBox(
                  height: 80,
                ),
                OnlineBehaviour(
                    '10. I accept all terms & conditions of all apps/websites without reading them',
                    'All of us are guilty for this one because a lot of websites/apps have loads of pages '
                        'talking about their conditions when accessing their services. If you ignore these they might'
                        'use the data stored on you in bad ways such as selling it to other '
                        'dodgy companies who might use your data in malicious ways',
                    true), //THIS IS WHERE I SET THE FINAL VARIABLE TO TRUE
                ElevatedButton(
                    style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all(Colors.orange)),
                    onPressed: Score.allQuestionsAnswered == true //THIS IS WHERE I USED TERNARY OPERATOR
                        ? () {
                            showDialog(
                                barrierDismissible: false,
                                context: context,
                                builder: (BuildContext context) {
                                  return AlertDialog(
                                    title: Text('Well done!'),
                                    content: Text(
                                        'Score: ' + Score.score.toString()),
                                    elevation: 24.0,
                                    shape: RoundedRectangleBorder(),
                                    actions: [
                                      TextButton(
                                          onPressed: () {
                                            Navigator.push(
                                                context,
                                                MaterialPageRoute(
                                                    builder: (context) =>
                                                        Homepage()));
                                          },
                                          child: Text('OK'))
                                    ],
                                  );
                                });
                          }
                        : null,
                    child: Text('Submit'))
              ],
            )));
  }
}

自定义小工具

class OnlineBehaviour extends StatefulWidget {
  final String behaviourQuestion;
  final String explanation;
  final bool lastQuestionAnswered;

  bool buttonTapped = false;

  bool questionsAnswered = false;

  OnlineBehaviour(
    this.behaviourQuestion,
    this.explanation,
    this.lastQuestionAnswered,
  );

  @override
  _OnlineBehaviourState createState() => _OnlineBehaviourState();
}

class _OnlineBehaviourState extends State<OnlineBehaviour> {
  disableButton() {
    setState(() {
      widget.questionsAnswered = true;
    });
  }

  activateSubmitButton() {
    setState(() {
      if (widget.lastQuestionAnswered == true) {
        Score.allQuestionsAnswered = true;  //FUNCTION THAT SETS THE BOOL IN THE SCORE CLASS TO TRUE
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(
          widget.behaviourQuestion,
          style: TextStyle(fontSize: 30, color: Colors.white),
        ),
        SizedBox(
          height: 20,
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ElevatedButton(
                style: ButtonStyle(
                    backgroundColor:
                        MaterialStateProperty.all(Color(0xFF45687b))),
                onPressed: widget.questionsAnswered == false
                    ? () {
                        setState(() {
                          Score.score += 5;
                          activateSubmitButton();
                          widget.buttonTapped = true;
                        });
                        if (widget.buttonTapped == true) {
                          disableButton();

                          print(Score.allQuestionsAnswered);
                        }
                      }
                    : null,
                child: Text(
                  '1',
                  style: TextStyle(
                    color: Color(0xFFe5e0e4),
                    fontSize: 15,
                    fontWeight: FontWeight.bold,
                  ),
                )),

推荐答案

我猜问题是即使在更新静态类上的值后,该按钮仍保持禁用状态。

如果这就是问题所在,那么这可能不是因为您未能更改值,也不是因为您的三元运算符错误,而只是因为您没有告诉主小部件再次检查静态类的值。为此,您应该更改setState方法中的值(您这样做了,但您使用的是自定义小部件的setState方法,这不会更新主小部件)。

有很多方法可以尝试和更改这一点,但我要做的是向您的自定义小部件声明一个新参数onAllQuestionsAnswered

在自定义小工具上

  final String behaviourQuestion;
  final String explanation;
  final bool lastQuestionAnswered;
  final VoidCallback onAllQuestionsAnswered; // <--

然后,当您设置静态类的值时,您可以这样做:

if (widget.lastQuestionAnswered == true) {
  onAllQuestionsAnswered();
}

最后,在您的主小部件上,您将传递以下回调:

OnlineBehaviour(
...
onAllQuestionsAnswered: () => setState(() =>Score.allQuestionsAnswered = true),
);

一般来说,您也可以完全删除静态变量,只将状态存储在主小部件上,但这超出了本答案的范围。

这篇关于使用三元运算符启用按钮(&A)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆