In our previous tutorial we have discuss about finding maximum number between two numbers. Current tutorial is just opposite of that tutorial. In today’s tutorial we would discuss about finding minimum number between two numbers in flutter using min<T extends num> function. The min() method is already given in dart in math package. We can use it by importing ‘dart:math’ class in our flutter project. So in this tutorial we would Flutter Dart Find Minimum Number Value Between Two Numbers Android iOS Example Tutorial.
Contents in this project Flutter Dart Find Minimum Number Value Between Two Numbers Android iOS Example Tutorial:
1. Open your project’s main.dart file and import both material.dart and dart:math library package.
1 2 |
import 'package:flutter/material.dart'; import 'dart:math'; |
2. Creating void main runApp() method and here we would call our main Root parent class MyApp.
1 |
void main() => runApp(MyApp()); |
3. Creating our main class named as MyApp extends State less widget.
1 2 3 4 |
class MyApp extends StatelessWidget { } |
4. Creating 3 integer variables named as A, B and C. We would assign some random values to both A and B and make the C empty so it can hold the result.
1 2 3 4 5 |
int A = 120; int B = 110; int C; |
5. Creating a function named as findMinBetween2Values(). Inside this function we would call min() method and assign the minimum value result to C. We would also print the C on terminal screen.
1 2 3 4 5 6 7 |
findMinBetween2Values() { // Finding Minimum Value. C = min(A, B); // Printing Minimum Value. print(C); } |
6. Creating Widget Build area and here we would make a Raised Button widget and call the findMinBetween2Values() function on button click event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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: () => findMinBetween2Values(), child: Text(' Find Minimum Value Between 2 Numbers '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(10, 10, 10, 10), )), ])))); } |
7. 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 |
import 'package:flutter/material.dart'; import 'dart:math'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { int A = 120; int B = 110; int C; findMinBetween2Values() { // Finding Minimum Value. C = min(A, B); // Printing Minimum Value. print(C); } @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: () => findMinBetween2Values(), child: Text(' Find Minimum Value Between 2 Numbers '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(10, 10, 10, 10), )), ])))); } } |
Screenshots:
