Skip to content

Commit f75eea7

Browse files
committed
Refactor code to improve variable declarations by removing redundant types and enhancing readability across multiple files
1 parent 80947a8 commit f75eea7

10 files changed

Lines changed: 55 additions & 47 deletions

File tree

analysis_options.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ include: package:flutter_lints/flutter.yaml
22

33
linter:
44
rules:
5-
- prefer_const_declarations
5+
- avoid_final_parameters
6+
- omit_local_variable_types
7+
- omit_obvious_local_variable_types
8+
- parameter_assignments
69
- prefer_const_constructors
10+
- prefer_const_declarations
711
- prefer_const_literals_to_create_immutables
8-
- unnecessary_const
12+
- prefer_final_fields
13+
- prefer_final_in_for_each
14+
- prefer_final_locals
915
- prefer_single_quotes
16+
- unnecessary_const
17+
- unnecessary_nullable_for_final_variable_declarations
1018

1119
formatter:
1220
page_width: 100

lib/common/app_settings.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ class AppSettings {
1616
_saveCounterTapMode();
1717
}
1818

19-
static const String _counterTapModeKey = 'counterTapMode';
19+
static const _counterTapModeKey = 'counterTapMode';
2020

2121
/// Saves the counter tap mode to persistent storage.
2222
Future<void> _saveCounterTapMode() async {
23-
SharedPreferences preferences = await SharedPreferences.getInstance();
23+
final preferences = await SharedPreferences.getInstance();
2424
await preferences.setBool(_counterTapModeKey, _counterTapMode);
2525
}
2626

2727
/// Loads app settings from persistent storage.
2828
Future<void> load() async {
29-
SharedPreferences preferences = await SharedPreferences.getInstance();
29+
final preferences = await SharedPreferences.getInstance();
3030
_counterTapMode = preferences.getBool(_counterTapModeKey) ?? false;
3131
}
3232
}

lib/common/strings.dart

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,51 @@ library;
99

1010
import '../screens/home_screen.dart';
1111

12-
const String appName = 'Hello World Counters';
12+
const appName = 'Hello World Counters';
1313

1414
// -----------------------------------------------------------------------------
1515
// App Drawer
1616
// -----------------------------------------------------------------------------
1717

1818
const String drawerTitle = appName;
19-
const String settingsItemTitle = 'Settings';
20-
const String aboutItemTitle = 'About this Hello World app';
21-
const String starAppItemTitle = 'Star on GitHub';
22-
const String rateAppItemTitle = 'Rate app';
19+
const settingsItemTitle = 'Settings';
20+
const aboutItemTitle = 'About this Hello World app';
21+
const starAppItemTitle = 'Star on GitHub';
22+
const rateAppItemTitle = 'Rate app';
2323

2424
const Map<MenuAction, String> menuActions = {
2525
MenuAction.reset: 'Reset counter',
2626
MenuAction.share: 'Share...',
2727
};
2828

29-
const String resetConfirm = 'Reset counter to zero?';
30-
const String resetConfirmReset = 'Reset';
31-
const String resetConfirmCancel = 'Cancel';
29+
const resetConfirm = 'Reset counter to zero?';
30+
const resetConfirmReset = 'Reset';
31+
const resetConfirmCancel = 'Cancel';
3232

3333
String shareText(String name, String value) => 'The $name is $value';
3434

3535
// -----------------------------------------------------------------------------
3636
// Home Screen - Main
3737
// -----------------------------------------------------------------------------
3838

39-
const String incrementTooltip = 'Increment';
40-
const String decrementTooltip = 'Decrement';
39+
const incrementTooltip = 'Increment';
40+
const decrementTooltip = 'Decrement';
4141

4242
// -----------------------------------------------------------------------------
4343
// Inspiration Screen
4444
// -----------------------------------------------------------------------------
4545

4646
String inspirationScreenTitle(String name) => '$name Inspiration';
47-
const String inspirationHeader = 'Running out of ideas? Try counting these:';
47+
const inspirationHeader = 'Running out of ideas? Try counting these:';
4848
String noInspirationTitle(String name) => 'No inspiration ideas yet for $name.';
49-
const String noInspirationSubtitle = 'Use this counter for anything you\'d like!';
50-
const String ideaCopied = 'Idea copied to clipboard!';
51-
const String ideaCopyTooltip = 'Tap to copy idea';
49+
const noInspirationSubtitle = 'Use this counter for anything you\'d like!';
50+
const ideaCopied = 'Idea copied to clipboard!';
51+
const ideaCopyTooltip = 'Tap to copy idea';
5252

5353
// -----------------------------------------------------------------------------
5454
// Settings Screen
5555
// -----------------------------------------------------------------------------
5656

57-
const String settingsTitle = 'Settings';
58-
const String counterTapModeTitle = 'Counter tap mode';
59-
const String counterTapModeSubtitle = 'Tap anywhere to increase counter';
57+
const settingsTitle = 'Settings';
58+
const counterTapModeTitle = 'Counter tap mode';
59+
const counterTapModeSubtitle = 'Tap anywhere to increase counter';

lib/common/urls.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
library;
99

1010
/// The app's home page URL.
11-
const String aboutUrl = 'https://www.thehelloworldwriter.com/helloworldcounters/';
11+
const aboutUrl = 'https://www.thehelloworldwriter.com/helloworldcounters/';
1212

1313
/// The URL to the source code repository of the app.
14-
const String starAppUrl = 'https://github.com/TheHelloWorldWriter/hello_world_counters';
14+
const starAppUrl = 'https://github.com/TheHelloWorldWriter/hello_world_counters';
1515

1616
/// The URL for rating the app (a redirect).
17-
const String rateAppUrl = 'https://www.thehelloworldwriter.com/helloworldcounters/rate/';
17+
const rateAppUrl = 'https://www.thehelloworldwriter.com/helloworldcounters/rate/';

lib/models/counter.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Counter {
4848

4949
/// Saves the counter value to persistent storage.
5050
Future<void> _saveValue() async {
51-
SharedPreferences preferences = await SharedPreferences.getInstance();
51+
final preferences = await SharedPreferences.getInstance();
5252
await preferences.setInt(_counterKey(type), value);
5353
}
5454

@@ -71,7 +71,7 @@ class Counter {
7171

7272
/// Returns the name of the specified counter type (e.g. "Black Counter").
7373
static String nameOf(CounterType type) {
74-
final String name = type.name;
74+
final name = type.name;
7575
return '${name[0].toUpperCase()}${name.substring(1).toLowerCase()} Counter';
7676
}
7777

@@ -95,16 +95,16 @@ class Counter {
9595
class Counters {
9696
/// Creates a Counters instance and creates the counter instances for all counter types.
9797
Counters() {
98-
for (var type in CounterType.values) {
98+
for (final type in CounterType.values) {
9999
_counters[type] = Counter(type);
100100
}
101101
}
102102

103103
/// The persistent storage key where to keep the current counter type.
104-
static const String currentCounterKey = 'current_counter';
104+
static const currentCounterKey = 'current_counter';
105105

106106
/// A map of counters for each counter type.
107-
final Map<CounterType, Counter> _counters = <CounterType, Counter>{};
107+
final _counters = <CounterType, Counter>{};
108108

109109
/// The current counter type.
110110
CounterType _currentType = CounterType.blue;
@@ -123,20 +123,20 @@ class Counters {
123123

124124
/// Saves the current counter type to persistent storage.
125125
Future<void> _saveCurrentType() async {
126-
SharedPreferences preferences = await SharedPreferences.getInstance();
126+
final preferences = await SharedPreferences.getInstance();
127127
await preferences.setInt(currentCounterKey, _currentType.index);
128128
}
129129

130130
/// Loads counter states from persistent storage.
131131
Future<void> load() async {
132-
SharedPreferences preferences = await SharedPreferences.getInstance();
132+
final preferences = await SharedPreferences.getInstance();
133133

134134
/// Load the current counter type, or set the default Blue counter
135-
final int counterIndex = preferences.getInt(currentCounterKey) ?? CounterType.blue.index;
135+
final counterIndex = preferences.getInt(currentCounterKey) ?? CounterType.blue.index;
136136
_currentType = CounterType.values[counterIndex];
137137

138138
/// Loads the values of all counters
139-
for (var counterType in _counters.keys) {
139+
for (final counterType in _counters.keys) {
140140
_counters[counterType]?.loadValue(preferences);
141141
}
142142
}

lib/screens/home_screen.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class HomeScreen extends StatefulWidget {
3434
/// The logic and internal state for the app home screen widget.
3535
class _HomeScreenState extends State<HomeScreen> {
3636
/// The map of counters for each counter type.
37-
final Counters _counters = Counters();
37+
final _counters = Counters();
3838

3939
/// The current app settings.
40-
final AppSettings _appSettings = AppSettings();
40+
final _appSettings = AppSettings();
4141

4242
@override
4343
void initState() {
@@ -73,8 +73,8 @@ class _HomeScreenState extends State<HomeScreen> {
7373
break;
7474
case MenuAction.share:
7575
// Share the current counter value using the platform's share sheet.
76-
final String name = _counters.current.name;
77-
final String value = toDecimalString(context, _counters.current.value);
76+
final name = _counters.current.name;
77+
final value = toDecimalString(context, _counters.current.value);
7878
SharePlus.instance.share(ShareParams(text: strings.shareText(name, value), subject: name));
7979
break;
8080
}
@@ -114,10 +114,10 @@ class _HomeScreenState extends State<HomeScreen> {
114114

115115
@override
116116
Widget build(BuildContext context) {
117-
final bool isPortrait = MediaQuery.of(context).size.height >= 500;
118-
final bool isLargeScreen = MediaQuery.of(context).size.longestSide >= 1024;
117+
final isPortrait = MediaQuery.of(context).size.height >= 500;
118+
final isLargeScreen = MediaQuery.of(context).size.longestSide >= 1024;
119119

120-
final CounterDisplay counterDisplay = CounterDisplay(
120+
final counterDisplay = CounterDisplay(
121121
value: _counters.current.value,
122122
color: _counters.current.color,
123123
isPortrait: isPortrait,

lib/screens/inspiration_screen.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class _InspirationScreenState extends State<InspirationScreen> {
3737

3838
@override
3939
Widget build(BuildContext context) {
40-
final Color counterColor = widget.counter.color;
41-
final Color textColor = counterColor.contrastOf();
42-
final String colorName =
40+
final counterColor = widget.counter.color;
41+
final textColor = counterColor.contrastOf();
42+
final colorName =
4343
widget.counter.type.name[0].toUpperCase() + widget.counter.type.name.substring(1);
4444

4545
return Scaffold(

lib/screens/settings_screen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SettingsScreen extends StatefulWidget {
2828
class _SettingsScreenState extends State<SettingsScreen> {
2929
@override
3030
Widget build(BuildContext context) {
31-
final bool isLargeScreen = MediaQuery.of(context).size.width >= 800.0;
31+
final isLargeScreen = MediaQuery.of(context).size.width >= 800.0;
3232

3333
return Scaffold(
3434
appBar: AppBar(

lib/utils/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:url_launcher/url_launcher.dart';
1111

1212
/// Formats [number] as a decimal, inserting locale-appropriate thousands separators as necessary.
1313
String toDecimalString(BuildContext context, int number) {
14-
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
14+
final localizations = MaterialLocalizations.of(context);
1515
return localizations.formatDecimal(number);
1616
}
1717

lib/widgets/counter_display.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CounterDisplay extends StatelessWidget {
2929

3030
@override
3131
Widget build(BuildContext context) {
32-
final TextStyle? counterStyle = isPortrait
32+
final counterStyle = isPortrait
3333
? Theme.of(context).textTheme.displayLarge
3434
: Theme.of(context).textTheme.displayMedium;
3535

0 commit comments

Comments
 (0)