简体   繁体   English

"为什么屏幕加载时我的 CircularProgressIndicator 随机旋转?"

[英]Why is my CircularProgressIndicator spinning randomly when the screen loads?

I have a CircularProgressIndicator that is meant to display progress when the user presses a button.我有一个 CircularProgressIndicator,用于在用户按下按钮时显示进度。 However, when I open the screen that uses this class it shows a loading animation and I'm not sure why.但是,当我打开使用此类的屏幕时,它会显示加载动画,我不知道为什么。 When the screen opens, it shouldn't show the animation until the user presses the button.当屏幕打开时,它不应该在用户按下按钮之前显示动画。

When the screen opens you see something similar to the animation on this page: https:\/\/www.c-sharpcorner.com\/article\/create-chrome-like-loading-animation\/<\/a>当屏幕打开时,您会在此页面上看到类似于动画的内容: https<\/a> :\/\/www.c-sharpcorner.com\/article\/create-chrome-like-loading-animation\/

class _MsgInputState extends State<MsgInput> with TickerProviderStateMixin {

  AnimationController? controller;

  @override
  void initState() {
    super.initState();
    initialize();

  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  void initialize() async {
    controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 3),
    )..addListener(() {
      setState(() {});
    });
  }

@override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Align(
          alignment: Alignment.bottomLeft,
          child: Column(
            children: [
              Row(
                children: [
                  GestureDetector(
                    onTap: () async {
                      controller?.forward();
                    },
                    onLongPressDown: (details) async {
                      controller?.forward();
                    },
                    onLongPressUp: () async {
                      controller?.stop();
                      controller?.reset();
                    },
                    child: Text('Record'),
                  ),
                ],
              ),
              Row(
                children: <Widget>[
                  SizedBox(width: 100),
                  CircularProgressIndicator(
                    value: controller?.value,
                    semanticsLabel: 'Linear progress indicator',
                  ),
                ],
              ),
            ],
          ),
        )
    );
  }

If the value provided to a CircularProgressIndicator<\/code> is null<\/code> it just defaults to a spinnging animation.如果提供给CircularProgressIndicator<\/code>的值为null<\/code> ,则它默认为旋转动画。

In the beginnin controller?.value<\/code> probably returns null<\/code> .在 beginnin controller?.value<\/code>可能返回null<\/code> 。 Change it to value: controller?.value ?? 0<\/code>将其更改为value: controller?.value ?? 0<\/code> value: controller?.value ?? 0<\/code> so that while it's null<\/code> 0 is used as a value. value: controller?.value ?? 0<\/code>以便当它为null<\/code>时 0 用作值。

"

Here is a quick examples on how the provided example could be achieved:以下是有关如何实现所提供示例的快速示例:

class StateProgress extends StatelessWidget {
  const StateProgress({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const Center(
      child: SizedBox(
        height: 50,
        width: 50,
        child: CircularProgressIndicator(
          strokeWidth: 2,
          valueColor: AlwaysStoppedAnimation<Color>(Colors.blueAccent /*Should be the color of the indicator*/),
          backgroundColor: Colors.white, //  Could be made to match the color of the background the indicator is on
        ),
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用加载时检测到屏幕覆盖 - screen overlay detected when my app loads 为什么我的 DialogFragment 中的 ProgressBar 停止旋转? - why does the ProgressBar in my DialogFragment stop spinning? 为什么当AdMob加载广告时,我的Android活动会关闭? - Why does my Android Activity close when AdMob loads an ad? 为什么我的进度对话框在新意图start()时没有旋转? - Why my progress dialog is not spinning while new intent start()? Android ListView快速滚动时会随机加载图像 - Android listview loads images randomly when fast scrolled 全屏 CircularProgressIndicator 阻止 FrameLayout 内的触摸 - Full screen CircularProgressIndicator blocks the touch within a FrameLayout 如何从可绘制文件夹中插入随机图像,使其在加载时随机出现在屏幕上? - How do I insert random images from my drawable folder to randomly appear on the screen when loaded? 应用加载时摆脱白屏? - Get rid of white screen when app loads? CircularProgressIndicator 处于活动状态时如何禁用交互? - How to disable interaction when CircularProgressIndicator is active? 为什么即使没有错误,我的Android Studio代码也会随机带红色下划线? - Why does my Android Studio code randomly get underlined in red even when there is no error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM