wandersonlwl / flutterflow-widget

Textfield para formatar os valores separando as casas decimais !
0 stars 0 forks source link

TEXTFIELD FORMATA VALROES #1

Open wandersonlwl opened 10 months ago

wandersonlwl commented 10 months ago

// Automatic FlutterFlow imports import '/backend/supabase/supabase.dart'; import '/flutter_flow/flutter_flow_theme.dart'; import '/flutter_flow/flutter_flow_util.dart'; import '/custom_code/widgets/index.dart'; // Imports other custom widgets import '/flutter_flow/custom_functions.dart'; // Imports custom functions import 'package:flutter/material.dart'; // Begin custom widget code // DO NOT REMOVE OR MODIFY THE CODE ABOVE!

// dependencie: currency_text_input_formatter: ^2.1.10

import 'package:currency_text_input_formatter/currency_text_input_formatter.dart'; import 'package:flutter/services.dart';

class CurrencyTextField extends StatefulWidget { const CurrencyTextField({ Key? key, this.width, this.height, required this.valormoeda2, required this.colorText, required this.fontSize, required this.borderRadius, required this.borderColor, required this.borderColorFocus, required this.onChangeAction, this.iconColor, this.iconSize, this.borderWidth, this.textAlign, this.fontFamily, }) : super(key: key);

final double? width; final double? height; final double valormoeda2; final Color colorText; final double fontSize; final double borderRadius; final Color borderColor; final Color borderColorFocus; final Future Function() onChangeAction; final Color? iconColor; final double? iconSize; final double? borderWidth; final TextAlign? textAlign; final String? fontFamily;

@override _CurrencyTextFieldState createState() => _CurrencyTextFieldState(); }

class _CurrencyTextFieldState extends State { late TextEditingController _controller;

@override void initState() { super.initState(); _controller = TextEditingController( text: widget.valormoeda2.toStringAsFixed(2), ); FFAppState().valormoeda2 = widget.valormoeda2; }

double parseCurrency(String value) { final cleanedText = value.replaceAll('R\$', '').trim(); final normalizedText = cleanedText.replaceAll('.', '').replaceAll(',', '.');

return double.tryParse(normalizedText) ?? 0.0;

}

@override Widget build(BuildContext context) { return TextFormField( controller: _controller, keyboardType: TextInputType.number, inputFormatters: [ CurrencyTextInputFormatter( locale: 'pt_BR', decimalDigits: 2, symbol: 'R\$', enableNegative: false, ), LengthLimitingTextInputFormatter(16), ], textAlignVertical: TextAlignVertical.center, textAlign: widget.textAlign ?? TextAlign.center, style: TextStyle( fontWeight: FontWeight.normal, color: widget.colorText, fontSize: widget.fontSize, fontFamily: widget.fontFamily ?? 'Open Sans', // Definindo Open Sans como padrão ), decoration: InputDecoration( contentPadding: EdgeInsets.zero, enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(widget.borderRadius), borderSide: BorderSide( color: widget.borderColor, width: widget.borderWidth ?? 1.0, ), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(widget.borderRadius), borderSide: BorderSide( color: widget.borderColorFocus, width: widget.borderWidth ?? 2.0, ), ), filled: false, prefixIcon: Icon( Icons.monetization_on_outlined, color: widget.iconColor ?? Theme.of(context).primaryColor, size: widget.iconSize ?? 24.0, ), ), onChanged: (text) { final parsedValue = parseCurrency(text); FFAppState().valormoeda2 = parsedValue; widget.onChangeAction(); }, ); } }

wandersonlwl commented 10 months ago

A variável valormoeda2 tem que ser do tipo double -