Skip to content

Instantly share code, notes, and snippets.

@antklim
Created November 3, 2020 02:02
Show Gist options
  • Select an option

  • Save antklim/bec52eb2d4b31e780574025b32b91e86 to your computer and use it in GitHub Desktop.

Select an option

Save antklim/bec52eb2d4b31e780574025b32b91e86 to your computer and use it in GitHub Desktop.
enum Operation { addition, subtraction, division, multiplication, sqrt }
class CalculatorInput extends StatefulWidget {
@override
_CalculatorInputState createState() => _CalculatorInputState();
}
class _CalculatorInputState extends State<CalculatorInput> {
final operations = <Operation, String>{
Operation.addition: 'Addition',
...
};
Operation operation = Operation.addition;
void onOperationChanged(Operation newOperation) {
setState(() {
operation = newOperation;
});
}
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(top: 10),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
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,
),
],
),
Operand(label: 'Operand A', initValue: 0),
operation == Operation.sqrt
? SizedBox(height: 68)
: Operand(label: 'Operand B', initValue: 0),
Container(
margin: const EdgeInsets.only(top: 10, bottom: 20),
child: Text('0 + 0 = 0.000')),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment