如何使图像随机发电机在颤动中?

发布于 01-23 14:54 字数 1015 浏览 3 评论 0原文

大家好我是新手Flutter& DART ,我正在尝试制作一个应用程序,该应用每次用户打开它时都会显示不同的图像, 我标记了此方法Randomnum,但我知道它不会长时间工作,因为我希望用户使用我拥有的默认图像下载自己的图像,

我的方法是使图像具有相同的名称但是使用不同的数字并制作一个随机数发电机:

Random random = new Random();
int randomNum = random.nextInt(3) + 1;

class _qouteeState extends State<qoutee> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: Expanded(
            flex: 3,
            child: Container(
              color: Colors.grey[300],
              child: Center(
                child: Image.asset('images/q$randomNum.jpg'),
              ),
            ),
          ),
        ),
      ),

    );
  }
}

图像:

因此,如何在不使图像具有相同名称的情况下使随机图像功能

hey guys I'm new to Flutter & Dart and I'm trying to make an app that will show a different image every time the user opens it,,
I marked this method randomNum but I know it will not work for long because I want the user to download their own images with the default images that I have,

my method is to make the images have the same name but with different numbers and make a RandomNumber generator :

Random random = new Random();
int randomNum = random.nextInt(3) + 1;

class _qouteeState extends State<qoutee> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: Expanded(
            flex: 3,
            child: Container(
              color: Colors.grey[300],
              child: Center(
                child: Image.asset('images/q$randomNum.jpg'),
              ),
            ),
          ),
        ),
      ),

    );
  }
}

images :
enter image description here

So how can I make Random image function without making the images have the same name

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑2025-01-30 14:54:17

您可以将图像的路径保存在字符串列表中。然后制作一个取决于此列表的长度的随机对象。例如,如果随机值为1,则返回列表中索引1的值。这是一个非常简单的例子。
列表路径= [path_of_image1,image2 ...];

Random random = Random();
int randomNum = random.nextInt(paths.length);
String selectedImagePath = paths[randomNum];
//you can display your image by a specific widget.

如果您询问如何添加用户添加图像并采取路径,则可以找到大量资源和解决方案。

you can save the path of your images in a list of String. then make a random object that depends on the length of this list. For example, if the random value was 1 then return the value of the index 1 in the list. here is a very simple example.
List paths = [path_of_image1, image2 ...];

Random random = Random();
int randomNum = random.nextInt(paths.length);
String selectedImagePath = paths[randomNum];
//you can display your image by a specific widget.

If you ask about how to add an image by the user and take the path you can find a lot of resources and solutions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文