Skip to content

Commit 7e4fd57

Browse files
committed
WorldClockTab: Add time in other cities
This adds a button that pops up a window where you can select a city. After chosing the city the city is added to the WorldClockTab and the time in that spesific city is displayed. the selected cities are currently saved in memory thus reloading the app will cause the clock to forget what you've selected.
1 parent d488cbf commit 7e4fd57

File tree

7 files changed

+491
-38
lines changed

7 files changed

+491
-38
lines changed

lib/main.dart

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import 'dart:ui';
1919

2020
import 'package:flutter/material.dart';
2121

22+
import './worldClock/tab.dart';
23+
import './worldClock/timezones.dart';
24+
2225
void main() {
26+
setupTimezoneInfo();
2327
runApp(new Clock());
2428
}
2529

@@ -122,32 +126,6 @@ class _ClockApp extends State<ClockApp> with TickerProviderStateMixin {
122126
}
123127
}
124128

125-
class WorldClockTab extends StatefulWidget {
126-
@override
127-
_WorldClockTabState createState() => _WorldClockTabState();
128-
}
129-
130-
class _WorldClockTabState extends State<WorldClockTab> {
131-
DateTime _datetime = DateTime.now();
132-
Timer? _ctimer;
133-
134-
@override
135-
Widget build(BuildContext context) {
136-
if (_ctimer == null)
137-
_ctimer = Timer.periodic(Duration(seconds: 1), (me) {
138-
_datetime = DateTime.now();
139-
setState(() {});
140-
});
141-
return Material(
142-
child: Center(
143-
child: Text(
144-
"${_datetime.hour}:${_datetime.minute < 10 ? "0" + _datetime.minute.toString() : _datetime.minute}:${_datetime.second < 10 ? "0" + _datetime.second.toString() : _datetime.second}",
145-
style: TextStyle(fontSize: 48, fontWeight: FontWeight.bold),
146-
)),
147-
);
148-
}
149-
}
150-
151129
class AlarmsTab extends StatelessWidget {
152130
@override
153131
Widget build(BuildContext context) {
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
Copyright 2022 The dahliaOS Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import 'package:flutter/material.dart';
18+
19+
import './timezones.dart';
20+
21+
class AddTimezoneScreen extends StatefulWidget {
22+
const AddTimezoneScreen({required this.onSelect});
23+
24+
final void Function(CityTimeZone cityTZ) onSelect;
25+
26+
@override
27+
State<AddTimezoneScreen> createState() => _AddTimezoneScreenState();
28+
}
29+
30+
class _AddTimezoneScreenState extends State<AddTimezoneScreen> {
31+
List<CityTimeZone> options = timezonesOfCities;
32+
33+
onInput(String query) {
34+
String normalizedQuery = query.toLowerCase();
35+
options = timezonesOfCities
36+
.where((e) => e.city.toLowerCase().contains(normalizedQuery))
37+
.toList();
38+
setState(() {});
39+
}
40+
41+
onSearchSubmit() {
42+
if (options.isNotEmpty) onSelect(options.first);
43+
}
44+
45+
onSelect(CityTimeZone option) {
46+
widget.onSelect(option);
47+
Navigator.of(context).pop();
48+
}
49+
50+
@override
51+
Widget build(BuildContext context) {
52+
return Scaffold(
53+
appBar: AppBar(
54+
centerTitle: true,
55+
elevation: 0,
56+
toolbarHeight: 75,
57+
title: Text('Choose a city'),
58+
),
59+
body: Column(
60+
children: [
61+
_SearchField(
62+
onInput: onInput,
63+
onSubmit: onSearchSubmit,
64+
),
65+
_Options(options: options, onSelect: onSelect),
66+
],
67+
),
68+
);
69+
}
70+
}
71+
72+
class _Options extends StatelessWidget {
73+
const _Options({required this.options, this.onSelect});
74+
75+
final List<CityTimeZone> options;
76+
final void Function(CityTimeZone)? onSelect;
77+
78+
@override
79+
Widget build(BuildContext context) {
80+
return Expanded(
81+
child: ListView.builder(
82+
itemCount: options.length,
83+
itemBuilder: (BuildContext context, int index) {
84+
final CityTimeZone option = options[index];
85+
86+
return TextButton(
87+
key: Key(index.toString()),
88+
style: TextButton.styleFrom(
89+
alignment: Alignment.centerLeft,
90+
padding: const EdgeInsets.all(20),
91+
),
92+
child: Column(
93+
crossAxisAlignment: CrossAxisAlignment.start,
94+
children: [
95+
Text(
96+
option.city,
97+
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
98+
),
99+
Text(
100+
option.prettyOffset,
101+
style: TextStyle(fontSize: 12),
102+
),
103+
],
104+
),
105+
onPressed: onSelect != null ? () => onSelect!(option) : null,
106+
);
107+
},
108+
),
109+
);
110+
}
111+
}
112+
113+
class _SearchField extends StatelessWidget {
114+
const _SearchField({required this.onInput, this.onSubmit});
115+
116+
final void Function(String s) onInput;
117+
final void Function()? onSubmit;
118+
119+
@override
120+
Widget build(BuildContext context) {
121+
return Padding(
122+
padding: const EdgeInsets.all(24),
123+
child: TextField(
124+
autofocus: true,
125+
decoration: InputDecoration(labelText: "City"),
126+
onChanged: onInput,
127+
onSubmitted: onSubmit != null ? (String s) => onSubmit!() : null,
128+
),
129+
);
130+
}
131+
}

0 commit comments

Comments
 (0)