When do we need to use the Dart “math” library in Flutter? Not only that, we also use the min method to find the smallest number.
Basically the Dart min method passes two numbers as arguments and returns the smallest one.
The e-commerce app, which we have been building, has gone through a lot of changes.
Firstly, we have added the products and provided the products with the help of the Provider package.
Secondly, the same way, we have provided the cart model and shown the carts page separately.
Finally, when we clear the cart, we need to provide the order items. So that we can finally buy them.
![Push replacement named Flutter - second example](https://i0.wp.com/sanjibsinha.com/wp-content/uploads/2022/07/Push-replacement-named-Flutter-second-example.webp?ssl=1)
Let’s see the code of the order item controllers.
After that we will discuss the code and will learn why we needed to use the Dart min method.
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import './../models/orders.dart';
class OrderItem extends StatefulWidget {
final OrderClass order;
const OrderItem(this.order, {Key? key}) : super(key: key);
@override
State<OrderItem> createState() => _OrderItemState();
}
class _OrderItemState extends State<OrderItem> {
var _expanded = false;
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.all(10),
child: Column(
children: <Widget>[
ListTile(
title: Text('\$${widget.order.amount}'),
subtitle: Text(
DateFormat('dd/MM/yyyy hh:mm').format(widget.order.dateTime),
),
trailing: IconButton(
icon: Icon(_expanded ? Icons.expand_less : Icons.expand_more),
onPressed: () {
setState(() {
_expanded = !_expanded;
});
},
),
),
if (_expanded)
Container(
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 4),
height: min(widget.order.products.length * 20.0 + 10, 100),
child: ListView(
children: widget.order.products
.map(
(prod) => Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
prod.title,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
Text(
'${prod.quantity}x \$${prod.price}',
style: const TextStyle(
fontSize: 18,
color: Colors.grey,
),
)
],
),
)
.toList(),
),
)
],
),
);
}
}
First of all we have imported the Dart math library. After that we used the min method.
But where?
Watch this part of code.
height: min(widget.order.products.length * 20.0 + 10, 100),
We’ve passed two values. Based on which the height of the Container will vary.
The height property of the Container Widget will measure whether the length of the products is lower than the given value 100.
A simple Dart math min method
For example, we can test this on a simple Dart program.
Here is the code.
import 'dart:math' as math;
main() {
int x = 56;
int y = 10;
int z;
int findMinmumValues() {
z = math.min(x, y);
// returning the Minimum Value.
return z;
}
print(findMinmumValues());
}
It will always return 10.
However, in our e-commerce app, it’s important for another reason.
If the length of the products that we have carted is greater than 100, the ListView will allow us to scroll.
It depends where and when we will use the Dart math min method.
Leave a Reply