|
| 1 | +import 'package:animated_emoji/animated_emoji.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:watch_it/watch_it.dart'; |
| 4 | +import 'package:yaru/yaru.dart'; |
| 5 | + |
| 6 | +import '../../../string_x.dart'; |
| 7 | +import '../../build_context_x.dart'; |
| 8 | +import '../weather_model.dart'; |
| 9 | +import 'city_search_field.dart'; |
| 10 | + |
| 11 | +class ErrorView extends StatefulWidget { |
| 12 | + const ErrorView({super.key, required this.error}); |
| 13 | + |
| 14 | + final String error; |
| 15 | + |
| 16 | + @override |
| 17 | + State<ErrorView> createState() => _ErrorViewState(); |
| 18 | +} |
| 19 | + |
| 20 | +class _ErrorViewState extends State<ErrorView> { |
| 21 | + late TextEditingController _controller; |
| 22 | + |
| 23 | + @override |
| 24 | + void initState() { |
| 25 | + super.initState(); |
| 26 | + _controller = TextEditingController(); |
| 27 | + } |
| 28 | + |
| 29 | + @override |
| 30 | + void dispose() { |
| 31 | + _controller.dispose(); |
| 32 | + super.dispose(); |
| 33 | + } |
| 34 | + |
| 35 | + @override |
| 36 | + Widget build(BuildContext context) { |
| 37 | + final model = di<WeatherModel>(); |
| 38 | + |
| 39 | + return Center( |
| 40 | + child: Padding( |
| 41 | + padding: const EdgeInsets.all(kYaruPagePadding), |
| 42 | + child: Column( |
| 43 | + mainAxisSize: MainAxisSize.min, |
| 44 | + children: [ |
| 45 | + AnimatedEmoji( |
| 46 | + widget.error.emptyCity |
| 47 | + ? AnimatedEmojis.sunWithFace |
| 48 | + : widget.error.cityNotFound |
| 49 | + ? AnimatedEmojis.thinkingFace |
| 50 | + : widget.error.invalidKey |
| 51 | + ? AnimatedEmojis.disguise |
| 52 | + : AnimatedEmojis.xEyes, |
| 53 | + size: 40, |
| 54 | + ), |
| 55 | + const SizedBox( |
| 56 | + height: kYaruPagePadding, |
| 57 | + ), |
| 58 | + if (widget.error.invalidKey) |
| 59 | + Flexible( |
| 60 | + child: Row( |
| 61 | + mainAxisSize: MainAxisSize.min, |
| 62 | + children: [ |
| 63 | + SizedBox( |
| 64 | + width: 300, |
| 65 | + child: TextField( |
| 66 | + onChanged: (v) => _controller.text = v, |
| 67 | + decoration: InputDecoration( |
| 68 | + suffixIconConstraints: const BoxConstraints( |
| 69 | + maxHeight: kYaruTitleBarItemHeight, |
| 70 | + minHeight: kYaruTitleBarItemHeight, |
| 71 | + minWidth: 45, |
| 72 | + ), |
| 73 | + suffixIcon: Tooltip( |
| 74 | + message: 'Save', |
| 75 | + child: ClipRRect( |
| 76 | + borderRadius: const BorderRadius.only( |
| 77 | + topRight: Radius.circular(kYaruButtonRadius), |
| 78 | + bottomRight: Radius.circular(kYaruButtonRadius), |
| 79 | + ), |
| 80 | + child: Material( |
| 81 | + color: Colors.transparent, |
| 82 | + child: InkWell( |
| 83 | + onTap: () => model.setApiKeyAndLoadWeather( |
| 84 | + _controller.text.toString(), |
| 85 | + ), |
| 86 | + child: const Center( |
| 87 | + widthFactor: 0, |
| 88 | + child: Icon(YaruIcons.save), |
| 89 | + ), |
| 90 | + ), |
| 91 | + ), |
| 92 | + ), |
| 93 | + ), |
| 94 | + errorMaxLines: 5, |
| 95 | + errorText: widget.error.invalidKey |
| 96 | + ? 'Please enter a valid API key' |
| 97 | + : widget.error, |
| 98 | + border: const OutlineInputBorder( |
| 99 | + borderSide: BorderSide.none, |
| 100 | + ), |
| 101 | + enabledBorder: const OutlineInputBorder( |
| 102 | + borderSide: BorderSide.none, |
| 103 | + ), |
| 104 | + fillColor: context.theme.colorScheme.surface |
| 105 | + .withOpacity(0.3), |
| 106 | + label: const Text('OpenWeather API key'), |
| 107 | + ), |
| 108 | + ), |
| 109 | + ), |
| 110 | + ], |
| 111 | + ), |
| 112 | + ) |
| 113 | + else if (widget.error.emptyCity || widget.error.cityNotFound) |
| 114 | + const SizedBox( |
| 115 | + width: 300, |
| 116 | + child: CitySearchField( |
| 117 | + watchError: true, |
| 118 | + ), |
| 119 | + ) |
| 120 | + else |
| 121 | + Text( |
| 122 | + widget.error, |
| 123 | + textAlign: TextAlign.center, |
| 124 | + ), |
| 125 | + ], |
| 126 | + ), |
| 127 | + ), |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
0 commit comments