创建一个小部件,它返回两个小部件,但具有延迟/计时器。

huangapple go评论73阅读模式
英文:

creating a widget that returns two widget but have delayed/timer

问题

如何创建一个小部件,首先显示"正在上传,请稍候...",然后在延迟后显示"上传成功",目前还没有数据库,所以我只想添加一些延迟,如果可能的话,最好可以重复使用。

Widget staticMessageGenerator(delayedMessage, currentMessage) {
    // 这里是文本
    Future.delayed(
      const Duration(seconds: 2),
      () {
        // 在Future.delayed之后添加文本小部件
        // Text(delayedMessage);
      },
    );

    return Text(currentMessage);
}
英文:

How do I create a Widget that first show like Uploading please wait... and after a delayed it would say Successfully Uploaded there are no databases yet, so I just want to add some delay into it and if possible would be great if it would be reusable.

Widget staticMessageGenerator(delayedMessage, currentMessage) {
    // Text here
    Future.delayed(
      const Duration(seconds: 2),
      () {
    // remove the return below and add Text Widget after the Future.delayed like
    // Text(delayedMessage);
      },
    );

    return Text(currentMessage);
  }

答案1

得分: 1

首先,我不建议在只返回FutureBuilder的情况下创建方法,并且主要问题是它可以在每次更改时重新调用API。

Widget staticMessageGenerator(delayedMessage, currentMessage) {
  return FutureBuilder(
      future: Future.delayed(const Duration(seconds: 2)),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return Text(delayedMessage);
        } else {
          return Text(currentMessage);
        }
      });
}

创建一个状态未来变量将解决这个问题。最有可能你需要,我建议如下。

class ExampleF3 extends StatefulWidget {
  const ExampleF3({super.key});

  @override
  State<ExampleF3> createState() => _ExampleF3State();
}

class _ExampleF3State extends State<ExampleF3> {
  Future<String> delayedMessage() async {
    await Future.delayed(const Duration(seconds: 2)); //你的上传操作,
    return 'Success';
  }

  late final future = delayedMessage();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
        future: future,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return Text(snapshot.data.toString());
          } else if (snapshot.hasError) {
            return Text(snapshot.error.toString());
          } else {
            return const Text('Loading...');
          }
        },
      ),
    );
  }
}
英文:

Firstly, I won't recommend creating method while it just return the FutureBuilder. And the main issue it can recall the api on every changes.

Widget staticMessageGenerator(delayedMessage, currentMessage) {
  return FutureBuilder(
      future: Future.delayed(const Duration(seconds: 2)),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return Text(delayedMessage);
        } else {
          return Text(currentMessage);
        }
      });
}

Creating a state future variable will solve the issue. & most likely you will be needing & I will suggest as follows.

class ExampleF3 extends StatefulWidget {
  const ExampleF3({super.key});

  @override
  State&lt;ExampleF3&gt; createState() =&gt; _ExampleF3State();
}

class _ExampleF3State extends State&lt;ExampleF3&gt; {
  Future&lt;String&gt; delayedMessage() async {
    await Future.delayed(const Duration(seconds: 2)); //your upload operation,
    return &#39;Success&#39;;
  }

  late final future = delayedMessage();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
        future: future,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return Text(snapshot.data.toString());
          } else if (snapshot.hasError) {
            return Text(snapshot.error.toString());
          } else {
            return const Text(&#39;Loading...&#39;);
          }
        },
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年7月18日 00:38:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定