From e6c31d826868a85c386f097fc2b0c2c6a4f3c749 Mon Sep 17 00:00:00 2001 From: mhmdanas Date: Mon, 6 Jun 2022 18:28:43 +0300 Subject: [PATCH] [ui] follow system time format Fixes https://github.com/Lacerte/clima/issues/292. --- lib/main.dart | 5 +++++ lib/ui/screens/weather_screen.dart | 2 +- lib/ui/widgets/weather/additional_info_widget.dart | 8 ++++++-- lib/ui/widgets/weather/hourly_forecasts_widget.dart | 5 ++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 86267bb6..e87552e5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -23,6 +23,8 @@ import 'package:clima/ui/themes/light_theme.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; +import 'package:intl/date_symbol_data_local.dart'; +import 'package:intl/intl_standalone.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sizer/sizer.dart'; @@ -34,6 +36,9 @@ Future main({ Widget Function(Widget widget)? topLevelBuilder, Locale? Function(BuildContext)? getLocale, }) async { + await findSystemLocale(); + await initializeDateFormatting(); + // Unless you do this, using method channels (like `SharedPreferences` does) // before running `runApp` throws an error. WidgetsFlutterBinding.ensureInitialized(); diff --git a/lib/ui/screens/weather_screen.dart b/lib/ui/screens/weather_screen.dart index 3cb2d568..f62135f9 100644 --- a/lib/ui/screens/weather_screen.dart +++ b/lib/ui/screens/weather_screen.dart @@ -129,7 +129,7 @@ class WeatherScreen extends HookConsumerWidget { : Row( children: [ Text( - 'Updated ${DateFormat.Md().add_jm().format(fullWeather.currentWeather.date.toLocal())} · ', + 'Updated ${DateFormat.Md().addPattern(MediaQuery.of(context).alwaysUse24HourFormat ? 'Hm' : 'jm').format(fullWeather.currentWeather.date.toLocal())} · ', style: TextStyle( color: Theme.of(context).textTheme.subtitle2!.color, fontSize: MediaQuery.of(context).size.shortestSide < diff --git a/lib/ui/widgets/weather/additional_info_widget.dart b/lib/ui/widgets/weather/additional_info_widget.dart index f87aa0b5..0a4eee95 100644 --- a/lib/ui/widgets/weather/additional_info_widget.dart +++ b/lib/ui/widgets/weather/additional_info_widget.dart @@ -41,6 +41,10 @@ class AdditionalInfoWidget extends ConsumerWidget { ), ); + final timeFormat = MediaQuery.of(context).alwaysUse24HourFormat + ? DateFormat.Hm() + : DateFormat.jm(); + return Column( children: [ Padding( @@ -107,13 +111,13 @@ class AdditionalInfoWidget extends ConsumerWidget { children: [ AdditionalInfoTile( title: 'Sunrise', - value: DateFormat.Hm().format( + value: timeFormat.format( currentDayForecast.sunrise.toUtc().add(timeZoneOffset), ), ), AdditionalInfoTile( title: 'Sunset', - value: DateFormat.Hm().format( + value: timeFormat.format( currentDayForecast.sunset.toUtc().add(timeZoneOffset), ), ), diff --git a/lib/ui/widgets/weather/hourly_forecasts_widget.dart b/lib/ui/widgets/weather/hourly_forecasts_widget.dart index 22752192..eb650663 100644 --- a/lib/ui/widgets/weather/hourly_forecasts_widget.dart +++ b/lib/ui/widgets/weather/hourly_forecasts_widget.dart @@ -44,7 +44,10 @@ class HourlyForecastsWidget extends ConsumerWidget { mainAxisAlignment: MainAxisAlignment.center, children: [ Text( - DateFormat.j().format( + (MediaQuery.of(context).alwaysUse24HourFormat + ? DateFormat.Hm() + : DateFormat.jm()) + .format( hourlyForecast.date.toUtc().add(timeZoneOffset), ), style: kSubtitle2TextStyle(context),