|
| 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