Skip to content

Commit 1c28447

Browse files
committed
cleanup & fuck .of(context)
1 parent 76b28f1 commit 1c28447

37 files changed

Lines changed: 545 additions & 857 deletions

File tree

lib/app/home/_widgets/blurred_button.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class BlurredTextButton extends StatelessWidget {
4848
child: TextButton(
4949
style: TextButton.styleFrom(
5050
shape: const StadiumBorder(),
51-
foregroundColor: Theme.of(context).brightness == Brightness.dark
51+
foregroundColor: context.theme.brightness == .dark
5252
? const Color.fromARGB(199, 250, 250, 250)
5353
: const Color.fromARGB(255, 50, 50, 50),
5454
textStyle: textStyle,

lib/app/home/_widgets/date_timeline_item.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class DateTimelineItem extends StatelessWidget {
2525

2626
final RenderBox? button = context.findRenderObject() as RenderBox?;
2727
final RenderBox? overlay =
28-
Navigator.of(context).overlay?.context.findRenderObject() as RenderBox?;
28+
context.navigator.overlay?.context.findRenderObject() as RenderBox?;
2929

3030
if (button == null || overlay == null) return;
3131

lib/app/home/_widgets/hero_weather.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class HeroWeather extends StatelessWidget {
1919

2020
@override
2121
Widget build(BuildContext context) {
22-
final screenHeight = MediaQuery.of(context).size.height;
23-
final statusBarHeight = MediaQuery.of(context).padding.top;
22+
final screenHeight = context.dimension.height;
23+
final statusBarHeight = context.padding.top;
2424

2525
return SizedBox(
2626
height: screenHeight * 0.5,

lib/app/home/_widgets/mode_toggle_button.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ModeToggleButton extends StatelessWidget {
5454
void _showModeMenu(BuildContext context) {
5555
final RenderBox? button = context.findRenderObject() as RenderBox?;
5656
final RenderBox? overlay =
57-
Navigator.of(context).overlay?.context.findRenderObject() as RenderBox?;
57+
context.navigator.overlay?.context.findRenderObject() as RenderBox?;
5858

5959
if (button == null || overlay == null) return;
6060

lib/app/home/_widgets/thunderstorm_card.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class ThunderstormCard extends StatelessWidget {
105105
child: Material(
106106
color: Colors.transparent,
107107
child: InkWell(
108-
onTap: () => Navigator.of(context).push(
108+
onTap: () => context.navigator.push(
109109
MaterialPageRoute(
110110
builder: (context) => ThunderstormPage(item: history),
111111
),

lib/app/home/page.dart

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
123123

124124
if (isFirstMeasure && mounted) {
125125
WidgetsBinding.instance.addPostFrameCallback((_) {
126-
final screenHeight = MediaQuery.of(context).size.height;
126+
final screenHeight = context.dimension.height;
127127
final targetSize = (height / screenHeight).clamp(0.25, 0.6);
128128
_sheetController.animateTo(
129129
targetSize,
@@ -394,7 +394,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
394394
imagePath: wallpaperPath,
395395
);
396396

397-
final screenHeight = MediaQuery.of(context).size.height;
397+
final screenHeight = context.dimension.height;
398398
final baseSnapSize = (_firstCardHeight / screenHeight).clamp(0.25, 0.6);
399399
final handleHeight = 28.0 / screenHeight;
400400
final minSize = handleHeight.clamp(0.03, 0.05);
@@ -543,7 +543,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
543543
}
544544

545545
Widget _buildDraggableSheet(List<HomeDisplaySection> homeSections) {
546-
final screenHeight = MediaQuery.of(context).size.height;
546+
final screenHeight = context.dimension.height;
547547
final baseSnapSize = (_firstCardHeight / screenHeight).clamp(0.25, 0.6);
548548
final handleHeight = 28.0 / screenHeight;
549549
final minSize = handleHeight.clamp(0.03, 0.05);
@@ -601,7 +601,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
601601

602602
Widget _buildHeroSection() {
603603
final code = GlobalProviders.location.code;
604-
final screenHeight = MediaQuery.of(context).size.height;
604+
final screenHeight = context.dimension.height;
605605

606606
if (code == null) {
607607
return SizedBox(
@@ -700,7 +700,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
700700
return Column(
701701
children: [
702702
...allCards,
703-
SizedBox(height: MediaQuery.of(context).padding.bottom + 16),
703+
SizedBox(height: context.padding.bottom + 16),
704704
],
705705
);
706706
}
@@ -714,9 +714,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
714714
),
715715
(
716716
icon: SimpleIcons.threads,
717-
color: Theme.of(context).brightness == Brightness.dark
718-
? Colors.white
719-
: Colors.black,
717+
color: context.theme.brightness == .dark ? Colors.white : Colors.black,
720718
url: 'https://www.threads.net/@dpip.tw',
721719
),
722720
(
@@ -726,7 +724,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
726724
),
727725
(
728726
icon: Symbols.favorite_rounded,
729-
color: Theme.of(context).colorScheme.primary,
727+
color: context.colors.primary,
730728
url: SettingsDonatePage.route,
731729
),
732730
];
@@ -800,7 +798,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
800798
color: color.withValues(alpha: 0.15),
801799
borderRadius: BorderRadius.circular(8),
802800
border: Border.all(
803-
color: Theme.of(context).brightness == Brightness.dark
801+
color: context.theme.brightness == .dark
804802
? Colors.white.withValues(alpha: 0.25)
805803
: const Color.fromARGB(
806804
255,
@@ -817,7 +815,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
817815
Text(
818816
label,
819817
style: context.texts.labelSmall?.copyWith(
820-
color: Theme.of(context).brightness == Brightness.dark
818+
color: context.theme.brightness == Brightness.dark
821819
? Colors.white
822820
: const Color.fromARGB(255, 90, 90, 90),
823821
fontWeight: FontWeight.w700,
@@ -827,7 +825,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
827825
Text(
828826
value,
829827
style: context.texts.bodySmall?.copyWith(
830-
color: Theme.of(context).brightness == Brightness.dark
828+
color: context.theme.brightness == Brightness.dark
831829
? Colors.white
832830
: const Color.fromARGB(255, 60, 60, 60),
833831
fontWeight: FontWeight.w600,
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/// Reusable header widget for settings pages.
2+
library;
3+
4+
import 'package:dpip/utils/extensions/build_context.dart';
5+
import 'package:dpip/widgets/ui/icon_container.dart';
6+
import 'package:flutter/cupertino.dart';
7+
8+
/// A header widget for settings pages, displaying an icon alongside a title
9+
/// and subtitle.
10+
///
11+
/// Use at the top of a settings page to give it a consistent visual identity:
12+
///
13+
/// ```dart
14+
/// SettingsHeader(
15+
/// icon: Symbols.straighten_rounded,
16+
/// iconColor: Colors.amberAccent,
17+
/// title: Text('Unit'),
18+
/// subtitle: Text('Customize display units'),
19+
/// )
20+
/// ```
21+
///
22+
/// When [iconColor] is omitted, the icon uses the theme's
23+
/// [ColorScheme.onPrimaryContainer] color on a [ColorScheme.primaryContainer]
24+
/// background.
25+
class SettingsHeader extends StatelessWidget {
26+
/// The icon displayed inside the [ContainedIcon].
27+
final IconData icon;
28+
29+
/// The color of the icon. Defaults to the theme's [ColorScheme.onPrimaryContainer]
30+
/// with [ColorScheme.primaryContainer] as the background.
31+
final Color? iconColor;
32+
33+
/// The primary label for this settings section.
34+
final Widget title;
35+
36+
/// A short description shown below [title].
37+
final Widget subtitle;
38+
39+
/// Creates a [SettingsHeader].
40+
const SettingsHeader({
41+
super.key,
42+
required this.icon,
43+
this.iconColor,
44+
required this.title,
45+
required this.subtitle,
46+
});
47+
48+
@override
49+
Widget build(BuildContext context) {
50+
return Padding(
51+
padding: .symmetric(horizontal: 16, vertical: 8),
52+
child: Row(
53+
spacing: 12,
54+
children: [
55+
ContainedIcon(
56+
icon,
57+
color: iconColor ?? context.colors.onPrimaryContainer,
58+
backgroundColor: iconColor == null
59+
? context.colors.primaryContainer
60+
: null,
61+
size: 28,
62+
),
63+
Column(
64+
crossAxisAlignment: .start,
65+
children: [
66+
DefaultTextStyle(
67+
style: context.texts.titleLarge!.copyWith(fontWeight: .bold),
68+
child: title,
69+
),
70+
DefaultTextStyle(
71+
style: context.texts.bodyLarge!,
72+
child: subtitle,
73+
),
74+
],
75+
),
76+
],
77+
),
78+
);
79+
}
80+
}

lib/app/settings/donate/page.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class _SettingsDonatePageState extends State<SettingsDonatePage>
183183

184184
return ListView(
185185
padding: EdgeInsets.only(
186-
bottom: MediaQuery.of(context).padding.bottom + 16,
186+
bottom: context.padding.bottom + 16,
187187
),
188188
children: [
189189
_buildHeader(),
@@ -622,7 +622,7 @@ class _SettingsDonatePageState extends State<SettingsDonatePage>
622622
final storeName = Platform.isIOS
623623
? 'App Store'
624624
: 'Google Play';
625-
ScaffoldMessenger.of(context).showSnackBar(
625+
context.scaffoldMessenger.showSnackBar(
626626
SnackBar(
627627
content: Text(
628628
'無法連線至 {store},請稍後再試。'.i18n.args({
@@ -635,7 +635,7 @@ class _SettingsDonatePageState extends State<SettingsDonatePage>
635635
}
636636
InAppPurchase.instance.restorePurchases();
637637

638-
ScaffoldMessenger.of(context).showSnackBar(
638+
context.scaffoldMessenger.showSnackBar(
639639
SnackBar(content: Text('正在恢復您購買的訂閱'.i18n)),
640640
);
641641
},

lib/app/settings/experimental/page.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:dpip/core/preference.dart';
55
import 'package:dpip/utils/extensions/build_context.dart';
66
import 'package:dpip/widgets/list/segmented_list.dart';
77
import 'package:flutter/material.dart';
8+
import 'package:go_router/go_router.dart';
89
import 'package:material_symbols_icons/symbols.dart';
910

1011
class SettingsExperimentalPage extends StatefulWidget {
@@ -322,11 +323,11 @@ class _ExperimentalWarningDialogState
322323
),
323324
actions: [
324325
TextButton(
325-
onPressed: () => Navigator.of(context).pop(false),
326+
onPressed: () => context.pop(false),
326327
child: Text('取消'.i18n),
327328
),
328329
FilledButton(
329-
onPressed: canConfirm ? () => Navigator.of(context).pop(true) : null,
330+
onPressed: canConfirm ? () => context.pop(true) : null,
330331
child: Text(
331332
canConfirm ? '啟用'.i18n : '${_countdown}s',
332333
),

lib/app/settings/layout.dart

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1+
/// Shared scaffold layout for all settings pages.
2+
library;
3+
4+
import 'package:dpip/core/i18n.dart';
5+
import 'package:dpip/utils/constants.dart';
6+
import 'package:dpip/utils/extensions/build_context.dart';
17
import 'package:flutter/material.dart';
28
import 'package:go_router/go_router.dart';
39

10+
/// A scaffold wrapper for settings pages.
11+
///
12+
/// Provides a consistent app bar with a back button and applies the
13+
/// fade-forward page transition theme to its [child]:
14+
///
15+
/// ```dart
16+
/// SettingsLayout(
17+
/// child: MySettingsPage(),
18+
/// )
19+
/// ```
420
class SettingsLayout extends StatefulWidget {
21+
/// The settings page content to display inside the scaffold body.
522
final Widget child;
6-
final String title;
723

8-
const SettingsLayout({super.key, required this.child, required this.title});
24+
/// Creates a [SettingsLayout].
25+
const SettingsLayout({super.key, required this.child});
926

1027
@override
1128
State<SettingsLayout> createState() => _SettingsLayoutState();
@@ -16,13 +33,18 @@ class _SettingsLayoutState extends State<SettingsLayout> {
1633

1734
@override
1835
Widget build(BuildContext context) {
19-
return Scaffold(
20-
appBar: AppBar(
21-
title: Text(widget.title),
22-
leading: BackButton(onPressed: () => context.pop()),
23-
centerTitle: true,
36+
return Theme(
37+
data: context.theme.copyWith(
38+
pageTransitionsTheme: kFadeForwardPageTransitionsTheme,
39+
),
40+
child: Scaffold(
41+
appBar: AppBar(
42+
title: Text('設定'.i18n),
43+
leading: BackButton(onPressed: () => context.pop()),
44+
centerTitle: true,
45+
),
46+
body: widget.child,
2447
),
25-
body: widget.child,
2648
);
2749
}
2850
}

0 commit comments

Comments
 (0)