如何在 Flutter 中创建随机数生成器?

How to create a random Number Genertor in Flutter?

我想知道如何创建随机数生成器。但不是通常的,我想构建以下内容:

如果你能帮助我,我会很高兴

对于随机数:

int MIN;
int MIN;
double randomNumber = random.nextInt(MAX) + MIN;

对于文本字段: 您从文本字段获取数据(例如使用文本字段 onSubmitted)并将其设置为最小值和最大值。

对于弹出窗口: // 可以设置标题和内容 使用 AlertDialog(title: Text('Random number') , content: Text(randomNumber.toString()))

例如这可能是你想要的代码(只是一个例子,你可以随意更改它):

import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(const MyApp());

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

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: LoginScreen(),
    );
  }
}

class LoginScreen extends StatefulWidget {
  createState() {
    return new LoginScreenState();
  }
}

class LoginScreenState extends State<LoginScreen> {
  int min = 1;
  int max = 1;
  int randomNumber = 1;
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          TextField(
            decoration: InputDecoration(labelText: 'Enter Min'),
            onSubmitted: (thisIsTheMinValueJustSubmitted) {
              min = int.parse(thisIsTheMinValueJustSubmitted);
            },
          ),
          TextField(
            decoration: InputDecoration(labelText: 'Enter Max'),
            onSubmitted: (thisIsTheMaxValueJustSubmitted) {
              max = int.parse(thisIsTheMaxValueJustSubmitted);
            },
          ),
          ElevatedButton(
              onPressed: () {
                setState(() {
                  randomNumber = Random().nextInt(max - min) + min;
                });
              },
              child: Text('Generate Number')),
          AlertDialog(
            title: Text('Random Number is:'),
            content: Text(randomNumber.toString()),
          ),
          Text(randomNumber.toString()),
        ],
      ),
    );
  }
}