Closed RoyalCoder88 closed 1 year ago
Hi,@RoyalCoder88 I'm sorry, I've been too busy lately You can do this first,divide and conquer, first deal with numbers, then with units,
last, set value
and suffix
, invoke setstate({})
Thanks a lot @mingsnx I really understand and appreciate your time, and I will give it a try, and I will be appreciated it if you will post a short real example, thanks in advance!
Hi, @RoyalCoder88 example
import 'dart:math';
import 'package:animated_digit/animated_digit.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int fractionDigits = 0;
String suffix = "";
late num value = 0;
void _incrementCounter() {
setState(() {
format(Random().nextInt(4294967296));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
AnimatedDigitWidget(
value: value,
suffix: suffix,
fractionDigits: fractionDigits,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void format(val) {
if (val < 1000) {
value = val;
fractionDigits = 0;
suffix = "";
} else if (val >= 1000 && val < 1000000) {
// less than 1 million
value = val / 1000;
fractionDigits = 2;
suffix = " K";
} else if (val >= 1000000 && val < (1000000 * 10 * 100)) {
// less than 100 million
value = val / 1000000;
fractionDigits = 2;
suffix = " M";
} else if (val >= (1000000 * 10 * 100) &&
val < (1000000 * 10 * 100 * 100)) {
// less than 100 billion
value = val / (1000000 * 10 * 100);
fractionDigits = 2;
suffix = " G";
} else if (val >= (1000000 * 10 * 100 * 100) &&
val < (1000000 * 10 * 100 * 100 * 100)) {
// less than 100 trillion
value = val / (1000000 * 10 * 100 * 100);
fractionDigits = 2;
suffix = " T";
} else {
value = val;
fractionDigits = 0;
suffix = "";
}
}
}
Thanks a lot, @mingsnx works like a charm❤️
Hi @mingsnx,
Thanks for your great package, can you please add support for formatting the text numbers for example like this:
or guide me please how to achieve this, thanks in advance!