如果按下按钮,文本不会改变 - FLUTTER

Text does not change if the button is pressed - FLUTTER

我正在尝试根据是否按下按钮来加密或解密字符串。 该算法之所以有效,是因为在我的控制台上我得到了正确的结果:按下按钮后,解密的文本就会按我的意愿显示。 但是这种变化并没有显示在屏幕上,实际上加密文本仍然存在并且没有发生任何变化。 如何确保按下 'Show Word List' 按钮后,我的文本会更改为显示解密的文本?

我在下面显示我的代码:

      String seedphrase = state.seedphrase;
      bool seedphraseforCrypted = true;
        
    
        if(SettingsPage.passwordCreated == true && seedphraseforCrypted == true) {
          seedphrase = AesEncryptionDecryption
              .encryptAES(
              seedphrase)
              .base64;
        }


     void decryptWordList() {
            setState(() {
              seedphrase = AesEncryptionDecryption.decryptAES(
                  seedphrase);
            });
     seedphraseforCrypted = false;
          }

      child: RichText(
              text: TextSpan(
                children: [
                  TextSpan(
                    text: seedphrase,
                      style: GoogleFonts.encodeSansSemiCondensed(fontSize: 18.0, fontWeight: FontWeight.w400,color: Palette.textColor)
                  ),
                  WidgetSpan(
                    child: Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 8.0),
                      child: Icon(
                        FeatherIcons.copy,
                        size: 25,
                      ),
                    ),
                  ),
                ],
              ),
            ),
    
     RoundedButton(
            text: 'Show Word List',
            selected: true,
            onPressed: ()  {
               decryptWordList();
             
            },
          ),

有人知道如何帮助我吗?谢谢!

  • 尝试在 decryptWordList 完成后在外部使用 setstate 并确保 seedphrase 值未被其他任何东西改变!
  • 你可以创建一个空的 flutter 项目,只使用你的方法,看看到底出了什么问题!

每当你设置状态时,密码确实被解密,但你在构建方法中的顶部有布尔检查,它总是设置为真。 创建另一个布尔值,检查是否解密或加密,在你的构建方法之外,默认是 true,当你想显示它时,将它设置为 false,你的函数将如下所示。

因此,在解密函数中,将此值更改为 false:


String seedphrase = state.seedphrase;
bool encrypted =true;  //Create this in your widget, directly before `Widget build...etc`
    
.
.
.
if(SettingsPage.passwordCreated == true && encrypted) {
  seedphrase = AesEncryptionDecryption
              .encryptAES(
              seedphrase)
              .base64;}
.
.
.
.
.
 void decryptWordList() {
            setState(() {
            seedphrase = AesEncryptionDecryption.decryptAES(seedphrase);
            encrypted =false; //Add this.
            });
          }