Skip to content

Commit

Permalink
(#1) Implement initial editing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrintt committed Oct 16, 2021
1 parent 3706121 commit 8bdd63f
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 96 deletions.
2 changes: 1 addition & 1 deletion shinoa/lib/view/calculator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class _CalculatorState extends State<Calculator> {
backgroundColor: kBackgroundColor,
systemOverlayStyle: SystemUiOverlayStyle.dark,
elevation: 0,
title: const ContextMenu(),
title: ContextMenu(controller: _controller),
),
body: Column(
children: [
Expand Down
80 changes: 64 additions & 16 deletions shinoa/lib/view/context_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'package:flutter/material.dart';
import 'package:shinoa/theme.dart';

class ContextMenu extends StatefulWidget {
const ContextMenu({Key? key}) : super(key: key);
final TextEditingController controller;

const ContextMenu({Key? key, required this.controller}) : super(key: key);

@override
_ContextMenuState createState() => _ContextMenuState();
Expand All @@ -12,6 +14,8 @@ class ContextMenu extends StatefulWidget {
class _ContextMenuState extends State<ContextMenu> {
static const _kButtonLabelPadding = 12.0;

TextEditingController get _controller => widget.controller;

Widget _buildMenuButton() {
return Row(
children: [
Expand All @@ -33,24 +37,68 @@ class _ContextMenuState extends State<ContextMenu> {
);
}

void _backspace() {
final text = _controller.text;
final textSelection = _controller.selection;
final selectionLength = textSelection.end - textSelection.start;
// There is a selection.
if (selectionLength > 0) {
final newText = text.replaceRange(
textSelection.start,
textSelection.end,
'',
);
_controller.text = newText;
_controller.selection = textSelection.copyWith(
baseOffset: textSelection.start,
extentOffset: textSelection.start,
);
return;
}
// The cursor is at the beginning.
if (textSelection.start == 0) {
return;
}
// Delete the previous character
final previousCodeUnit = text.codeUnitAt(textSelection.start - 1);
final offset = _isUtf16Surrogate(previousCodeUnit) ? 2 : 1;
final newStart = textSelection.start - offset;
final newEnd = textSelection.start;
final newText = text.replaceRange(
newStart,
newEnd,
'',
);
_controller.text = newText;
_controller.selection = textSelection.copyWith(
baseOffset: newStart,
extentOffset: newStart,
);
}

bool _isUtf16Surrogate(int value) => value & 0xF800 == 0xD800;

Widget _buildBackspaceButton() {
return Row(
children: [
const Text(
'DELETE',
style: TextStyle(
color: kTextColor,
fontSize: 16,
return GestureDetector(
onTap: _backspace,
child: Row(
children: [
const Text(
'DELETE',
style: TextStyle(
color: kTextColor,
fontSize: 16,
),
),
),
Padding(
padding: const EdgeInsets.only(left: _kButtonLabelPadding),
child: Icon(
Icons.backspace_outlined,
color: kAccentLightColor,
Padding(
padding: const EdgeInsets.only(left: _kButtonLabelPadding),
child: Icon(
Icons.backspace_outlined,
color: kAccentLightColor,
),
),
),
],
],
),
);
}

Expand Down
1 change: 1 addition & 0 deletions shinoa/lib/view/display.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class _DisplayState extends State<Display> {
controller: _controller,
readOnly: true,
showCursor: true,
autofocus: true,
),
),
],
Expand Down
163 changes: 84 additions & 79 deletions shinoa/lib/view/keyboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,99 +51,104 @@ class _KeyBoardState extends State<KeyBoard> {

@override
Widget build(BuildContext context) {
return Column(children: <Widget>[
Row(
children: [
Container(
width: MediaQuery.of(context).size.width,
height: 25,
color: kAccentColor,
)
],
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: GridView.builder(
shrinkWrap: true,
itemCount: buttons.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4),
itemBuilder: (BuildContext contex, int index) {
bool equal = buttons[index] == '=';
bool ce = buttons[index] == 'C';
return Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
style: ce
? ButtonStyle(
backgroundColor:
MaterialStateProperty.all(kBackgroundColor),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
return Column(
children: <Widget>[
Row(
children: [
Container(
width: MediaQuery.of(context).size.width,
height: 25,
color: kAccentColor,
)
],
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: GridView.builder(
shrinkWrap: true,
itemCount: buttons.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4),
itemBuilder: (BuildContext contex, int index) {
bool equal = buttons[index] == '=';
bool ce = buttons[index] == 'C';
return Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
style: ce
? ButtonStyle(
backgroundColor:
MaterialStateProperty.all(kBackgroundColor),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
side: const BorderSide(color: kPaperColor)),
),
)
: equal
? ButtonStyle(
backgroundColor:
MaterialStateProperty.all(kAccentColor),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
side: const BorderSide(
color: kPaperColor,
),
),
)
: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(kPaperColor),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
),
)
: equal
? ButtonStyle(
backgroundColor:
MaterialStateProperty.all(kAccentColor),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
),
),
)
: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(kPaperColor),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
),
),
),
),
onPressed: () {
if (buttons[index] == 'C') {
clearScreenOp();
clearScreenResult();
} else if (buttons[index] == 'DEL') {
removeLastChar();
} else if (buttons[index] == '=') {
equalPressed();
} else if (isOperator(previus) &&
isOperator(buttons[index])) {
return;
} else {
setState(() {
screenOperation += buttons[index];
previus = buttons[index];
});
}
},
child: Text(
buttons[index],
style: const TextStyle(fontSize: 20),
onPressed: () {
if (buttons[index] == 'C') {
_controller.clear();

/// clearScreenOp();
/// clearScreenResult();
} else if (buttons[index] == 'DEL') {
/// _backspace();
} else if (buttons[index] == '=') {
equalPressed();
} else if (isOperator(previus) &&
isOperator(buttons[index])) {
return;
} else {
_insertText(buttons[index]);
}
},
child: Text(
buttons[index],
style: const TextStyle(fontSize: 20),
),
),
),
);
},
);
},
),
),
),
),
]);
],
);
}

void _insertText(String text) {
final text = _controller.text;
final controllerText = _controller.text;
final textSelection = _controller.selection;

final newText =
text.replaceRange(textSelection.start, textSelection.end, text);
final newText = controllerText.replaceRange(
textSelection.start,
textSelection.end,
text,
);

final textLength = text.length;

_controller.text = newText;
_controller.selection = textSelection.copyWith(
baseOffset: textSelection.start + textLength,
Expand Down

0 comments on commit 8bdd63f

Please sign in to comment.