Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markus Bittner: Calc stuff #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 111 additions & 3 deletions 001_calculator/lib/feature/calculator/calculator_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,122 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:function_tree/function_tree.dart';

/// The state of the calculator
class CalculatorState {
// TODO(dkbast): implement state
abstract class CalculatorState {

/// lala
String get display => '0';
}

/// The reset state
class ResetState extends CalculatorState {}

/// The display update state
class UpdateDisplayState extends CalculatorState {

/// lala
UpdateDisplayState({required this.display});

/// lalal
@override
String display;
}

/// The reset state
class DivideByZeroState extends CalculatorState {
@override
String get display => 'lala';
}

/// The business logic of the calculator
class CalculatorCubit extends Cubit<CalculatorState> {
/// The business logic of the calculator
CalculatorCubit() : super(CalculatorState());
CalculatorCubit() : super(ResetState());

/// number
String number = '0';

// TODO(dkbast): implement cubit
/// do something
void doSomethingWithKey(String key) {
switch (key) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
addNumber(key);
break;
case 'C':
resetDisplay();
break;
case '+':
case '-':
case '/':
case '*':
case '^':
addOperator(key);
break;
case '=':
calculateStuff(key);
break;
case '+/-':
toggleSign();
break;
default:
break;
}
}

void _updateDisplayState() {
emit(UpdateDisplayState(display: number));
}

/// lala
void toggleSign() {
if (number.startsWith('-')) {
number = number.replaceFirst('-', '');
} else {
number = '-$number';
}
_updateDisplayState();
}

/// Adds number i swear
void addNumber(String number) {
if (this.number == '0' && number == '0') {
return;
}

if (this.number == '0') {
this.number = number;
} else {
this.number += number;
}
_updateDisplayState();
}

/// lala
void calculateStuff(String operator) {
number = number.interpret().toString();
_updateDisplayState();
}

/// Lala
void addOperator(String operator) {
number += operator;
_updateDisplayState();
}

/// resets display
void resetDisplay() {
number = '0';
emit(ResetState());
}
}
245 changes: 125 additions & 120 deletions 001_calculator/lib/feature/calculator/calculator_view.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// ignore_for_file: library_private_types_in_public_api

import 'package:calculator/feature/calculator/calculator.dart';
import 'package:calculator/feature/calculator/widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

/// The view of the calculator
/// If a user wants to use the calculator, this is the widget they should use
Expand All @@ -17,137 +19,140 @@ class CalculatorView extends StatefulWidget {
class _CalculatorPageState extends State<CalculatorView> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Calculator'),
),
body: Center(
return BlocProvider<CalculatorCubit>(
create: (context) => CalculatorCubit(),
child: Scaffold(
appBar: AppBar(
title: const Text('Calculator'),
),
body: Center(
// A Column Widget that contains the display and the keypad
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// A Text Widget that displays the current number
Text(
'0',
style: Theme.of(context).textTheme.displayLarge,
),
const _CalculatorDisplay(),
// A Row Widget that contains the keypad
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// A Column Widget that contains the first row of buttons
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// A Row Widget that contains the first row of buttons
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Button(
text: '7',
onPressed: () {},
),
Button(
text: '8',
onPressed: () {},
),
Button(
text: '9',
onPressed: () {},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
_CalcButton(text: '7'),
_CalcButton(text: '8'),
_CalcButton(text: '9'),
],
),
// A Row Widget that contains the second row of buttons
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Button(
text: '4',
onPressed: () {},
),
Button(
text: '5',
onPressed: () {},
),
Button(
text: '6',
onPressed: () {},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
_CalcButton(text: '4'),
_CalcButton(text: '5'),
_CalcButton(text: '6'),
],
),
// A Row Widget that contains the third row of buttons
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Button(
text: '1',
onPressed: () {},
),
Button(
text: '2',
onPressed: () {},
),
Button(
text: '3',
onPressed: () {},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
_CalcButton(text: '1'),
_CalcButton(text: '2'),
_CalcButton(text: '3'),
],
),
// A Row Widget that contains the fourth row of buttons
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Button(
text: '0',
onPressed: () {},
),
Button(
text: '.',
onPressed: () {},
),
Button(
text: '+/-',
onPressed: () {},
),
],
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
_CalcButton(text: '0'),
_CalcButton(text: '.'),
_CalcButton(text: '+/-'),
],
),
],
),
// A Column Widget that contains the second row of buttons
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Button(
text: '+',
onPressed: () {},
),
Button(
text: '-',
onPressed: () {},
),
Button(
text: '*',
onPressed: () {},
),
Button(
text: '/',
onPressed: () {},
),
Button(
text: '=',
onPressed: () {},
),
Button(
text: 'C',
onPressed: () {},
),
],
),
],
),
],
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
_CalcButton(
text: '+',
),
_CalcButton(
text: '-',
),
_CalcButton(
text: '*',
),
_CalcButton(
text: '/',
),
_CalcButton(
text: '^',
),
_CalcButton(
text: '=',
),
_CalcButton(
text: 'C',
),
],
),
],
),
],
),
),
),
);
}
}

/// Counter display widget
class _CalculatorDisplay extends StatefulWidget {
/// Contructor
const _CalculatorDisplay({super.key});

@override
State<_CalculatorDisplay> createState() => _CalculatorDisplayState();
}

class _CalculatorDisplayState extends State<_CalculatorDisplay> {
@override
Widget build(BuildContext context) {
return BlocBuilder<CalculatorCubit, CalculatorState>(
builder: (context, state) {
return Text(
key: const ValueKey('CalculatorDisplay'),
state.display,
style: Theme.of(context).textTheme.displayLarge,
);
});
}
}

/// lala
class _CalcButton extends StatelessWidget {
/// lala
const _CalcButton({super.key, required this.text});

/// lala
final String text;

@override
Widget build(BuildContext context) {
return Button(
key: ValueKey(text),
text: text,
onPressed: (text) =>
context.read<CalculatorCubit>().doSomethingWithKey(text),
);
}
}
Loading