你好,我如何在Flutter中执行随机操作?

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

Hi how can I do random operations with flutter?

问题

Hi how do I want to show you random city name and time in my application? Different times and cities should appear each time the app is entered? Let me illustrate what I mean: The user entered the application and saw Amsterdam at 23:43 as the time. How can I do if I want the output from the application to appear in the opposite Hague 08:37 when it enters again? Please help.

英文:

Hi how do I want to show you random city name and time in my application? Different times and cities should appear each time the app is entered?Let me illustrate what I mean: The user entered the application and saw Amsterdam at 23:43 as the time. How can I do if I want the output from the application to appear in the opposite Hague 08:37 when it enters again? Please help

答案1

得分: 0

以下是代码的翻译部分:

您可以拥有`cities`列表和`times`列表,甚至可以基于当前时间生成`time`。

示例代码:

```dart
class MyApp extends StatelessWidget {
  final cities = ['阿姆斯特丹', '海牙', '鹿特丹', '乌特勒支', '格罗宁根'];

  @override
  Widget build(BuildContext context) {
    final random = Random(); // 您可以使用随机数生成随机城市
    final city = cities[random.nextInt(cities.length)];
    final time = DateTime.now().add(Duration(minutes: random.nextInt(60 * 24)));

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('随机城市')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(city, style: TextStyle(fontSize: 32)),
              SizedBox(height: 16),
              Text('${time.hour.toString()}:${time.minute.toString()}'),
            ],
          ),
        ),
      ),
    );
  }
}
英文:

You can have list of cities and list of times or you can even generate time based on present time.

Sample code:

class MyApp extends StatelessWidget {
  final cities = ['Amsterdam', 'The Hague', 'Rotterdam', 'Utrecht', 'Groningen'];

  @override
  Widget build(BuildContext context) {
    final random = Random(); // You can use random to generate random cities
    final city = cities[random.nextInt(cities.length)];
    final time = DateTime.now().add(Duration(minutes: random.nextInt(60 * 24)));

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Random City')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(city, style: TextStyle(fontSize: 32)),
              SizedBox(height: 16),
              Text('${time.hour.toString()}:${time.minute.toString()}'),
            ],
          ),
        ),
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年3月1日 15:37:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600732.html
匿名

发表评论

匿名网友

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

确定