From 0453b3b4207f09035eafebdbe91ce1371c8fcc26 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 10 Apr 2021 11:58:22 -0400 Subject: [PATCH] Youtube parte 2 terminada --- lib/bloc/calculator/calculator_bloc.dart | 75 +++++++++++++++++++---- lib/bloc/calculator/calculator_event.dart | 11 ++++ lib/bloc/calculator/calculator_state.dart | 13 ++++ lib/screens/calculator_screen.dart | 30 ++++----- lib/widgets/results_labels.dart | 22 ++++++- 5 files changed, 123 insertions(+), 28 deletions(-) diff --git a/lib/bloc/calculator/calculator_bloc.dart b/lib/bloc/calculator/calculator_bloc.dart index 56fcce7..70b7b82 100644 --- a/lib/bloc/calculator/calculator_bloc.dart +++ b/lib/bloc/calculator/calculator_bloc.dart @@ -19,25 +19,78 @@ class CalculatorBloc extends Bloc { Stream mapEventToState( CalculatorEvent event, ) async* { - + + // Borrar todo if ( event is ResetAC ) { - yield CalculatorState( - firstNumber: '0', - mathResult: '0', - secondNumber: '0', - operation: 'none' - ); + yield* this._resetAC(); + + // Agregar números } else if ( event is AddNumber ) { - yield CalculatorState( - firstNumber: '0', - secondNumber: '0', - operation: 'none', + yield state.copyWith( mathResult: (state.mathResult == '0') ? event.number : state.mathResult + event.number, ); + + // Cambiar signo de + o - + } else if ( event is ChangeNegativePositive ) { + yield state.copyWith( + mathResult: state.mathResult.contains('-') + ? state.mathResult.replaceFirst('-', '') + : '-' + state.mathResult + ); + + // Borrar último digito + } else if ( event is DeleteLastEntry ) { + yield state.copyWith( + mathResult: state.mathResult.length > 1 + ? state.mathResult.substring(0, state.mathResult.length - 1) + : '0' + ); + + // Agregar operación + } else if ( event is OperationEntry ) { + yield state.copyWith( + firstNumber: state.mathResult, + mathResult: '0', + operation: event.operation, + secondNumber: '0' + ); + + // Calcular resultado + } else if ( event is CalculateResult ) { + yield* _calculateResult(); } + } + Stream _resetAC() async* { + yield CalculatorState( + firstNumber: '0', + mathResult: '0', + secondNumber: '0', + operation: '+' + ); } + + Stream _calculateResult() async* { + + final double num1 = double.parse( state.firstNumber ); + final double num2 = double.parse( state.mathResult ); + + switch( state.operation ) { + + case '+': + yield state.copyWith( + secondNumber: state.mathResult, + mathResult: '${num1 + num2}' + ); + break; + + + default: + yield state; + } + } + } diff --git a/lib/bloc/calculator/calculator_event.dart b/lib/bloc/calculator/calculator_event.dart index 7618ad4..8f5ce5c 100644 --- a/lib/bloc/calculator/calculator_event.dart +++ b/lib/bloc/calculator/calculator_event.dart @@ -9,3 +9,14 @@ class AddNumber extends CalculatorEvent{ final String number; AddNumber(this.number); } + +class ChangeNegativePositive extends CalculatorEvent{} + +class DeleteLastEntry extends CalculatorEvent{} + +class OperationEntry extends CalculatorEvent{ + final String operation; + OperationEntry(this.operation); +} + +class CalculateResult extends CalculatorEvent{} \ No newline at end of file diff --git a/lib/bloc/calculator/calculator_state.dart b/lib/bloc/calculator/calculator_state.dart index 513cff6..af6af67 100644 --- a/lib/bloc/calculator/calculator_state.dart +++ b/lib/bloc/calculator/calculator_state.dart @@ -14,5 +14,18 @@ class CalculatorState { this.operation = '+' }); + CalculatorState copyWith({ + String? mathResult, + String? firstNumber, + String? secondNumber, + String? operation, + }) => CalculatorState( + mathResult : mathResult ?? this.mathResult, + firstNumber : firstNumber ?? this.firstNumber, + secondNumber: secondNumber ?? this.secondNumber, + operation : operation ?? this.operation, + ); + + } diff --git a/lib/screens/calculator_screen.dart b/lib/screens/calculator_screen.dart index d0c51bf..60981ea 100644 --- a/lib/screens/calculator_screen.dart +++ b/lib/screens/calculator_screen.dart @@ -39,17 +39,17 @@ class CalculatorScreen extends StatelessWidget { CalculatorButton( text: '+/-', bgColor: Color(0xffA5A5A5 ), - onPressed: () => print('+/-'), + onPressed: () => calculatorBloc.add( ChangeNegativePositive() ), ), CalculatorButton( text: 'del', bgColor: Color(0xffA5A5A5 ), - onPressed: () => print('del'), + onPressed: () => calculatorBloc.add( DeleteLastEntry() ), ), CalculatorButton( text: '/', bgColor: Color(0xffF0A23B ), - onPressed: () => print('/'), + onPressed: () => calculatorBloc.add( OperationEntry('/') ), ), ], ), @@ -72,7 +72,7 @@ class CalculatorScreen extends StatelessWidget { CalculatorButton( text: 'X', bgColor: Color(0xffF0A23B ), - onPressed: () => print('X'), + onPressed: () => calculatorBloc.add( OperationEntry('X') ), ), ], ), @@ -82,20 +82,20 @@ class CalculatorScreen extends StatelessWidget { children: [ CalculatorButton( text: '4', - onPressed: () => print('4'), + onPressed: () => calculatorBloc.add( AddNumber('4') ), ), CalculatorButton( text: '5', - onPressed: () => print('5'), + onPressed: () => calculatorBloc.add( AddNumber('5') ), ), CalculatorButton( text: '6', - onPressed: () => print('6'), + onPressed: () => calculatorBloc.add( AddNumber('6') ), ), CalculatorButton( text: '-', bgColor: Color(0xffF0A23B ), - onPressed: () => print('-'), + onPressed: () => calculatorBloc.add( OperationEntry('-') ), ), ], ), @@ -105,20 +105,20 @@ class CalculatorScreen extends StatelessWidget { children: [ CalculatorButton( text: '1', - onPressed: () => print('1'), + onPressed: () => calculatorBloc.add( AddNumber('1') ), ), CalculatorButton( text: '2', - onPressed: () => print('2'), + onPressed: () => calculatorBloc.add( AddNumber('2') ), ), CalculatorButton( text: '3', - onPressed: () => print('3'), + onPressed: () => calculatorBloc.add( AddNumber('3') ), ), CalculatorButton( text: '+', bgColor: Color(0xffF0A23B ), - onPressed: () => print('+'), + onPressed: () => calculatorBloc.add( OperationEntry('+') ), ), ], ), @@ -129,16 +129,16 @@ class CalculatorScreen extends StatelessWidget { CalculatorButton( text: '0', big: true, - onPressed: () => print('0'), + onPressed: () => calculatorBloc.add( AddNumber('0') ), ), CalculatorButton( text: '.', - onPressed: () => print('.'), + onPressed: () => calculatorBloc.add( AddNumber('.') ), ), CalculatorButton( text: '=', bgColor: Color(0xffF0A23B ), - onPressed: () => print('='), + onPressed: () => calculatorBloc.add( CalculateResult() ), ), ], ), diff --git a/lib/widgets/results_labels.dart b/lib/widgets/results_labels.dart index 14646d3..5aee145 100644 --- a/lib/widgets/results_labels.dart +++ b/lib/widgets/results_labels.dart @@ -12,13 +12,31 @@ class ResultsLabels extends StatelessWidget { Widget build(BuildContext context) { return BlocBuilder( builder: (context, state) { + + if ( state.firstNumber == '0' && state.secondNumber == '0' ) { + return MainResultText( + text: state.mathResult.endsWith('.0') + ? state.mathResult.substring(0, state.mathResult.length - 2) + : state.mathResult + ); + } + + return Column( children: [ SubResult(text: state.firstNumber ), SubResult(text: state.operation ), - SubResult(text: state.secondNumber ), + SubResult( + text: state.secondNumber.endsWith('.0') + ? state.secondNumber.substring(0, state.secondNumber.length - 2) + : state.secondNumber + ), LineSeparator(), - MainResultText(text: state.mathResult ), + MainResultText( + text: state.mathResult.endsWith('.0') + ? state.mathResult.substring(0, state.mathResult.length - 2) + : state.mathResult + ), ], ); },