flutter ElevatedButton problems - flutter

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();

Related

Easy Localization Flutter package

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'));

The getter 'Share' isn't defined for the class '_SettingsState'

I have the "share_plus" package, which works fine on emulator. I imported the package as usual, I also tried the functionality to share the actual app without any problem. But when i try to run with the Xcode in real device it gives me this error:
import 'package:share_plus/share_plus.dart';
^
lib/Settings.dart:138:19: Error: The getter 'Share' isn't defined for the class '_SettingsState'.
- '_SettingsState' is from 'package:flutter_projects/Settings.dart' ('lib/Settings.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'Share'.
Share.share('https://apps.apple.com/.....');
What could be the problem?
This is my code:
SettingsSection(
title: 'Extra',
tiles: [
SettingsTile(
title: ('Condividi').tr(), leading: Icon(Icons.share),
onPressed: (context) async {
//share_plus requires iPad users to provide the sharePositionOrigin parameter.
final box = context.findRenderObject() as RenderBox?;
await Share.share('https://apps.apple....,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
);
}),
],
),

The method 'FlatButton' isn't defined for the class 'PlatformButton' | showing when using enough_giphy_flutter package

when using emoji_picker and enough_giphy_flutter packages, It shows error like this,
The method 'FlatButton' isn't defined for the class 'PlatformButton'
Try correcting the name to the name of an existing method,
or defining a method named 'FlatButton'. child: FlatButton( ^^^^^^^^^^
I think It is because - the latest version of Flutter, Flatbutton is deprecated and uses TextButton.(https://docs.flutter.dev/release/breaking-changes/buttons#context)
But, How do I use these packages in the latest version of Flutter?
my code here : used emoji_picker package
Widget showEmojiPicker() {
return EmojiPicker(
rows: 4,
columns: 7,
onEmojiSelected: (emoji, category) {
print(emoji);
},
);
}

Flutter : How to close alertdialog from another file

I have 2 dart file. I want close the alertdialog from another dart file in specific conditions.
How can I ?
for example the function i want.(I wrote to express myself better.)
void example(){
if(control==false){
alertDialog.close();
}
Alert dialog file
class AlertForm extends StatefulWidget {
#override
_AlertFormState createState() => _AlertFormState();
}
class _AlertFormState extends State<AlertForm> {
void _showDialog() {
// flutter defined function
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Alert Dialog title"),
content: new Text("Alert Dialog body"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
So what I want is to use the pop action in a function in another flutter file.
Call Navigator.of(context).pop(); inside the if block (of the void example function of the other Dart file) and the alert should close.
But here is the problem, the context above might not be part of or aware of the dialog and the above may not work.
More info about your setup would suggest a solution to this problem. Like how exactly does the other Dart file exist relative to the file where _showDialog is declared? If it's something you can easily provide context from AlertForm Dart file to the closingDialog Dart file, then you should know what to do. If that's not the case then a navigator key should help.
You can keep a navigator key for the entire application. Then instead of closing the dialog with Navigator.of(context).pop();, you use navigatorKey.currentState!.pop();.
You should have properly setup the navigator key yourself. Refer to this Stackoverflow answer for how to do that: https://stackoverflow.com/a/72945522/13644299
It is not compulsory to use get_it package. You can simply export the NavigationService (that will keep the navigatorKey) from any Dart file or with any method you deem fit, depending on your current state management architecture.

The argument Future can not be assigned to the parameter type void Function()

I have a statefull widged where a button has
onPressed: _exportData,
where _exportData defined this way
Future<void> _exportData() async {
...
setState(() {
_message = 'A message'; // I want to use localization string here
});
}
I need to pass localization string into the above setState() and I do it this way
Lang.key(ctx, 'about')
i.e. I need to pass the context there, but when I try to make it like this
onPressed: _exportData(ctx), // the error here
and
Future<void> _exportData(BuildContext ctx) async {...
there is an error on onPressed
The argument Future can not be assigned to the parameter type
'void Function()'
How cat I pass context inside _exportData or how can I use localization string from inside the _exportData?
update: actualy there was no need to pass context into _exportData, because context is awailable inside state class
onPressed: () async { await _exportData(context) }
Make this change and let us know.
EDIT:
onPressed: _exportData(ctx), // the error here
You cannot do this because you can only pass a reference to a function in onPressed. If you want to pass arguments to a function, you'll have to do it the way I have shown above (although, it needs not to be asynchronous necessarily). Function name without the opening and a closing parenthesis is the reference while the one with the parenthesis is the actual function call.
The reason for this is that onPressed takes a reference/address to the function/pointer and not the actual function call. So if we had to pass the argument (context in your case) by your way, we will need to put the parenthesis around the argument making it a function call and not a reference hence executing it as soon as the build method runs and not when onPressed triggers it.
The way I showed you assigns an anonymous function to onPressed. See, there is no parenthesis in the end, so not executing it straight away and acting as a reference to a function. I put an async and await there because your _exportData function returns a future.
Hope that clears your doubt!