英文:
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()}'),
],
),
),
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论