Skip to content

Instantly share code, notes, and snippets.

@antklim
Last active November 9, 2020 21:32
Show Gist options
  • Save antklim/2483d5e1861feb97b30b9a430af585a8 to your computer and use it in GitHub Desktop.
Save antklim/2483d5e1861feb97b30b9a430af585a8 to your computer and use it in GitHub Desktop.
class OperandInput extends StatefulWidget {
final String label;
final Operand operand;
final CalculatorUseCase useCase;
final Function onChanged;
const OperandInput({Key key, this.label, this.operand, this.useCase, this.onChanged}) : super(key: key);
@override
_OperandInputState createState() => _OperandInputState();
}
class _OperandInputState extends State<OperandInput> {
TextEditingController controller;
@override
void initState() {
super.initState();
controller = TextEditingController(text: '${widget.useCase.operandValue(widget.operand)}');
}
void onFromMemory() {
if (widget.useCase.memory == null) return;
setState(() {
controller.text = '${widget.useCase.memory}';
});
widget.useCase.fromMemory(widget.operand);
widget.onChanged();
}
void onChanged(String v) {
double value = double.tryParse(v) ?? 0;
widget.useCase.setOperand(widget.operand)(value);
widget.onChanged();
}
@override
Widget build(BuildContext context) {
return Container(
...
Flexible(child: TextField(onChanged: onChanged, ...)),
Padding(child: RaisedButton(onPressed: onFromMemory, ...)),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment