mirror of
https://github.com/hmalik144/easy_cc_flutter.git
synced 2025-12-10 03:05:34 +00:00
Text changed on listeners (#4)
This commit is contained in:
@@ -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<MainViewModel> {
|
||||
@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<MainViewModel> {
|
||||
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<MainViewModel> {
|
||||
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)
|
||||
})
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
@@ -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<String> data = <String>['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);
|
||||
}
|
||||
}
|
||||
}
|
||||
4
lib/Utils/SelectionType.dart
Normal file
4
lib/Utils/SelectionType.dart
Normal file
@@ -0,0 +1,4 @@
|
||||
enum SelectionType {
|
||||
conversionFrom,
|
||||
conversionTo
|
||||
}
|
||||
@@ -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: <TextInputFormatter>[
|
||||
FilteringTextInputFormatter.allow(RegExp("[0-9]")),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
10
pubspec.lock
10
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"
|
||||
|
||||
Reference in New Issue
Block a user