Last active
May 13, 2025 20:32
-
-
Save hectorAguero/7e671eb5c343b1a1d20403e36aa708c8 to your computer and use it in GitHub Desktop.
Flutter decimal text rendering for doubles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
final one = 1.0; | |
final oneHalf = 1.5; | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
Text('$one'), | |
Text('${one.toCleanedValue()}'), | |
Text('${oneHalf}'), | |
Text('${oneHalf.toCleanedValue()}'), | |
Text('${2.0}'), | |
Text('${2.0.toCleanedValue()}'), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
extension IntegerConverterExtension on double { | |
num toCleanedValue() { | |
if (this == toInt()) { | |
return toInt(); | |
} | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment