In our previous tutorials we have learn about finding max and min number between two numbers. But the main functionality every flutter dart developer needs is finding maximum and minimum number between multiple numbers from array. So in today’s tutorial we would learn about Flutter Find Maximum Minimum Number Between Multiple Numbers Dart Android iOS Example Tutorial.
Contents in this project Flutter Find Maximum Minimum Number Between Multiple Numbers Android iOS Dart Example:
1. Open your project’s main.dart file and import material.dart and ‘dart:math’ package in your project.
1 2 |
import 'package:flutter/material.dart'; import 'dart:math'; |
2. Creating void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main Root parent MyApp extends StatelessWidget class.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
4. Creating a integer type variable named as C. We would use this variable to hold the maximum and minimum number result.
1 |
int C; |
5. Creating a function named as findMinValue(). In this function we would use [].reduce(min) method to find the minimum of given numbers. We would also print the result on Terminal screen.
1 2 3 4 5 6 7 |
findMinValue() { // Finding Minimum Value. C = [1, 2, 8, 6, 12, 13, 13, 17, 20].reduce(min); // Printing Minimum Value. print(C); } |
6. Creating another function named as findMaxValue(). In this function we would use the [].reduce(max) method to show the maximum number between given numbers..
1 2 3 4 5 6 7 |
findMaxValue() { // Finding Max Value. C = [1, 2, 8, 6, 12, 13, 13, 17, 20].reduce(max); // Printing Max Value. print(C); } |
7. Creating Widget Build area and now we would make 2 Raised Button widgets and call both above functions on button onPressed event.
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 |
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: () => findMaxValue(), child: Text(' Find Max Value Between Multiple Numbers '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(10, 10, 10, 10), )), Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => findMinValue(), child: Text(' Find Minimum Value Between Multiple Numbers '), textColor: Colors.white, color: Colors.pink, padding: EdgeInsets.fromLTRB(10, 10, 10, 10), )), ])))); } |
8. 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 45 46 47 48 49 50 51 52 53 |
import 'package:flutter/material.dart'; import 'dart:math'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { int C; findMinValue() { // Finding Minimum Value. C = [1, 2, 8, 6, 12, 13, 13, 17, 20].reduce(min); // Printing Minimum Value. print(C); } findMaxValue() { // Finding Max Value. C = [1, 2, 8, 6, 12, 13, 13, 17, 20].reduce(max); // Printing Max 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: () => findMaxValue(), child: Text(' Find Max Value Between Multiple Numbers '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(10, 10, 10, 10), )), Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => findMinValue(), child: Text(' Find Minimum Value Between Multiple Numbers '), textColor: Colors.white, color: Colors.pink, padding: EdgeInsets.fromLTRB(10, 10, 10, 10), )), ])))); } } |
Screenshots:
