szakes1
szakes1

Reputation: 904

Cannot replace a string in Dart

I'm re-writing a simple application to calculate averages, however I have a hard time with a TextField.

I set the TextField to be able to replace a comma to a period if a locale is en_US, otherwise it should replace a period to a comma if locale is different than en_US. However, replaceAll() method doesn't seem to work.

Sorry for the code quality, but I will fix it after I see where's the problem.

Screenshot

Code:

import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/services.dart';


class ArithmeticAverageScreen extends StatefulWidget {
  @override
  _ArithmeticAverageScreenState createState() => _ArithmeticAverageScreenState();
}



class _ArithmeticAverageScreenState extends State<ArithmeticAverageScreen> {
  var _grades = List<Widget>();

  var _textFieldController = TextEditingController();
  var _gradesList = List<String>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('arithmetic_average_title').tr(),
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: ListView(
          children: <Widget>[
            Card(
              child: Container(
                padding: EdgeInsets.symmetric(vertical: 20.0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    ListTile(
                      leading: Icon(Icons.help),
                      title: Text('arithmetic_average_help').tr(),
                      subtitle: Text('arithmetic_average_help_content').tr(),
                    )
                  ],
                ),
              ) 
            ),
            SizedBox(height: 16.0),
            Card(
              child: Container(
                padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 20.0),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    Text('arithmetic_average_your_grades', style: Theme.of(context).textTheme.headline5).tr(),
                    SizedBox(height: 16.0),
                    Text('arithmetic_average_grades_one_at_a_time', style: Theme.of(context).textTheme.headline6,).tr(),
                    SizedBox(height: 16.0),
                    Row(
                      children: <Widget>[
                        Container(
                          width: 60.0,
                          child: TextField(
                            controller: _textFieldController,
                            inputFormatters: [ WhitelistingTextInputFormatter(RegExp("[0-9,.]")) ],

                            decoration: InputDecoration(
                              hintText: '5',
                              labelText: 'arithmetic_average_grade'.tr()
                            ),
                          ),
                        ),
                        SizedBox(width: 20.0,),
                        RaisedButton(
                          onPressed: () {
                            if (_textFieldController.text == '') {
                              showDialog(
                                context: context,
                                builder: (context) {
                                  return AlertDialog(
                                    content: Text('arithmetic_average_type_number').tr(),
                                  );
                                }
                              );
                            }
                            else {

                              String locale = Localizations.localeOf(context).toString();

                                if (locale == 'en_US') {
                                  if (_textFieldController.text.contains(',')) {

                                    print('True');
                                    _textFieldController.text.replaceAll(',', '.');
                                  }
                                }
                                else if (locale == 'pl_PL') {
                                  if (_textFieldController.text.contains('.')) {
                                    _textFieldController.text.replaceAll('.', ',');
                                  }
                                }
                              setState(() {
                                _gradesList.add(_textFieldController.text);
                                print(_gradesList);

                                _grades.add(Text(_textFieldController.text));
                              });
                            }
                          },
                          color: Colors.teal[300],
                          textColor: Colors.white,
                          child: Text('arithmetic_average_add_button').tr(),
                        ),
                      ],
                    ),
                    SizedBox(height: 16.0,),
                    Wrap(
                      children: _grades,
                    ),
                  ],
                ),
              ),
            )
          ],
        ),
      )
    );
  }
}

Upvotes: 0

Views: 1588

Answers (1)

Sanjay Sharma
Sanjay Sharma

Reputation: 4035

In the following line, you are not updating the text of the text field so replace the following

_textFieldController.text.replaceAll(',', '.');

with this

_textFieldController.text = _textFieldController.text.replaceAll(',', '.')

Upvotes: 2

Related Questions