|
| 1 | +/// 24 小時預報。 |
| 2 | +library; |
| 3 | + |
| 4 | +import 'package:dpip/app/new_home/_models/home_model.dart'; |
| 5 | +import 'package:dpip/core/i18n.dart'; |
| 6 | +import 'package:dpip/utils/extensions/build_context.dart'; |
| 7 | +import 'package:dpip/utils/log.dart'; |
| 8 | +import 'package:dpip/widgets/responsive/responsive_container.dart'; |
| 9 | +import 'package:flutter/material.dart'; |
| 10 | +import 'package:material_symbols_icons/material_symbols_icons.dart'; |
| 11 | +import 'package:provider/provider.dart'; |
| 12 | + |
| 13 | +const double _kColumnWidth = 56.0; |
| 14 | + |
| 15 | +class Forecast extends StatelessWidget { |
| 16 | + const Forecast({super.key}); |
| 17 | + |
| 18 | + @override |
| 19 | + Widget build(BuildContext context) { |
| 20 | + return Selector<HomeModel, Map<String, dynamic>?>( |
| 21 | + selector: (_, m) => m.forecast, |
| 22 | + builder: (context, forecast, _) { |
| 23 | + if (forecast == null) return const SizedBox.shrink(); |
| 24 | + try { |
| 25 | + final data = forecast['forecast'] as List<dynamic>?; |
| 26 | + if (data == null || data.isEmpty) return const SizedBox.shrink(); |
| 27 | + |
| 28 | + return ResponsiveContainer( |
| 29 | + child: Container( |
| 30 | + margin: const .symmetric(horizontal: 16, vertical: 8), |
| 31 | + decoration: BoxDecoration( |
| 32 | + color: context.colors.surfaceContainerLow, |
| 33 | + borderRadius: .circular(16), |
| 34 | + border: Border.all( |
| 35 | + color: context.colors.outlineVariant.withValues(alpha: 0.5), |
| 36 | + ), |
| 37 | + ), |
| 38 | + child: Padding( |
| 39 | + padding: const .symmetric(vertical: 14), |
| 40 | + child: Column( |
| 41 | + crossAxisAlignment: .start, |
| 42 | + children: [ |
| 43 | + Padding( |
| 44 | + padding: const .symmetric(horizontal: 14), |
| 45 | + child: Text( |
| 46 | + '24 小時預報'.i18n, |
| 47 | + style: context.texts.labelMedium?.copyWith( |
| 48 | + color: context.colors.onSurfaceVariant, |
| 49 | + fontWeight: .w600, |
| 50 | + letterSpacing: 0.5, |
| 51 | + ), |
| 52 | + ), |
| 53 | + ), |
| 54 | + const SizedBox(height: 12), |
| 55 | + SingleChildScrollView( |
| 56 | + scrollDirection: .horizontal, |
| 57 | + padding: const .symmetric(horizontal: 6), |
| 58 | + child: Row( |
| 59 | + crossAxisAlignment: .start, |
| 60 | + children: [ |
| 61 | + for (var i = 0; i < data.length; i++) |
| 62 | + _HourColumn( |
| 63 | + item: data[i] as Map<String, dynamic>, |
| 64 | + isNow: i == 0, |
| 65 | + ), |
| 66 | + ], |
| 67 | + ), |
| 68 | + ), |
| 69 | + ], |
| 70 | + ), |
| 71 | + ), |
| 72 | + ), |
| 73 | + ); |
| 74 | + } catch (e, s) { |
| 75 | + TalkerManager.instance.error('Failed to render forecast card', e, s); |
| 76 | + } |
| 77 | + return const SizedBox.shrink(); |
| 78 | + }, |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +class _HourColumn extends StatelessWidget { |
| 84 | + final Map<String, dynamic> item; |
| 85 | + final bool isNow; |
| 86 | + |
| 87 | + const _HourColumn({required this.item, required this.isNow}); |
| 88 | + |
| 89 | + static (IconData, Color?) _weatherIcon(BuildContext context, String weather) => switch (weather) { |
| 90 | + final s when s.contains('晴') => (Symbols.sunny_rounded, Colors.orangeAccent), |
| 91 | + final s when s.contains('雨') => (Symbols.rainy_light_rounded, Colors.blueAccent), |
| 92 | + final s when s.contains('雲') || s.contains('陰') => ( |
| 93 | + Symbols.cloud_rounded, |
| 94 | + context.colors.onSurfaceVariant, |
| 95 | + ), |
| 96 | + final s when s.contains('雷') => (Symbols.flash_on_rounded, Colors.amber), |
| 97 | + final s when s.contains('雪') => (Symbols.snowflake_rounded, Colors.lightBlue[200]), |
| 98 | + _ => (Symbols.wb_cloudy_rounded, context.colors.onSurfaceVariant), |
| 99 | + }; |
| 100 | + |
| 101 | + @override |
| 102 | + Widget build(BuildContext context) { |
| 103 | + final time = item['time'] as String? ?? ''; |
| 104 | + final weather = item['weather'] as String? ?? ''; |
| 105 | + final temp = (item['temperature'] as num?)?.toDouble() ?? 0.0; |
| 106 | + final pop = item['pop'] as int? ?? 0; |
| 107 | + |
| 108 | + final (icon, color) = _weatherIcon(context, weather); |
| 109 | + |
| 110 | + final primaryColor = isNow |
| 111 | + ? context.colors.onSurface |
| 112 | + : context.colors.onSurface.withValues(alpha: 0.82); |
| 113 | + final labelColor = isNow |
| 114 | + ? context.colors.onSurface |
| 115 | + : context.colors.onSurfaceVariant.withValues(alpha: 0.75); |
| 116 | + |
| 117 | + return SizedBox( |
| 118 | + width: _kColumnWidth, |
| 119 | + child: Column( |
| 120 | + mainAxisSize: .min, |
| 121 | + children: [ |
| 122 | + Text( |
| 123 | + isNow ? '現在'.i18n : time, |
| 124 | + style: context.texts.labelSmall?.copyWith( |
| 125 | + color: labelColor, |
| 126 | + fontWeight: isNow ? .w700 : .w500, |
| 127 | + height: 1, |
| 128 | + ), |
| 129 | + ), |
| 130 | + const SizedBox(height: 10), |
| 131 | + Icon(icon, color: color, fill: 1, size: 24), |
| 132 | + if (pop > 0) ...[ |
| 133 | + const SizedBox(height: 6), |
| 134 | + Row( |
| 135 | + mainAxisSize: .min, |
| 136 | + mainAxisAlignment: .center, |
| 137 | + children: [ |
| 138 | + Icon( |
| 139 | + Symbols.water_drop_rounded, |
| 140 | + size: 10, |
| 141 | + color: Colors.blueAccent.withValues(alpha: 0.85), |
| 142 | + ), |
| 143 | + const SizedBox(width: 2), |
| 144 | + Text( |
| 145 | + '$pop%', |
| 146 | + style: TextStyle( |
| 147 | + fontSize: 10, |
| 148 | + color: Colors.blueAccent.withValues(alpha: 0.85), |
| 149 | + fontWeight: .w600, |
| 150 | + height: 1, |
| 151 | + ), |
| 152 | + ), |
| 153 | + ], |
| 154 | + ), |
| 155 | + ], |
| 156 | + const SizedBox(height: 10), |
| 157 | + Text( |
| 158 | + '${temp.round()}°', |
| 159 | + style: context.texts.titleMedium?.copyWith( |
| 160 | + fontWeight: isNow ? .w700 : .w600, |
| 161 | + color: primaryColor, |
| 162 | + height: 1, |
| 163 | + ), |
| 164 | + ), |
| 165 | + ], |
| 166 | + ), |
| 167 | + ); |
| 168 | + } |
| 169 | +} |
0 commit comments