Skip to content

Instantly share code, notes, and snippets.

typedef Operator = num Function(num) Function(num);
typedef OperatorFmt = String Function(num) Function(num);
class Operation {
final String name;
final Operator oper;
final OperatorFmt format;
Operation({this.name, this.oper, this.format});
class CalculatorUseCase {
num memory;
Operation operation; // an instance of Operation class
num operandA;
num operandB;
CalculatorUseCase(
{Operation operation, this.operandA = 0, this.operandB = 0}) {
this.operation = operation ?? Add;
}
class _CalculatorInputState extends State<CalculatorInput> {
// Map replaced with the list
final operations = <Operation>[Add, Sub, Mul, Div, Sqrt];
...
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
Row(
class CalculatorUseCase {
...
// callbacks to notify state changes
void Function() onMemoryChanged;
void Function() onOperationChanged;
CalculatorUseCase(
{Operation operation,
this.operandA = 0,
this.operandB = 0,
class CalculatorState extends ChangeNotifier {
CalculatorUseCase _useCase;
CalculatorState() {
_useCase = CalculatorUseCase(
onMemoryChanged: onMemoryChanged,
onOperationChanged: onOperationChanged);
}
void onMemoryChanged() => notifyListeners();
class CalculusScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<CalculatorState>(
create: (_) => CalculatorState(),
builder: (context, _) => CalculusScreenContainer());
}
}
class CalculusScreenContainer extends StatelessWidget {
class CalculatorInput extends StatelessWidget { // StatefulWidget replaced with StatelessWidget
@override
Widget build(BuildContext context) {
CalculatorState state = Provider.of<CalculatorState>(context);
final operations = <Operation>[Add, Sub, Mul, Div, Sqrt];
return Container(
child: Column(
children: <Widget>[
void main() {
group('Calculus screen', () {
...
testWidgets('has one operand when operation is SQRT', (WidgetTester tester) async {
// initiate required state
CalculatorState state = CalculatorState();
state.setOperation(Sqrt);
await tester.pumpWidget(MaterialApp(
home: ChangeNotifierProvider<CalculatorState>(
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
func Routes(client HTTPClient) map[string]http.HandlerFunc {
return map[string]http.HandlerFunc{
"/do": doHandler(),
"/remote": remoteHandler(client),
"/": notFoundHandler,
}
func Start(address string) error {
mux := http.NewServeMux()
client := &http.Client{}
for route, handler := range Routes(client) {
mux.Handle(route, handler)
}
s := &http.Server{
Addr: address,