I use the easy localization package, how can I check and change the language with just one button?
https://pub.dev/packages/easy_localization
TextButton(
onPressed: () {
// 'En'
// 'Ar'
},
child: Text('Switch lang'),
)
You need to have JSON file for changing the language.
if so, simply you need to take the language by calling
EasyLocalization.of(context)?.locale;
or after importing EasyLocalization package just write context.locale;
For changing use EasyLocalization.of(context)?.setLocale(Locale('EN')); or context.setLocale(Locale('EN'));
Related
I wanted to create a checklist for the programming languages I know. I have used Set() to contain the data. But when I use it in the CheckboxListTile() it is not checking the box in the UI. I wanted to do it with using flutter_hooks.
I have created an enum for ProgrammingLanguages.
enum ProgrammingLanguages { C, DART, PYTHON, JAVASCRIPT }
Then I have initialised the set in stateless Widget class like this
final _selectedLanguages = useState<Set>(Set<ProgrammingLanguages>());
In the build I have added a CheckBoxListTile like this.
CheckboxListTile(
title: Text('C'),
value: _selectedLanguages.value.contains(ProgrammingLanguages.C),
onChanged: (value) {
value
? _selectedLanguages.value.remove(ProgrammingLanguages.C)
: _selectedLanguages.value.add(ProgrammingLanguages.C);
}),
But in the final UI I am not able to activate the check box. Please help.
Here is my code
ElevatedButton(
child : Text ("Continue"),
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
KeyboardUtil.hideKeyboard(context);
Navigator.pushNamed(context, LoginSuccessScreen.routeName);
}
},
),
ElevatedButton isn't a function. (Documentation) Try correcting the name to match an existing function, or define a method or function named 'ElevatedButton'.
The name ElevatedButton is defined in the libraries package:fitnessapp/components/elevated_button.dart and package:flutter/src/material/elevated_button.dart (via package:flutter/material.dart). (Documentation) Try using 'as prefix' for one of the import directives, or hiding the name from all but one of the imports.
"How i can fix this problem" ?
In the dart file you use, there are 2 imported files that have the same name, so it does not know which to use. Either remove one of them or name the libraries:
import 'package:fitnessapp/components/elevated_button.dart' as MyButton;
Then whenever you want to use something from this file, you can call it like:
MyButton.ElevatedButton();
I'm trying to localize my Flutter app by following the documentation.
What I'm trying to achieve is that while building widgets dynamically, I wanted to translate the data which comes from my model. This is what I've tried so far
List.generate(services.length, (index) {
final Service service = services[index];
return Material(
borderRadius: BorderRadius.circular(10.0),
child: Text(
AppLocalizations.of(context).{{service.title}} // Here I wanted to translate the service title
),
);
}
How can I achieve this in Flutter? or any other possible method to translate dynamic contents.
Dynamic translation is not supported by Flutter localization packages. You can try to integrate google translation API in you app. But I strongly convinced that it should be a server-side feature and Flutter client should obtain already translated messages in your models. To achieve this, for example, you can use http-headers with your device locale to tell client's language to server.
Also you need to use arguments for Intl messages according to this guide. Here is an example of message with String argument in AppLocalizations:
String someLocalizedString(String argument) => Intl.message(
'Localized part - $argument',
name: 'someLocalizedString',
locale: localeName,
args: [argument],
);
This string inside .arb file:
"someLocalizedString": "Localized part - {argument}",
"#someLocalizedString": {
"type": "text",
"placeholders": {
"argument": {}
}
}
How I will change the back button icon in flutter using the theme. So it can be reflected throughout the application. I saw multiple options in the theme to change the size and color of the icon. But didn't see the change icon option.
Example:
Scaffold(
appBar(
leading: BackButton(), // icon depends on TargetPlattrom
),
),
Or you can create your own constant widget basis on IconButton with your own icon settings, than use it as value for leading property. In addition, there is an AppBarTheme class, which can be used when you create instance of ThemeData.
https://api.flutter.dev/flutter/material/AppBarTheme-class.html
P.S. If you look at AppBar source code you see that regular widget CloseButton or BackButton are used. So there is no specific style for it. And you have 2 ways:
Create your own global widget and use it in each Scaffold.
Subclass Scaffold with your own leading property and use it.
appBar: AppBar(
automaticallyImplyLeading: false,
leading: Builder(
builder: (BuildContext context) {
return IconButton(
icon: const Icon(Icons.alarm_on), // Put icon of your preference.
onPressed: () {
// your method (function),
},
);
},
),
title: Text(widget.title),
centerTitle: true,
The above code will change the default back icon to alarm on icon. You can change it to any thing.
I'm not sure if this is the right way to do this. I'm still learning and I don't know if this will work in all cases. I've done this using VS Code while my app is in development.
I looked up the backbutton references(Shift + Alt + F12) and found the actual button in flutter sdk. I changed it there in the back_button.dart file which can be found in your flutter installation folder : c:\flutter\packages\flutter\lib\src\material\back_button.dart
There, edit the static IconData method of the button and choose any Icon you wanna use for any platform.
static IconData _getIconData(TargetPlatform platform) {
switch (platform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
case TargetPlatform.iOS:
case TargetPlatform.macOS:
return Icons.arrow_back_ios;
}
}
In the above case Icons.arrow_back_ios will be returned in all platforms. You can specify the return Icon for each platform.
However, I haven't tested this in like a real world "production use" situation so I hope someone else here can clarify.
This will do the job for you:
IconButton(
onPressed: (){
Navigator.pop(context);
},
icon:Icon(Icons.arrow_back_ios),
//replace with our own icon data.
)
For the future seekers like me.
i used this article
https://www.thirdrocktechkno.com/blog/how-to-implement-localization-in-flutter
to make my app multilang.
and i want to fit the banner in my app (no google ads) to the correct lang and correct on_tap
for example now i have just one:
GestureDetector(
child: Column(children: [
Image(
image: AssetImage('assets/me.jpg'), // Here i want to use another image for each lang...
),
]),
onTap: () {
openUrl();
},
),
I use flutter_localizations.
thank you.
I don't have much experience with localization but you can try localizing the pathname.
For example
English pathname: assets/me_en.jpg
Spanish pathname: assets/me_es.jp
... and so on.