i wanted to know how i can create a random number generator. But not the usual, i want to build the following:
In the App there should be a TextField where the user can put in a min and a max value of the number generator.
After clicking a button, there should be a popup or a modified AlertDialog which prints the result
I would be glad if u could help me.
CodePudding user response:
For random number:
int MIN;
int MIN;
double randomNumber = random.nextInt(MAX) MIN;
For text fields: You get the data from text field (For instance using text field onSubmitted) and set it to min and max.
for pop up:
// You can set the title and content
with using the AlertDialog(title: Text('Random number') , content: Text(randomNumber.toString()))
For instance this may be the code that you want (just an example, you can change it as you wish):
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()),
],
),
);
}
}