Select Language Class
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:user_ui/setting_screen.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SelectLanguage extends StatefulWidget {
const SelectLanguage({Key? key}) : super(key: key);
@override
State<SelectLanguage> createState() => _SelectLanguageState();
}
class _SelectLanguageState extends State<SelectLanguage> {
String? selectedLanguage;
Color textColor = Colors.black54;
@override
void initState() {
super.initState();
_loadSelectedLanguage();
}
// Load selected language from SharedPreferences
void _loadSelectedLanguage() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
selectedLanguage = prefs.getString('selectedLanguage') ?? 'en_US';
Get.updateLocale(Locale(selectedLanguage!.split('_')[0], selectedLanguage!.split('_')[1]));
});
}
// Save selected language to SharedPreferences
void _saveSelectedLanguage(String language) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('selectedLanguage', language);
Get.updateLocale(Locale(language.split('_')[0], language.split('_')[1]));
}
@override
Widget build(BuildContext context) {
final height = MediaQuery.of(context).size.height;
final width = MediaQuery.of(context).size.width;
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('selectLanguage'.tr, style: boldText),
backgroundColor: Colors.white,
leading: IconButton(
onPressed: () {
Navigator.of(context).pop(true);
},
icon: Icon(Icons.arrow_back_ios),
),
titleSpacing: 0.0,
),
body: Column(
children: [
SizedBox(
height: height * 0.055,
width: width,
child: ListTile(
title: Text(
'english'.tr,
style: semiBoldText.copyWith(
color: selectedLanguage == 'en_US' ? blue : textColor,
fontSize: height * 0.025,
),
),
leading: Transform.scale(
scale: 1.4,
child: Radio<String>(
activeColor: blue,
value: 'en_US',
groupValue: selectedLanguage,
onChanged: (value) {
setState(() {
selectedLanguage = value;
_saveSelectedLanguage(value!);
});
Get.to(() =>
SettingScreen());
},
),
),
),
),
SizedBox(
height: height * 0.055,
width: width,
child: ListTile(
title: Text(
'hindi'.tr,
style: semiBoldText.copyWith(
color: selectedLanguage == 'hi_IN' ? blue : textColor,
fontSize: height * 0.025,
),
),
leading: Transform.scale(
scale: 1.4,
child: Radio<String>(
activeColor: blue,
value: 'hi_IN',
groupValue: selectedLanguage,
onChanged: (value) {
setState(() {
selectedLanguage = value;
_saveSelectedLanguage(value!);
});
Get.to(() =>
SettingScreen());
},
),
),
),
),
SizedBox(
height: height * 0.055,
width: width,
child: ListTile(
title: Text(
'marathi'.tr,
style: semiBoldText.copyWith(
color: selectedLanguage == 'mr_IN' ? blue : textColor,
fontSize: height * 0.025,
),
),
leading: Transform.scale(
scale: 1.4,
child: Radio<String>(
activeColor: blue,
value: 'mr_IN',
groupValue: selectedLanguage,
onChanged: (value) {
setState(() {
selectedLanguage = value;
_saveSelectedLanguage(value!);
});
Get.to(() =>
SettingScreen());
},
),
),
),
),
],
),
);
}
}
// Constants for text styles
const regulatText = TextStyle(fontFamily: 'Pangram', fontWeight: FontWeight.normal);
const semiBoldText = TextStyle(fontFamily: 'Pangram', fontWeight: FontWeight.w500);
const boldText = TextStyle(fontFamily: 'Pangram', fontWeight: FontWeight.bold);
// Color constants
Color blue = Color(0xFF027FFF);
Color red = Color(0xFFFF2121);
Color white = Colors.white;
in main.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:suer_ui/utils/translations.dart';
import 'package:share_trick_ui/welcome_page.dart';
import 'package:shared_preferences/shared_preferences.dart';
// void main(){
void main() async{
WidgetsFlutterBinding.ensureInitialized();
// Load selected language from SharedPreferences
SharedPreferences prefs = await SharedPreferences.getInstance();
String selectedLanguage = prefs.getString('selectedLanguage') ?? 'en_US';
// Set the locale based on the selected language
Get.updateLocale(Locale(
selectedLanguage.split('_')[0],
selectedLanguage.split('_')[1],
));
runApp(const MainScreen());
}
class MainScreen extends StatelessWidget {
const MainScreen({super.key});
@override
Widget build(BuildContext context) {
// return MaterialApp(
return GetMaterialApp(
translations: AppTranslations(),
locale: Get.deviceLocale, // Default locale based on device settings
fallbackLocale: Locale('en', 'US'),
debugShowCheckedModeBanner: false,
home: SplashScreen(),
);
}
}
App Translation
import 'package:get/get.dart';
class AppTranslations extends Translations {
@override
Map<String, Map<String, String>> get keys => {
'en_US': {
'selectLanguage': 'Select Language',
'english': 'English',
'hindi': 'Hindi',
'marathi': 'Marathi',
'setting': 'Settings',
},
'hi_IN': {
'selectLanguage': 'भाषा चुनें',
'english': 'अंग्रेज़ी',
'hindi': 'हिन्दी',
'marathi': 'मराठी',
'setting': 'सेटिंग्स',
},
'mr_IN': {
'selectLanguage': 'भाषा निवडा',
'english': 'अंग्रेजी',
'hindi': 'हिंदी',
'marathi': 'मराठी',
'setting': 'सेटिंग्ज',
},
};
}