This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CalculatorUseCase { | |
... | |
// callbacks to notify state changes | |
void Function() onMemoryChanged; | |
void Function() onOperationChanged; | |
CalculatorUseCase( | |
{Operation operation, | |
this.operandA = 0, | |
this.operandB = 0, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CalculatorState extends ChangeNotifier { | |
CalculatorUseCase _useCase; | |
CalculatorState() { | |
_useCase = CalculatorUseCase( | |
onMemoryChanged: onMemoryChanged, | |
onOperationChanged: onOperationChanged); | |
} | |
void onMemoryChanged() => notifyListeners(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CalculusScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return ChangeNotifierProvider<CalculatorState>( | |
create: (_) => CalculatorState(), | |
builder: (context, _) => CalculusScreenContainer()); | |
} | |
} | |
class CalculusScreenContainer extends StatelessWidget { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>[ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |