In Flutter the ‘dart:math’ library contains most of mathematical functions. One of them is pow() method. The pow() function returns Returns x to the power of given exponent. Using this we can find Square, Cube and all the mathematical exponent related calculations. All we have to do is pass the Number and exponent and it will return us the calculated result. So in this tutorial we would Flutter Dart Calculate Find Cube or N Power of Exponent Example.
Contents in this project Flutter Dart Calculate Find Cube or N Power of Exponent Example:
1. Open your project’s main.dart file and import material.dart and dart:math package.
1 2 |
import 'package:flutter/material.dart'; import 'dart:math'; |
2. Creating our main Void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main class MyApp extends StatelessWidget. This is our Root class.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
4. Creating a Double variable named as A with 9 value. We would also make a variable named as B with empty value.
1 2 3 |
double A = 9; double B; |
5. Creating a function named as findSquareRoot(). In this function we would call the pow() inbuilt method and pass both arguments. We would also print the Cube on Terminal screen. To read more about this function please go check out Flutter Dart official page.
1 2 3 4 5 6 7 |
findSquareRoot() { // Finding Cube. B = pow(A, 3); // Printing Cube on Terminal. print(B); } |
6. Creating Widget Build area and we would make a Raised Button widget. We would call the findSquareRoot() function on button onPress 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: () => findSquareRoot(), child: Text('Find Cube Of Given Number'), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), )), ])))); } |
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 |
import 'package:flutter/material.dart'; import 'dart:math'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { double A = 9; double B; findSquareRoot() { // Finding Cube. B = pow(A, 3); // Printing Cube on Terminal. print(B); } @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: () => findSquareRoot(), child: Text('Find Cube Of Given Number'), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), )), ])))); } } |
Screenshots:
