Skip to content

Instantly share code, notes, and snippets.

@antklim
Last active November 9, 2020 23:41
Show Gist options
  • Save antklim/0a027cd44c248337086e0a0efed04559 to your computer and use it in GitHub Desktop.
Save antklim/0a027cd44c248337086e0a0efed04559 to your computer and use it in GitHub Desktop.
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