From a08ccd526c6c90a684bbe6b52603e1db0d8976b4 Mon Sep 17 00:00:00 2001 From: denisnadey Date: Thu, 1 Oct 2020 15:20:49 +0300 Subject: [PATCH] release 1.o --- analysis_options.yaml | 5 +- lib/main.dart | 370 ++++++++++++++++++++++++++++++++---------- pubspec.lock | 40 ++--- 3 files changed, 305 insertions(+), 110 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 26128b9..7b6c39d 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1 +1,4 @@ -include: package:lint/analysis_options.yaml \ No newline at end of file +include: package:lint/analysis_options.yaml +analyzer: + enable-experiment: + - extension-methods \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 658a607..3438442 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,69 +1,60 @@ import 'package:flutter/material.dart'; -enum Operation { plus, minus, multi, div, point, res } -enum Numbers { zero, one, two, three, four, five, six, seven, eight, nine } -double a, b; -dynamic operator; +enum Operation { + plus, + minus, + multi, + div, + point, + res, + percent, + plusMinus, + backspace, + clear +} -String textResh = "0"; - -void getCalc(dynamic type) { - if (type is Numbers) { - if (a == null) { - a = type.index.toDouble(); - print("a: ${a}"); - } else if (a != null && operator == null) { - if (a >= 0) { - a = (a * 10) + type.index; - print("a: ${a} - concat"); - } - } else if (operator != null) { - if (b == null) { - b = type.index.toDouble(); - print("b: ${b}"); - } else if (b != null) { - if (b >= 0) { - b = (b * 10) + type.index; - print("b: ${b} - concat"); - } - } +extension OperationExtension on Operation { + String getString() { + switch (this) { + case Operation.plus: + return "+"; + case Operation.minus: + return "-"; + case Operation.multi: + return "x"; + case Operation.div: + return "÷"; + case Operation.point: + return "."; + case Operation.res: + return "="; + default: + throw UnimplementedError(); } } - - if (type is Operation) { - if (type == Operation.res) { - print("счиатаем выполняя : ${operator}"); - a = operationEvent(a.toInt(), b.toInt(), operator); - b = null; - - print("ответ : ${a}"); - } else { - operator = type; - } - print(type); - } } -double operationEvent(int x, int y, dynamic op) { - double result = 0; - switch (op) { - case (Operation.plus): - result = (x + y).toDouble(); - break; - case (Operation.minus): - result = (x - y).toDouble(); - break; - case (Operation.multi): - result = (x * y).toDouble(); - break; - case (Operation.div): - result = (x / y).toDouble(); - break; - default: - result = 0; - break; +enum Numbers { zero, one, two, three, four, five, six, seven, eight, nine } + +extension NumbersExtension on num { + num backspace() { + String pull = "$this"; + pull = pull.substring(0, pull.length - 1); + if (pull.isNotEmpty && pull.split('').last.contains('.')) { + pull = pull.substring(0, pull.length - 1); + } + return num.parse(pull, (pull) => 0); + } + + num concat({bool pointer, int value}) { + if (pointer == true) { + final String pull = "${this}." + "$value"; + return num.parse(pull); + } else { + final String pull = "$this" + "$value"; + return num.parse(pull); + } } - return result; } void main() { @@ -94,6 +85,166 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { + num a, b; + bool pointStatus; + dynamic operator; + String textResh = "0"; + + void getCalc(dynamic type) { + if (type is Numbers) { + if (a == null) { + a = type.index; + + setState(() { + textResh = '$a'; + }); + } else if (a != null && operator == null) { + if (pointStatus == true) { + a = a.concat(pointer: true, value: type.index); + pointStatus = false; + } else { + a = a.concat(pointer: false, value: type.index); + } + + setState(() { + textResh = '$a'; + }); + } else if (operator != null) { + if (b == null) { + b = type.index; + setState(() { + textResh = "$a ${(operator as Operation).getString()} $b"; + }); + } else if (b != null) { + if (pointStatus == true) { + b = b.concat(pointer: true, value: type.index); + pointStatus = false; + } else { + b = b.concat(pointer: false, value: type.index); + } + pointStatus = false; + + setState(() { + textResh = "$a ${(operator as Operation).getString()} $b"; + }); + } + } + } else if (type is Operation) { + if (type == Operation.res) { + setState(() { + textResh = "$a ${(operator as Operation).getString()} $b"; + }); + a = operationEvent(a, b, operator as Operation); + b = null; + operator = null; + setState(() { + textResh = "$a"; + }); + } else if (type == Operation.point) { + if (a != null && b == null) { + if (a is double) { + pointStatus = false; + setState(() { + textResh = "$a"; + }); + } else { + pointStatus = true; + setState(() { + textResh = "$a."; + }); + } + } else { + if (b is double) { + pointStatus = false; + setState(() { + textResh = "$a ${(operator as Operation).getString()} $b"; + }); + } else { + pointStatus = true; + setState(() { + textResh = "$a ${(operator as Operation).getString()} $b."; + }); + } + } + } else if (type == Operation.clear) { + a = 0; + b = null; + operator = null; + setState(() { + textResh = "$a"; + }); + } else if (type == Operation.backspace) { + if (a != null && b == null) { + a = a.backspace(); + setState(() { + textResh = "$a"; + }); + } else { + b = b.backspace(); + setState(() { + textResh = "$a ${(operator as Operation).getString()} $b"; + }); + } + } else if (type == Operation.plusMinus) { + if (a != null && b == null) { + a = a * -1; + setState(() { + textResh = "$a"; + }); + } else { + b = b * -1; + setState(() { + textResh = "$a ${(operator as Operation).getString()} $b"; + }); + } + } else if (type == Operation.percent) { + if (a != null && b == null) { + a = a / 100; + setState(() { + textResh = "$a"; + }); + } else { + b = b / 100; + setState(() { + textResh = "$a ${(operator as Operation).getString()} $b"; + }); + } + } else { + operator = type; + setState(() { + textResh = "$a ${(operator as Operation).getString()}"; + }); + } + } + } + + num operationEvent(num x, num y, Operation op) { + num result = 0; + switch (op) { + case Operation.plus: + result = x + y; + break; + case Operation.minus: + result = x - y; + break; + case Operation.multi: + result = x * y; + break; + case Operation.div: + result = x / y; + break; + default: + result = 0; + break; + } + + if (result % 1 == 0) { + return result.toInt(); + } else { + return result; + } + } + @override Widget build(BuildContext context) { return Scaffold( @@ -111,41 +262,82 @@ class _MyHomePageState extends State { child: Align( alignment: Alignment.centerRight, child: Text( - '$textResh', - style: TextStyle(fontSize: 80, color: Colors.white), + textResh, + style: const TextStyle(fontSize: 80, color: Colors.white), ), ), ), Row( children: [ - CallButton(text: "7", value: Numbers.seven), - CallButton(text: "8", value: Numbers.eight), - CallButton(text: "9", value: Numbers.nine), - CallButton(text: "/", value: Operation.div), + CallButton( + text: "±", + value: Operation.plusMinus, + onPressed: getCalc, + ), + CallButton( + text: "%", + value: Operation.percent, + onPressed: getCalc), + CallButton( + text: "⌫", + value: Operation.backspace, + onPressed: getCalc), + CallButton( + text: "AC", value: Operation.clear, onPressed: getCalc), ], ), Row( children: [ - CallButton(text: "4", value: Numbers.four), - CallButton(text: "5", value: Numbers.five), - CallButton(text: "6", value: Numbers.six), - CallButton(text: "*", value: Operation.multi), + CallButton( + text: "7", + value: Numbers.seven, + onPressed: getCalc, + ), + CallButton( + text: "8", value: Numbers.eight, onPressed: getCalc), + CallButton( + text: "9", value: Numbers.nine, onPressed: getCalc), + CallButton( + text: "/", value: Operation.div, onPressed: getCalc), ], ), Row( children: [ - CallButton(text: "1", value: Numbers.one), - CallButton(text: "2", value: Numbers.two), - CallButton(text: "3", value: Numbers.three), - CallButton(text: "-", value: Operation.minus), + CallButton( + text: "4", value: Numbers.four, onPressed: getCalc), + CallButton( + text: "5", value: Numbers.five, onPressed: getCalc), + CallButton( + text: "6", value: Numbers.six, onPressed: getCalc), + CallButton( + text: "*", value: Operation.multi, onPressed: getCalc), ], ), Row( children: [ - CallButton(text: "0", value: Numbers.zero), - CallButton(text: ".", value: Operation.point), - CallButton(text: "=", value: Operation.res), - CallButton(text: "+", value: Operation.plus), + CallButton( + text: "1", value: Numbers.one, onPressed: getCalc), + CallButton( + text: "2", value: Numbers.two, onPressed: getCalc), + CallButton( + text: "3", value: Numbers.three, onPressed: getCalc), + CallButton( + text: "-", value: Operation.minus, onPressed: getCalc), + ], + ), + Row( + children: [ + CallButton( + text: "0", value: Numbers.zero, onPressed: getCalc), + CallButton( + text: ".", value: Operation.point, onPressed: getCalc), + CallButton( + text: "=", value: Operation.res, onPressed: getCalc), + CallButton( + text: "+", + value: Operation.plus, + onPressed: getCalc, + ), ], ), ], @@ -156,9 +348,13 @@ class _MyHomePageState extends State { } class CallButton extends StatelessWidget { - String text; - dynamic value; - CallButton({this.text, this.value}); + final String text; + final dynamic value; + + final void Function(dynamic) onPressed; + + const CallButton( + {@required this.text, @required this.value, @required this.onPressed}); @override Widget build(BuildContext context) { @@ -171,16 +367,12 @@ class CallButton extends StatelessWidget { decoration: BoxDecoration( borderRadius: BorderRadius.circular(12), color: const Color(0xE0E0E0E0)), - child: AspectRatio( - aspectRatio: 1, - child: FlatButton( - onPressed: () { - getCalc(value); - }, - child: Text( - text, - style: TextStyle(fontSize: 60, color: Colors.blue[900]), - ), + child: FlatButton( + height: 60, + onPressed: () => onPressed(value), + child: Text( + text, + style: TextStyle(fontSize: 40, color: Colors.blue[900]), ), ), ), diff --git a/pubspec.lock b/pubspec.lock index bdce59e..55fc951 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,42 +7,42 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0-nullsafety" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety" + version: "2.1.0-nullsafety.1" characters: dependency: transitive description: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.2" + version: "1.1.0-nullsafety.3" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety" + version: "1.2.0-nullsafety.1" clock: dependency: transitive description: name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety" + version: "1.1.0-nullsafety.1" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0-nullsafety.2" + version: "1.15.0-nullsafety.3" cupertino_icons: dependency: "direct main" description: @@ -56,7 +56,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety" + version: "1.2.0-nullsafety.1" flutter: dependency: "direct main" description: flutter @@ -73,28 +73,28 @@ packages: name: lint url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10-nullsafety" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.2" + version: "1.3.0-nullsafety.3" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety" + version: "1.8.0-nullsafety.1" sky_engine: dependency: transitive description: flutter @@ -106,55 +106,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety" + version: "1.8.0-nullsafety.2" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.10.0-nullsafety" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety" + version: "2.1.0-nullsafety.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety" + version: "1.1.0-nullsafety.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19-nullsafety" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.2" + version: "1.3.0-nullsafety.3" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.2" + version: "2.1.0-nullsafety.3" sdks: - dart: ">=2.10.0-0.0.dev <2.10.0" + dart: ">=2.10.0-110 <2.11.0"