Home > Software design >  I have problem in my flutter calculator project in if part
I have problem in my flutter calculator project in if part

Time:12-30

I making calculator
for calculator i want 4321 to 4,321 => after 3 numbers it must to put ',' this is the text form fild part i think its bether to skip this part to therd part in cods (go to ///////// part )

 Container(
              alignment: Alignment.centerRight,
              child: TextFormField(
                textAlign: TextAlign.end,
                readOnly: true,
                cursorColor: Colors.grey[700],
                controller: controller,
                keyboardType: TextInputType.none,
                decoration: const InputDecoration(
                  hintText: '0',
                  hintStyle: TextStyle(color: Colors.white, fontSize: 45),
                  enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                      width: 0,
                      color: Colors.transparent,
                    ),
                  ),
                  focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                      width: 0,
                      color: Colors.transparent,
                    ),
                  ),
                ),
                style: const TextStyle(color: Colors.white, fontSize: 43),
              ),
            ),

I made class to bild elevator button :

class ElebatedButtonMaker extends StatelessWidget {
  ElebatedButtonMaker({
    Key? key,
    required this.color,
    required this.height,
    required this.width,
    this.widget,
    required this.text,
    this.onPressed,
  }) : super(key: key);

  final String text;
  final Color color;
  final double width;
  final double height;
  void Function()? onPressed;
  Widget? widget;
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
        onPressed: onPressed ?? () {},
        style: ElevatedButton.styleFrom(
            backgroundColor: color,
            elevation: 0,
            fixedSize: Size(MediaQuery.of(context).size.width * width,
                MediaQuery.of(context).size.width * height)),
        child: text.isNotEmpty
            ? Text(
                text,
                style: const TextStyle(
                  fontSize: 25,
                ),
              )
            : widget);
  }
}

////////////////////////////////////////////////////////////////////////////////////////////// I didnt see eny problems here and it Im calling this class for putting it in the onpressed part

class Passclass {
  const Passclass({
    this.pakonebord,
    this.resetbord,
    this.textstring,
    this.textdouble,
    required this.controller,
  });
  final bool? resetbord;
  final bool? pakonebord;
  final String? textstring;
  final TextEditingController controller;
  final int? textdouble;
  final String alamat = ',';
  passer() {
    if (textdouble != null) {
      if (textdouble == 0) {
        return controller.text = '0';
      }
      return controller.text = controller.text   textdouble.toString();
    }
    if (!(controller.text.contains('.'))) {
      return controller.text = controller.text   textstring!;
    }

    if (controller.text.length > 3) {
      return controller.text = controller.text.substring(0, 1)  
          alamat  
          controller.text.substring(1);
    } 
    //  *********BUG*************** this if must to do take 3 numbelrs and put ',' inside them
  }

  reset() {
    if (resetbord == true) {
      return controller.text = '0';
    }
  }

  pakon() {
    if (controller.text.isNotEmpty) {
      if (pakonebord == true) {
        return controller.text =
            controller.text.substring(0, controller.text.length - 1);
      }
    }
  }
}

i dont have eny errors but it didnt do all the if's was do ing well but thisone i dont know pleas help me

i tryed alot bot i didnt sucsessful i did smplis it for myself do ing :

void main() {
  String text = '4321';
  if (text.length > 3) {
    print('${text.substring(0, 1)},${text.substring(1)}');
  }
}

i did work well but in the project i dont know what is the main problem of it

CodePudding user response:

Instead of all this code, you could use intl package to do this for you:

import 'package:intl/intl.dart';

final numFormatter = new NumberFormat("#,##0.00", "en_US");

//usage
var formattedString = numFormatter.format(1234567.89);

//Output will be 1,234,567.89
print(formattedString);

CodePudding user response:

@override
  Widget build(BuildContext context) {
    String addComma(String number, {bool? isReturnZero}) {
      number = number.trim();
      if (double.tryParse(number) != null) {
        if (double.parse(number) == 0) {
          return isReturnZero != null ? '0' : '';
        } else {
          NumberFormat formater = NumberFormat();
          if (double.parse(number) > 999) formater = NumberFormat('#,000');
          return formater.format(double.tryParse(number));
        }
      } else {
        return number;
      }
    }

    return Scaffold(
      body: Container(
          alignment: Alignment.center,
          child: TextField(
            controller: _textEditingController,
            focusNode: _focusNode,
            inputFormatters: [
              FilteringTextInputFormatter.allow(RegExp(r"[0-9]"),
                  replacementString: ''),
              LengthLimitingTextInputFormatter(50),
            ],
            onChanged: (text) {
              _focusNode.requestFocus();
              var newText = addComma(text);
              _textEditingController.value = TextEditingValue(
                  text: newText,
                  selection: TextSelection.fromPosition(TextPosition(
                      affinity: TextAffinity.downstream,
                      offset: newText.length)));
            },
          )),
    );
  }
}
  • Related