Last active
November 9, 2020 23:41
-
-
Save antklim/0a027cd44c248337086e0a0efed04559 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class _CalculatorInputState extends State<CalculatorInput> { | |
final operations = <Operation, String>{ | |
Operation.addition: 'Addition', | |
... | |
}; | |
Operation operation = Operation.addition; | |
num operandA = 0; | |
num operandB = 0; | |
void onOperationChanged(Operation newOperation) { | |
setState(() { | |
operation = newOperation; | |
}); | |
widget.onChanged(calculatorValue); // notify parent about state change | |
} | |
ValueChanged<String> onOperandChanged(Operand operand) => (String v) { | |
double value = double.tryParse(v) ?? 0; | |
setState(() { | |
if (operand == Operand.A) operandA = value; | |
if (operand == Operand.B) operandB = value; | |
}); | |
widget.onChanged(calculatorValue); // notify parent about state change | |
}; | |
... // calculator value and format getters | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
child: Column( | |
children: <Widget>[ | |
Row( | |
children: <Widget>[ | |
Text('Operation', style: Theme.of(context).textTheme.bodyText2), | |
DropdownButton( | |
value: operation, | |
items: operations.entries | |
.map((entry) => DropdownMenuItem( | |
child: Text(entry.value), value: entry.key)) | |
.toList(), | |
onChanged: onOperationChanged, | |
), | |
], | |
), | |
OperandInput( | |
label: 'Operand A', | |
initValue: operandA, | |
memory: widget.memory, | |
onChanged: onOperandChanged(Operand.A)), | |
operation == Operation.sqrt | |
? SizedBox(height: 68) | |
: OperandInput(label: 'Operand B', ...), | |
Container( | |
child: Text('$calculatorFormat = ${calculatorValue.toStringAsFixed(3)}')), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment