Skip to content

Commit

Permalink
Merge pull request #2 from mjarkk/add-timer-tab
Browse files Browse the repository at this point in the history
TimerTab: Add timer tab
  • Loading branch information
nmcain authored Mar 19, 2022
2 parents d488cbf + 7d9c0c5 commit 669c92f
Show file tree
Hide file tree
Showing 7 changed files with 832 additions and 73 deletions.
63 changes: 63 additions & 0 deletions lib/keyboard.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2022 The dahliaOS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import 'dart:ui';

import 'package:flutter/material.dart';

class KeyboardEvents {
List<void Function(RawKeyEvent input)> listeners = [];

onKey(RawKeyEvent input) {
for (var listener in listeners) {
listener(input);
}
}
}

class KeyboardEventsListener extends StatefulWidget {
KeyboardEventsListener({
required this.keyboardEvents,
required this.child,
required this.onKey,
});

final KeyboardEvents keyboardEvents;
final Widget child;
final void Function(RawKeyEvent input) onKey;

@override
State<KeyboardEventsListener> createState() => _KeyboardEventsListenerState();
}

class _KeyboardEventsListenerState extends State<KeyboardEventsListener> {
@override
void initState() {
widget.keyboardEvents.listeners.add(widget.onKey);
super.initState();
}

@override
Widget build(BuildContext context) {
return widget.child;
}

@override
void deactivate() {
widget.keyboardEvents.listeners.remove(widget.onKey);
super.deactivate();
}
}
141 changes: 83 additions & 58 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import 'dart:ui';

import 'package:flutter/material.dart';

import './timer/tab.dart';
import './keyboard.dart';

void main() {
runApp(new Clock());
}
Expand Down Expand Up @@ -57,68 +60,84 @@ class ClockApp extends StatefulWidget {
class _ClockApp extends State<ClockApp> with TickerProviderStateMixin {
@override
Widget build(BuildContext context) {
TabController tcon = TabController(length: 2, vsync: this);
return Scaffold(
appBar: AppBar(
centerTitle: true,
elevation: 0,
toolbarHeight: 75,
title: Row(children: [
TabBar(
controller: tcon,
indicator: BoxDecoration(
color: Theme.of(context).canvasColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8), topRight: Radius.circular(8)),
),
isScrollable: true,
tabs: [
Tab(
icon: Icon(Icons.access_time),
text: "Clock",
final TabController tcon = TabController(length: 3, vsync: this);

final KeyboardEvents keyboardEvents = KeyboardEvents();

return new RawKeyboardListener(
focusNode: FocusNode(skipTraversal: true),
autofocus: true,
onKey: keyboardEvents.onKey,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
elevation: 0,
toolbarHeight: 75,
title: Row(children: [
TabBar(
controller: tcon,
indicator: BoxDecoration(
color: Theme.of(context).canvasColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8),
topRight: Radius.circular(8)),
),
isScrollable: true,
tabs: [
Tab(
icon: Icon(Icons.access_time),
text: "Clock",
),
Tab(
icon: Icon(Icons.alarm),
text: "Alarms",
),
Tab(
icon: Icon(Icons.hourglass_empty),
text: "Timer",
)
],
),
Tab(
icon: Icon(Icons.alarm),
text: "Alarms",
Expanded(child: Container()),
PopupMenuButton(
icon: Icon(Icons.more_vert),
itemBuilder: (context) => <PopupMenuEntry>[
PopupMenuItem(child: Text("Settings"), value: "settings")
],
onSelected: (value) {
if (value == "settings")
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Error"),
content: new Text("ERROR FEATURE NOT IMPLEMENTED"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
;
},
),
],
]),
),
Expanded(child: Container()),
PopupMenuButton(
icon: Icon(Icons.more_vert),
itemBuilder: (context) => <PopupMenuEntry>[
PopupMenuItem(child: Text("Settings"), value: "settings")
body: TabBarView(
controller: tcon,
children: [
WorldClockTab(),
AlarmsTab(),
TimerTab(keyboardEvents: keyboardEvents)
],
onSelected: (value) {
if (value == "settings")
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Error"),
content: new Text("ERROR FEATURE NOT IMPLEMENTED"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
;
},
),
]),
),
body: TabBarView(
controller: tcon,
children: [WorldClockTab(), AlarmsTab()],
));
)));
}
}

Expand All @@ -131,6 +150,12 @@ class _WorldClockTabState extends State<WorldClockTab> {
DateTime _datetime = DateTime.now();
Timer? _ctimer;

@override
void deactivate() {
_ctimer?.cancel();
super.deactivate();
}

@override
Widget build(BuildContext context) {
if (_ctimer == null)
Expand Down
Loading

0 comments on commit 669c92f

Please sign in to comment.