Dart Generate Random Number Using Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 10-10-2020

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.

Dart Generate Random Number Using Flutter Android App

Dart Generate Random Number
Complete Code For Dart Generate Random Number In Flutter
Main.dart

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

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

class MyApp extends StatelessWidget {
  void  generateRandomNumber() {
    var random = new Random();
    print(random.nextInt(100));
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
        home: Scaffold(
          backgroundColor: Colors.black,
            body: Center(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Container(
                          margin: const EdgeInsets.fromLTRB(20, 10, 20, 10),
                          child:
                          RaisedButton(
                            onPressed: () => generateRandomNumber(),
                            child: Text('Generate Random Number '),
                            textColor: Colors.white,
                            color: Colors.orange,
                            padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
                          )
                      ),

                    ])
            )
        )
    );
  }
}

Related Post