To change the state of EntryTextbox
widget when a button was pressed, declare a containing widget (I suppose it's CalculatorFrame
) as StatefullWidget
with String
or whatever field, which you will initialize EntryTextbox
from. Implement a function that changes that field when a button is pressed, and pass it as a constructor parameter to a NumberButton
widget.
Here is a schematic example:
import 'package:flutter/material.dart';
class CalculatorFrame extends StatefulWidget {
@override
_CalculatorFrameState createState() => _CalculatorFrameState();
}
class _CalculatorFrameState extends State<CalculatorFrame> {
String _text;
@override
Widget build(BuildContext context) => ...( // container widget of your choice
EntryTextbox(text: _text),
NumberButton(text: '0', onPressed: _onPressed,),
...,
NumberButton(text: '9', onPressed: _onPressed,),
);
void _onPressed(String buttonText) =>
setState(() => _text += buttonText);
}
class EntryTextbox extends StatelessWidget {
final String text;
const EntryTextbox({Key key, this.text}) : super(key: key);
@override
Widget build(BuildContext context) => ...(text: text); // text entry widget of your choice
}
class NumberButton extends StatelessWidget {
final String text;
final Function(String) onPressed;
const NumberButton({Key key, this.onPressed, this.text}) : super(key: key);
@override
Widget build(BuildContext context) => ...Button( // button widget of your choice
child: Text(text),
onPressed: () => onPressed(text),
);
}
Since in your example NumberButton
widgets sit inside NumberBoards
widget, it's NumberBoards
responsibility to accept onPressed
as a constructor parameter and pass it to each NumberButton
constructor.
In my example, onPressed
accepts String
parameter - text of the button which was pressed. You might decide to pass int
or perhaps a more complex data class.
This non-architectural approach works fine for tiny apps and samples. However, I'd rather recommend using BLoC or ScopedModel for your task. Simple app state management section of the official documentation might be helpful.