Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振-设置状态未更新特定小部件_Flutter_Dart_Flutter Layout_Flutter Animation - Fatal编程技术网

Flutter 颤振-设置状态未更新特定小部件

Flutter 颤振-设置状态未更新特定小部件,flutter,dart,flutter-layout,flutter-animation,Flutter,Dart,Flutter Layout,Flutter Animation,我试图在点击屏幕的基础上更新两个不同的文本。这对其中一个文本有效,但对另一个文本无效。我指的是TyperAnimatedTextKit小部件和它下面的文本小部件。前者不起作用,但后者起作用 正如下面的代码片段所示,当点击屏幕时,有两个小部件需要更改。一个是文本小部件。点击屏幕时,它会将字符串更新为另一个类”(quoteBrain.getQuoteAuthor())中的值。这实际上起作用并成功更改屏幕上的文本。点击屏幕会导致调用ScreenStapped()方法,该方法更新状态并从quoteBra

我试图在点击屏幕的基础上更新两个不同的文本。这对其中一个文本有效,但对另一个文本无效。我指的是TyperAnimatedTextKit小部件和它下面的文本小部件。前者不起作用,但后者起作用

正如下面的代码片段所示,当点击屏幕时,有两个小部件需要更改。一个是文本小部件。点击屏幕时,它会将字符串更新为另一个类”(quoteBrain.getQuoteAuthor())中的值。这实际上起作用并成功更改屏幕上的文本。点击屏幕会导致调用ScreenStapped()方法,该方法更新状态并从quoteBrain类中获取下一个字符串

然而我也在使用动画文本工具包包中的TyperAnimatedTextKit。尽管代码使用与文本小部件相同的逻辑(即从另一个类中获取字符串并调用函数进行更新),但这永远不起作用

我想知道是否有人能提供一些见解

非常感谢并乐意提供更多细节

import 'package:flutter/material.dart';
import 'quote_brain.dart';
import 'package:animated_text_kit/animated_text_kit.dart';

QuoteBrain quoteBrain = QuoteBrain();

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  bool buttonVisible = true;

  void ScreenTapped() {
    setState(() {
      quoteBrain.NextQuote();
      buttonVisible = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          fit: BoxFit.fill,
          image: AssetImage('images/nightsky.png'),
        ),
      ),
      child: GestureDetector(
        onTap: () {
          print('Screen was tapped');
          ScreenTapped();
        },
        child: Scaffold(
          backgroundColor: Colors.transparent,
          body: Column(
            children: <Widget>[
              Expanded(
                child: Container(
                  alignment: Alignment.center,
                  margin: EdgeInsets.all(30),
                  padding: EdgeInsets.symmetric(vertical: 150),
                  child: Column(
                    children: <Widget>[
                      TyperAnimatedTextKit(
                        isRepeatingAnimation: false,
                        text: [quoteBrain.getQuoteText()],
                        textStyle: TextStyle(fontFamily: 'QuiteMagical', fontSize: 50, color: Colors.white),
                      ),
                      Text(
                        quoteBrain.getQuoteAuthor(),
                        style: TextStyle(fontFamily: 'QuiteMagical', fontSize: 50, color: Colors.white),
                      ),
                      SizedBox(height: 54),
                      Visibility(
                        visible: buttonVisible,
                        child: FadeAnimatedTextKit(
                          repeatForever: false,
                          text: ["TAP ANYWHERE"],
                          textStyle: TextStyle(color: Colors.white),
                        ),
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

如果有人仍面临此问题,则需要向AnimatedTextKit小部件添加ValueKey

所以问题应该是

TyperAnimatedTextKit(
key:ValueKey(quoteBrain.getQuoteText()),
isRepeatingAnimation:错误,
text:[quoteBrain.getQuoteText()],
textStyle:textStyle(fontFamily:'quitemagic',fontSize:50,颜色:Colors.white),
)

现在调用setState应该按预期更新您的小部件

如果有人仍然面临此问题,您需要向AnimatedTextKitWidget添加ValueKey

所以问题应该是

TyperAnimatedTextKit(
key:ValueKey(quoteBrain.getQuoteText()),
isRepeatingAnimation:错误,
text:[quoteBrain.getQuoteText()],
textStyle:textStyle(fontFamily:'quitemagic',fontSize:50,颜色:Colors.white),
)

现在调用setState应按预期更新您的小部件

请提供QuoteBrainclass@TipuSultan谢谢,我已经将quotebrain类和quote类添加到post@macl64这与动画文本小部件有关。人们之前也遇到过同样的问题,github上相应的问题已经在没有任何修改的情况下解决了解决方案。链接到问题-@JigarPatel谢谢你的背景。我想现在我会尝试找到另一种方法来实现我想要的动画效果(一个字母出现在另一个字母之后的动画)请提供报价单class@TipuSultan谢谢,我已经将quotebrain类和quote类添加到post@macl64这与动画文本小部件有关。人们之前也遇到过同样的问题,github上的相应问题已在没有解决方案的情况下解决。链接到问题-@JigarPatel感谢您的支持我想现在我会尝试找到另一种方法来实现我想要的动画效果(一个字母出现在另一个字母之后的动画)
import 'quote.dart';

class QuoteBrain {
  int quoteIndex = 0;

  List<Quote> _quoteBank = [
    Quote('\"This is an example of a quote. I am making it really long to see how it wraps\"', '\~Person One'),
    Quote('This is a second test of a quote', '\~Person Two')
  ];

  String getQuoteText() {
    return _quoteBank[quoteIndex].quoteText;
  }

  String getQuoteAuthor() {
    return _quoteBank[quoteIndex].quoteAuthor;
  }

  void NextQuote() {
    if (quoteIndex < _quoteBank.length - 1) {
      quoteIndex++;
    }
  }
}
class Quote {
  String quoteText;
  String quoteAuthor;

  Quote(this.quoteText, this.quoteAuthor);
}