Hello friends, In today’s tutorial we would learn about random number. In flutter we would generate random number using their own function Random(). This function would return us random number from given maximum range. But using a simple tweak we can use Random method to generate random number within a range. We can define minimum and maximum range of random number. So when we generate random number the number would be between maximum and minimum range. So let’s get started 🙂 .
Contents in this project Flutter Get Generate Random Number Between Min and Max Range:-
1. Open your project’s main.dart and import material.dart and dart:math package.
1 2 3 |
import 'package:flutter/material.dart'; import 'dart:math'; |
2. Creating our void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main MyApp extends StatelessWidget class. This is our main Class.
1 2 3 4 |
class MyApp extends StatelessWidget { } |
4. Creating a function named as randomNumber(). In this function we would first make the object of Random() class. Now we would make 2 integer type variable named as min and max. In the min number we would define the minimum number from which we want to start the random number and max number. Now at the last we would print the generate positive random number within a range on Terminal window.
1 2 3 4 5 6 7 8 9 10 11 |
void randomNumber() { var random = new Random(); int min = 10; int max = 200; int result = min + random.nextInt(max - min); print(result); } |
5. Creating Widget Build area -> Material App -> Scaffold Widget -> Center Widget -> Column Widget -> Here we would make our Raised Button. On the onPress event of Button we would call the randomNumber() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => randomNumber(), child: Text( ' Click Here To Generate Random Number Between Range', textAlign: TextAlign.center, style: TextStyle(fontSize: 26), ), textColor: Colors.white, color: Colors.lightGreen, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), )), ])))); } |
6. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import 'package:flutter/material.dart'; import 'dart:math'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { void randomNumber() { var random = new Random(); int min = 10; int max = 200; int result = min + random.nextInt(max - min); print(result); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => randomNumber(), child: Text( ' Click Here To Generate Random Number Between Range', textAlign: TextAlign.center, style: TextStyle(fontSize: 26), ), textColor: Colors.white, color: Colors.lightGreen, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), )), ])))); } } |
Screenshots:-
