Skip to content

Commit

Permalink
Youtube parte 2 terminada
Browse files Browse the repository at this point in the history
  • Loading branch information
Klerith committed Apr 10, 2021
1 parent 0ce52fd commit 0453b3b
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 28 deletions.
75 changes: 64 additions & 11 deletions lib/bloc/calculator/calculator_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,78 @@ class CalculatorBloc extends Bloc<CalculatorEvent, CalculatorState> {
Stream<CalculatorState> 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<CalculatorState> _resetAC() async* {
yield CalculatorState(
firstNumber: '0',
mathResult: '0',
secondNumber: '0',
operation: '+'
);
}

Stream<CalculatorState> _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;
}
}

}
11 changes: 11 additions & 0 deletions lib/bloc/calculator/calculator_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
13 changes: 13 additions & 0 deletions lib/bloc/calculator/calculator_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);


}

30 changes: 15 additions & 15 deletions lib/screens/calculator_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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('/') ),
),
],
),
Expand All @@ -72,7 +72,7 @@ class CalculatorScreen extends StatelessWidget {
CalculatorButton(
text: 'X',
bgColor: Color(0xffF0A23B ),
onPressed: () => print('X'),
onPressed: () => calculatorBloc.add( OperationEntry('X') ),
),
],
),
Expand All @@ -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('-') ),
),
],
),
Expand All @@ -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('+') ),
),
],
),
Expand All @@ -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() ),
),
],
),
Expand Down
22 changes: 20 additions & 2 deletions lib/widgets/results_labels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,31 @@ class ResultsLabels extends StatelessWidget {
Widget build(BuildContext context) {
return BlocBuilder<CalculatorBloc, CalculatorState>(
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
),
],
);
},
Expand Down

0 comments on commit 0453b3b

Please sign in to comment.