You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter点击按钮后文本未更新:AES解密seedphrase后界面无变化如何解决

问题原因

你当前的核心问题是变量的定义位置和加密逻辑的执行时机错误:

  • 你把seedphrase、seedphraseforCrypted两个变量和加密判断逻辑都写在了build方法内部,每次调用setState触发界面刷新时,build方法都会重新执行,你在解密方法里修改的变量值会被build里的初始化+加密逻辑直接覆盖,所以界面始终显示加密后的内容。
修复方案

步骤1:调整变量定义位置

将两个变量从build方法中移出,改为State类的成员变量,加密逻辑移到initState生命周期中执行,只在页面初始化时做一次加密处理。

步骤2:调整解密方法的赋值逻辑

把状态修改逻辑都包裹在setState内,避免后续状态判断出错。

修正后的代码示例
class YourPageState extends State<YourPage> {
  // 变量定义在build方法外部,作为State类的成员变量
  late String seedphrase;
  bool seedphraseforCrypted = true;

  @override
  void initState() {
    super.initState();
    // 初始化时仅执行一次加密逻辑
    seedphrase = state.seedphrase;
    if(SettingsPage.passwordCreated == true && seedphraseforCrypted == true) {
      seedphrase = AesEncryptionDecryption
          .encryptAES(seedphrase)
          .base64;
    }
  }

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

  @override
  Widget build(BuildContext context) {
    // build方法内不要放变量初始化和加密逻辑
    return Column(
      children: [
        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();
          },
        ),
      ],
    );
  }
}

内容的提问来源于stack exchange,提问作者user16560224

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 02:27:04