Skip to content

Commit

Permalink
migrate to code analyze flutter_lints
Browse files Browse the repository at this point in the history
  • Loading branch information
aissat committed May 13, 2022
1 parent bd3fcbc commit 42b4c11
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 238 deletions.
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#https://dart.dev/guides/language/analysis-options
#include: package:pedantic/analysis_options.yaml
include: package:flutter_lints/flutter.yaml
6 changes: 3 additions & 3 deletions bin/generate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ String _resolve(Map<String, dynamic> translations, bool? skipUnnecessaryKeys,
if (!_preservedKeywords.contains(key)) {
accKey != null && !ignoreKey
? fileContent +=
' static const ${accKey.replaceAll('.', '_')}\_$key = \'$accKey.$key\';\n'
' static const ${accKey.replaceAll('.', '_')}_$key = \'$accKey.$key\';\n'
: !ignoreKey
? fileContent += ' static const $key = \'$key\';\n'
: null;
Expand Down Expand Up @@ -260,12 +260,12 @@ class CodegenLoader extends AssetLoader{

Map<String, dynamic>? data = json.decode(await fileData.readAsString());

final mapString = JsonEncoder.withIndent(' ').convert(data);
final mapString = const JsonEncoder.withIndent(' ').convert(data);
gFile += 'static const Map<String,dynamic> $localeName = $mapString;\n';
}

gFile +=
'static const Map<String, Map<String,dynamic>> mapLocales = \{${listLocales.join(', ')}\};';
'static const Map<String, Map<String,dynamic>> mapLocales = {${listLocales.join(', ')}};';
classBuilder.writeln(gFile);
}

Expand Down
10 changes: 6 additions & 4 deletions lib/src/easy_localization_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ class EasyLocalization extends StatefulWidget {
}

@override
// ignore: library_private_types_in_public_api
_EasyLocalizationState createState() => _EasyLocalizationState();

// ignore: library_private_types_in_public_api
static _EasyLocalizationProvider? of(BuildContext context) =>
_EasyLocalizationProvider.of(context);

Expand Down Expand Up @@ -207,11 +209,11 @@ class _EasyLocalizationProvider extends InheritedWidget {
// Locale get startLocale => parent.startLocale;

/// Change app locale
Future<void> setLocale(Locale _locale) async {
Future<void> setLocale(Locale locale) async {
// Check old locale
if (_locale != _localeState.locale) {
assert(parent.supportedLocales.contains(_locale));
await _localeState.setLocale(_locale);
if (locale != _localeState.locale) {
assert(parent.supportedLocales.contains(locale));
await _localeState.setLocale(locale);
}
}

Expand Down
24 changes: 13 additions & 11 deletions lib/src/easy_localization_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class EasyLocalizationController extends ChangeNotifier {
if (useFallbackTranslations && _fallbackLocale != null) {
Map<String, dynamic>? baseLangData;
if (_locale.countryCode != null && _locale.countryCode!.isNotEmpty) {
baseLangData = await loadBaseLangTranslationData(Locale(locale.languageCode));
baseLangData =
await loadBaseLangTranslationData(Locale(locale.languageCode));
}
data = await loadTranslationData(_fallbackLocale!);
if (baseLangData != null) {
Expand All @@ -103,7 +104,8 @@ class EasyLocalizationController extends ChangeNotifier {
}
}

Future<Map<String, dynamic>?> loadBaseLangTranslationData(Locale locale) async {
Future<Map<String, dynamic>?> loadBaseLangTranslationData(
Locale locale) async {
try {
return await loadTranslationData(Locale(locale.languageCode));
} on FlutterError catch (e) {
Expand Down Expand Up @@ -133,24 +135,24 @@ class EasyLocalizationController extends ChangeNotifier {

Future<void> _saveLocale(Locale? locale) async {
if (!saveLocale) return;
final _preferences = await SharedPreferences.getInstance();
await _preferences.setString('locale', locale.toString());
final preferences = await SharedPreferences.getInstance();
await preferences.setString('locale', locale.toString());
EasyLocalization.logger('Locale $locale saved');
}

static Future<void> initEasyLocation() async {
final _preferences = await SharedPreferences.getInstance();
final _strLocale = _preferences.getString('locale');
_savedLocale = _strLocale != null ? _strLocale.toLocale() : null;
final _foundPlatformLocale = await findSystemLocale();
_deviceLocale = _foundPlatformLocale.toLocale();
final preferences = await SharedPreferences.getInstance();
final strLocale = preferences.getString('locale');
_savedLocale = strLocale?.toLocale();
final foundPlatformLocale = await findSystemLocale();
_deviceLocale = foundPlatformLocale.toLocale();
EasyLocalization.logger.debug('Localization initialized');
}

Future<void> deleteSaveLocale() async {
_savedLocale = null;
final _preferences = await SharedPreferences.getInstance();
await _preferences.remove('locale');
final preferences = await SharedPreferences.getInstance();
await preferences.remove('locale');
EasyLocalization.logger('Saved locale deleted');
}

Expand Down
10 changes: 6 additions & 4 deletions lib/src/localization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ class Localization {

String _replaceArgs(String res, List<String>? args) {
if (args == null || args.isEmpty) return res;
args.forEach((String str) => res = res.replaceFirst(_replaceArgRegex, str));
for (var str in args) {
res = res.replaceFirst(_replaceArgRegex, str);
}
return res;
}

Expand All @@ -119,8 +121,8 @@ class Localization {
String? name,
NumberFormat? format,
}) {
late var pluralCase;
late var res;
late PluralCase pluralCase;
late String res;
var pluralRule = _pluralRule(_locale.languageCode, value);
switch (value) {
case 0:
Expand Down Expand Up @@ -169,7 +171,7 @@ class Localization {
}

String _gender(String key, {required String gender}) {
return _resolve(key + '.$gender');
return _resolve('$key.$gender');
}

String _resolvePlural(String key, String subKey) {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/public.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ String tr(
Map<String, String>? namedArgs,
String? gender,
}) {
return Localization.instance.tr(key, args: args, namedArgs: namedArgs, gender: gender);
return Localization.instance
.tr(key, args: args, namedArgs: namedArgs, gender: gender);
}

/// {@template plural}
Expand Down
15 changes: 8 additions & 7 deletions lib/src/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@ import 'package:flutter/material.dart';

class FutureErrorWidget extends StatelessWidget {
final String msg;
const FutureErrorWidget({this.msg = 'Loading ...'});
const FutureErrorWidget({Key? key, this.msg = 'Loading ...'})
: super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Icon(
const Icon(
Icons.error_outline,
color: Colors.red,
size: 64,
textDirection: TextDirection.ltr,
),
SizedBox(height: 20),
Text(
const SizedBox(height: 20),
const Text(
'Easy Localization:',
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
style: TextStyle(
fontWeight: FontWeight.w700, color: Colors.red, fontSize: 25.0),
),
SizedBox(height: 10),
const SizedBox(height: 10),
Text(
'"$msg"',
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
style: TextStyle(
style: const TextStyle(
fontWeight: FontWeight.w500, color: Colors.red, fontSize: 14.0),
),
SizedBox(height: 30),
const SizedBox(height: 30),
// Center(
// child: CircularProgressIndicator()
// ),
Expand Down
Loading

0 comments on commit 42b4c11

Please sign in to comment.