Random number is useful in many applications where developer wants to generate unique keys or something unique values. In flutter there is a class named as Random which can generate random integer number. We can define the number largest range and the produced number will be in that range. So in this tutorial we would Flutter Dart Generate Random Number on Button click Android iOS Example Tutorial.
Contents in this project Flutter Dart Generate Random Number on Button click in Android iOS Example Tutorial:
1. Import material.dart and dart:math package in your app’s main.dart file.
1 2 3 |
import 'package:flutter/material.dart'; import 'dart:math'; |
2. Create void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Create our main MyApp class extends with Statelesswidget.
1 2 3 4 |
class MyApp extends StatelessWidget { } |
4. Create a function named as generateRandomNumber(). In this function we would firstly create object of Random class then use the objectName.nextInt() method to generate random number in given range. We are printing the generated random number on Terminal screen.
1 2 3 4 5 6 7 8 |
void generateRandomNumber() { var random = new Random(); // Printing Random Number between 1 to 100 on Terminal Window. print(random.nextInt(100)); } |
5. Creating Widget Build area in MyApp class. Now we would make Scaffold widget -> Center Widget -> Column widget -> Container widget -> Raised Button widget. We would call the generateRandomNumber() function on button click event. So every time when user press the button it’ll generates us a new random number between given range.
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 |
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: () => generateRandomNumber(), child: Text(' Click Here To Generate Random Number '), textColor: Colors.white, color: Colors.lightBlue, 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 44 |
import 'package:flutter/material.dart'; import 'dart:math'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { void generateRandomNumber() { var random = new Random(); // Printing Random Number between 1 to 100 on Terminal Window. print(random.nextInt(100)); } @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: () => generateRandomNumber(), child: Text(' Click Here To Generate Random Number '), textColor: Colors.white, color: Colors.lightBlue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } } |
Screenshot:

First, this is the best website that solves my all problems in flutter.
I requested that tell me the code of the random number in a specific range in which I can describe min and max value.
Thanks for appreciating my work Noman. Yes I’ll surely make a new tutorial regarding to your query 🙂 .
And view the numbers in the app?
You can make a Text widget and store the random number into State, Now when you create a Random number store into state and on the Text it will update automatically.