mingsnx / animated_digit

A scrolling digit animation widget
MIT License
37 stars 17 forks source link

Text format support for example 10.000 => 10k #7

Closed RoyalCoder88 closed 1 year ago

RoyalCoder88 commented 2 years ago

Hi @mingsnx,

Thanks for your great package, can you please add support for formatting the text numbers for example like this:

      int value = int.parse(count);

      if (value < 1000) {
        // less than a thousand
        return value.toStringAsFixed(0);
      } else if (value >= 1000 && value < 1000000) {
        // less than 1 million
        double result = value / 1000;
        return "${result.toStringAsFixed(2)} K";
      } else if (value >= 1000000 && value < (1000000 * 10 * 100)) {
        // less than 100 million
        double result = value / 1000000;
        return "${result.toStringAsFixed(2)} M";
      } else if (value >= (1000000 * 10 * 100) &&
          value < (1000000 * 10 * 100 * 100)) {
        // less than 100 billion
        double result = value / (1000000 * 10 * 100);
        return "${result.toStringAsFixed(2)} B";
      } else if (value >= (1000000 * 10 * 100 * 100) &&
          value < (1000000 * 10 * 100 * 100 * 100)) {
        // less than 100 trillion
        double result = value / (1000000 * 10 * 100 * 100);
        return "${result.toStringAsFixed(2)} T";
      } else {
        return count;
      }

or guide me please how to achieve this, thanks in advance!

mingsnx commented 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({})

RoyalCoder88 commented 1 year ago

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!

mingsnx commented 1 year ago

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 = "";
    }
  }
}
RoyalCoder88 commented 1 year ago

Thanks a lot, @mingsnx works like a charm❤️