1
0
Fork 0
flutter_demo_energy/lib/widgets/energy_widget.dart

29 lines
745 B
Dart
Raw Normal View History

2023-04-12 08:41:41 +02:00
import 'package:flutter/material.dart';
class EnergyWidget extends StatelessWidget {
const EnergyWidget({
super.key,
required this.energy,
});
final double energy;
@override
Widget build(BuildContext context) {
return Text("Energy ${_format(energy)}Joule", style: Theme.of(context).textTheme.headlineLarge,);
}
String _format(double e) {
if (e > 1000000000000) {
return "${(energy / 1000000000000).round()} T";
} else if (e > 1000000000) {
return "${(energy / 1000000000).round()} G";
} else if (e > 1000000) {
return "${(energy / 1000000).round()} M";
} else if ( e > 1000) {
return "${(energy / 1000).round()} k";
} else {
return "${energy.round()} ";
}
}
}