From 3c1ecd76c21a5674ed72a2e283638ecab99a3905 Mon Sep 17 00:00:00 2001 From: hmalik144 Date: Mon, 5 Sep 2022 23:04:13 +0100 Subject: [PATCH] Text changed on listeners (#4) --- lib/Home.dart | 12 ++++++++++-- lib/MainViewModel.dart | 18 ++++++++++++++++-- lib/Utils/SelectionType.dart | 4 ++++ lib/views/EditText.dart | 16 +++++++++------- pubspec.lock | 10 +++++----- 5 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 lib/Utils/SelectionType.dart diff --git a/lib/Home.dart b/lib/Home.dart index e4d1d87..f7477b6 100644 --- a/lib/Home.dart +++ b/lib/Home.dart @@ -1,4 +1,5 @@ import 'package:easy_cc_flutter/MainViewModel.dart'; +import 'package:easy_cc_flutter/Utils/SelectionType.dart'; import 'package:easy_cc_flutter/views/DropDownBox.dart'; import 'package:easy_cc_flutter/views/EditText.dart'; import 'package:flutter/cupertino.dart'; @@ -16,6 +17,9 @@ class HomePage extends BaseStatelessWidget { @override Widget displayWidget( BuildContext context, MainViewModel model, Widget? child) { + TextEditingController controller1 = TextEditingController(); + TextEditingController controller2 = TextEditingController(); + return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ @@ -24,7 +28,9 @@ class HomePage extends BaseStatelessWidget { child: Column( children: [ DropDownBox(model.data, (selected) {}), - EditText("Enter conversion from", model.top, (selection) => {}) + ConverterEditText("Enter conversion from", controller1, (input) => { + controller2.text = model.convertInput(input, SelectionType.conversionFrom) + }) ], ), ), @@ -33,7 +39,9 @@ class HomePage extends BaseStatelessWidget { child: Column( children: [ DropDownBox(model.data, (selected) {}), - EditText("Enter conversion from", model.bottom, (selection) => {}) + ConverterEditText("Enter conversion from", controller2, (input) => { + controller1.text = model.convertInput(input, SelectionType.conversionTo) + }) ], ), ), diff --git a/lib/MainViewModel.dart b/lib/MainViewModel.dart index 0f0c776..d283e58 100644 --- a/lib/MainViewModel.dart +++ b/lib/MainViewModel.dart @@ -1,9 +1,9 @@ import 'package:easy_cc_flutter/BaseViewModel.dart'; +import 'package:easy_cc_flutter/Utils/SelectionType.dart'; class MainViewModel extends BaseViewmodel { - String? top; - String? bottom; + double conversionRate = 1.4; final List data = ['ALL - Albanian Lek', 'AFN - Afghan Afghani', @@ -160,4 +160,18 @@ class MainViewModel extends BaseViewmodel { 'XOF - West African CFA Franc', 'YER - Yemeni Rial', 'ZMW - Zambian Kwacha']; + + String convertInput(String? input, SelectionType type) { + if (input == null || input.isEmpty) { + return ""; + } + double convertedInput = double.parse(input); + + switch (type) { + case SelectionType.conversionFrom: + return (convertedInput * conversionRate).toStringAsFixed(2); + case SelectionType.conversionTo: + return (convertedInput / conversionRate).toStringAsFixed(2); + } + } } \ No newline at end of file diff --git a/lib/Utils/SelectionType.dart b/lib/Utils/SelectionType.dart new file mode 100644 index 0000000..563276b --- /dev/null +++ b/lib/Utils/SelectionType.dart @@ -0,0 +1,4 @@ +enum SelectionType { + conversionFrom, + conversionTo +} \ No newline at end of file diff --git a/lib/views/EditText.dart b/lib/views/EditText.dart index 05108cb..ddd18bd 100644 --- a/lib/views/EditText.dart +++ b/lib/views/EditText.dart @@ -1,22 +1,21 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import '../Utils/Constants.dart'; -class EditText extends StatelessWidget { +class ConverterEditText extends StatelessWidget { final String _hintText; - final String? _input; final Function(String?) _onChanged; + final TextEditingController _controller; - const EditText(this._hintText, this._input, this._onChanged, {super.key}); + const ConverterEditText(this._hintText, this._controller, this._onChanged, {super.key}); @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.symmetric(vertical: 2, horizontal: 4), child: TextField( - controller: TextEditingController( - text: _input - ), + controller: _controller, decoration: InputDecoration( contentPadding: const EdgeInsets.all(14), border: const OutlineInputBorder( @@ -28,7 +27,10 @@ class EditText extends StatelessWidget { hintText: _hintText, ), onChanged: _onChanged, - + keyboardType: const TextInputType.numberWithOptions(decimal: true), + inputFormatters: [ + FilteringTextInputFormatter.allow(RegExp("[0-9]")), + ], ), ); } diff --git a/pubspec.lock b/pubspec.lock index 8c5f4f6..e6d0b63 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -134,14 +134,14 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.11" + version: "0.12.12" material_color_utilities: dependency: transitive description: name: material_color_utilities url: "https://pub.dartlang.org" source: hosted - version: "0.1.5" + version: "0.2.0" meta: dependency: transitive description: @@ -188,7 +188,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.9.0" + version: "1.9.1" stack_trace: dependency: transitive description: @@ -244,7 +244,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.9" + version: "0.4.13" toast: dependency: "direct main" description: @@ -279,7 +279,7 @@ packages: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.2" + version: "2.1.3" sdks: dart: ">=2.18.0-190.0.dev <3.0.0" flutter: ">=1.20.0"