how to open Whatsapp application in mobile application using flutter - flutter

ElevatedButton(
onPressed:() {
launchWhatsapp(phoneNum,message);
},
child: Text("Message the Buyer",
style: TextStyle(
fontWeight: FontWeight.w400
),
),
),
So this is my button code
launchWhatsapp(String number,String text) async{
String url = "https://wa.me/${number}";
if(await canLaunchUrlString(url)){
await launchUrlString(url);
}
and this is my function method. I used url_laucher to make it work but it will redirect to the browser and show this
Image Error but when I try to copy the same link to the browser it works but when trying to open from my app it will show the error as shown in the picture.

Try below code
Your function:
whatsApp() {
return launchUrl(
Uri.parse(
//'https://wa.me/1234567890' //you use this url also
'whatsapp://send?phone=1234567890',//put your number here
),
);
}
Your Widget:
ElevatedButton(
onPressed: () {
whatsApp();
},
child: Text(
"Message the Buyer",
style: TextStyle(fontWeight: FontWeight.w400),
),
),
set internet permission to your AndroidManifest.xml file path = project_name/android/app/src/main/AndroidManifest.xml add below line above of application tag
<uses-permission android:name="android.permission.INTERNET"/>

Related

How to direct download file from google drive to local in flutter web?

How can I direct download the file in flutter web? I used the anchor method to download the local file by giving the asset path, it works fine in debug mode but when I hoisted the flutter web project in GitHub pages it was downloading a .html file.
const String resume = "assets/docs/resume.pdf";
download(url) {
html.AnchorElement anchorElement = html.AnchorElement(href: url);
anchorElement.download = "resume";
anchorElement.click();
}
TextButton(
onPressed: () => download(resume),
child: FittedBox(
child: Row(
children: [
Text(
"Download CV",
style: TextStyle(
color: Theme.of(context).textTheme.bodyText1!.color,
),
),
const SizedBox(
width: defaultPadding / 2,
),
SvgPicture.asset("assets/icons/download.svg")
],
),
),
),
So I tried to download the file which is in google drive by giving the file link instead of the local asset path, in this case, it just redirects and previews the file in a new tab.
But I wanted to download the file directly with just one click.
I have been searching for 2 days since I didn't get any direct proper answer to download files using flutter web However I found some way to make it possible.
step1: Get a shareable link of your google file and make it public it may look like this below drive.google.com/file/d/1Q7MB6smDEFd-PzpqK-3cC2_fAZc4yaXF/view?usp=sharing
step2: copy the unique id of your sharable link, which I highlighted in above
step3: Replace FILEID with your unique id
https://drive.google.com/uc?export=download&id=FILEID ---> https://drive.google.com/uc?export=download&id=1Q7MB6smDEFd-PzpqK-3cC2_fAZc4yaXF that's it direct downloadable link is ready just replace constant with above link
const String resume = "https://drive.google.com/uc?export=download&id=1Q7MB6smDEFd-PzpqK-3cC2_fAZc4yaXF";
download(url) {
html.AnchorElement anchorElement = html.AnchorElement(href: url);
anchorElement.download = "resume";
anchorElement.click();
}
TextButton(
onPressed: () => download(resume),
child: FittedBox(
child: Row(
children: [
Text(
"Download CV",
style: TextStyle(
color: Theme.of(context).textTheme.bodyText1!.color,
),
),
const SizedBox(
width: defaultPadding / 2,
),
SvgPicture.asset("assets/icons/download.svg")
],
),
),
),
I referred to these pages 1) https://www.howtogeek.com/747810/how-to-make-a-direct-download-link-for-google-drive-files/ 2) https://medium.com/vijay-r/flutter-web-local-file-downloader-5cf087929bc

Flutter Local Notifications Plugin: createNotificationChannel function not working

I am trying to create an android notification channel with flutter_local_notifications: ^5.0.0+4
Put it looks like it does not work as should be.
I wrote a code using getNotificationChannels to verify if the channel was created successfully
Please take a look to my code
Future<void> _getNotificationChannels() async {
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications', // description
importance: Importance.max,
));
final Widget notificationChannelsDialogContent =
await _getNotificationChannelsDialogContent();
await showDialog<void>(
context: context,
builder: (BuildContext context) => AlertDialog(
content: notificationChannelsDialogContent,
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
),
);
}
Future<Widget> _getNotificationChannelsDialogContent() async {
try {
final List<AndroidNotificationChannel>? channels =
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()!
.getNotificationChannels();
return Container(
width: double.maxFinite,
child: ListView(
children: <Widget>[
const Text(
'Notifications Channels',
style: TextStyle(fontWeight: FontWeight.bold),
),
const Divider(color: Colors.black),
if (channels?.isEmpty ?? true)
const Text('No notification channels')
else
for (AndroidNotificationChannel channel in channels!)
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('id: ${channel.id}\n'
'name: ${channel.name}\n'
'description: ${channel.description}\n'
'groupId: ${channel.groupId}\n'
'importance: ${channel.importance.value}\n'
'playSound: ${channel.playSound}\n'
'sound: ${channel.sound?.sound}\n'
'enableVibration: ${channel.enableVibration}\n'
'vibrationPattern: ${channel.vibrationPattern}\n'
'showBadge: ${channel.showBadge}\n'
'enableLights: ${channel.enableLights}\n'
'ledColor: ${channel.ledColor}\n'),
const Divider(color: Colors.black),
],
),
],
),
);
} on PlatformException catch (error) {
return Text(
'Error calling "getNotificationChannels"\n'
'code: ${error.code}\n'
'message: ${error.message}',
);
}
}
As you can see I am using createNotificationChannel to create a notifications channel then I am using getNotificationChannels to see if the channel was created successfully.
I am getting always "No notification channels" as a result in the dialog.
I do not know what is wrong with my code,
Thanks for your help
The problem was the Android version that I am using for testing,
Notification channels is a concept that is specific Android 8 or newer, which is why the API docs state that the method to create channels is only for those versions of Android

flutter: how to get the button widget in this flutter test?

I'm working with flutter test. I want to test something after tap a widget but I don't know how to find the button. Because I custom it rather than use Icon or iconButton. Also, I can't find it according to its text because another widget has the same name. And also two InkWell widget as you can see. My basic test code is here.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:grpc/grpc.dart';
import 'package:project/page/login.dart';
void main() {
testWidgets('login page test', (WidgetTester tester) async {
// Build our app and trigger a frame
await tester.pumpWidget(Login());
expect(find.text('login'), findsNWidgets(2));
expect(find.text('ID'), findsOneWidget);
expect(find.text('password'), findsOneWidget);
expect(find.text('end'), findsOneWidget);
// how to find my custom button
await tester.tap((find.byElementType(InkWell))); <- my question here
// test other things after tapping the button.
});
}
InkWell(
key: const Key("btnLogin"),
child: ElevatedButton(
onPressed: () {
_decrementCounter();
},
child: const Text(
" Login ",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
),
),
Like this also we can do
await tester.tap(find.byKey(const Key('btnLogin')));
await tester.pump();
Instead of find.byElementType() use find.byType().
So your code will be like this:
await tester.tap((find.byType(InkWell)));
Like this you have to mention key (key: const Key("btnLogin"),)
ElevatedButton(
key: const Key("btnLogin"),
onPressed: () {
_decrementCounter();
},
child: const Text(
" Login ",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
),
in Widget test
await tester.tap(find.byKey(const Key('btnLogin')));
await tester.pump();
give a key name to your inkwell button like this.
- in your login screen.
InkWell(
key: const Key("login"),
child: ElevatedButton(
onPressed: () {
},
child: const Text(
" Login ",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
),
),
**- in your testing file**
var loginButton = find.byKey(const Key('login'));
await tester.tap(login);

I'd like to add a like button to this flutter blog app

This project can be found here:https://github.com/Santos-Enoque/flutter_blog_app
So far I have this connected to firebase realtime database and it works well. I'm trying to add a like button to the Home page(lib/screens/home.dart) where all the posts are listed.
The Homepage displays the blog results using a Card with a ListTile. The trailing property of the ListTile card is already used so I'd like to use the leading property of the ListTile card to display a favourite icon which would increment a counter++ when tapped and also save the results to Firebase. Just like Facebook's like button.
Here's the code below:
child: Card(
child: ListTile(
title: ListTile(
onTap: (){
_incrementCounter();
},
leading: FittedBox(
fit: BoxFit.fitWidth,
child: Row(
children: <Widget>[
Icon(Icons.favorite),
Text(postsList[index].counter.toString()
),
],
),
),
title: Text(
postsList[index].title,
style: TextStyle(
fontSize: 16.0, fontWeight: FontWeight.bold),
),
trailing: Text(
timeago.format(DateTime.fromMillisecondsSinceEpoch(postsList[index].date)),
style: TextStyle(fontSize: 12.0, color: Colors.grey),
),
),
subtitle: Padding(
padding: const EdgeInsets.only(bottom: 14.0),
child: Text(postsList[index].body, style: TextStyle(fontSize: 14.0),),
),
),
),
Here's the _increment counter code:
try {
var ref = FirebaseDatabase.instance.reference().child('posts/{postId}/counter');
await ref.once().then((data) async => {
await ref.set(data.value + 1),
});
} catch (e) {
print(e.message);
}
}
![Home page of blog]https://photos.google.com/share/AF1QipMK6C-Wx2vZHHbE2jDMQsfYNnwl9OWK_5W8OKOfiIChcXt-gnWnCH7ba_EpyRnRAA?key=cGxkRkVSSk9PQTdtTXB0MzZBRDNHNUVzSGxlcDVB
The blog posts are displayed as cards as in the image ... I'm trying to add an icon on the left side of the card(leading) plus an incrementing value everytime someone taps the icon. Something like the like button on facebook. And also save the data to firebase realtime database.
Any help is much appreciated ... Thank you all!
I think what you may want to do is add this function to your onPressed. You will also need to set the text of the next to the icon equal to the new value read.
void like() async {
try {
var ref = FirebaseDatabase.instance.reference().child('path to likes for a post');
await ref.once().then((data) async => {
await ref.set(data.value + 1);
});
} catch (e) {
print(e.message);
}
}
Hope this helps.
P.S.- You may find this video of use: https://www.youtube.com/watch?v=l8_7RTRRmHo
Thanks to everyone who helped with this:
Code for UI:
leading: FittedBox(
fit: BoxFit.fitWidth,
child: Row(
children: <Widget>[
Icon(Icons.favorite),
Text(postsList[index].counter.toString()
),
],
),
),
Code for Function:
onTap: (){
_incrementCounter(postsList[index].key);
}
...
void _incrementCounter(key) async {
try {
var ref = FirebaseDatabase.instance.reference().child('posts/'+ key +'/counter');
await ref.once().then((data) async => {
await ref.set(data.value + 1),
});
} catch (e) {
print(e.message);
}
}
}

Flutter add clickable link/url to a CheckboxListTile

I have a CheckboxListTile and a widget in the title tag. How can I create a html text (text + url) to this widget?
My text is: 'Hello Click here!'
I tried this: flutter_html: ^0.10.4
with no luck
Thanks
Edit:
You can try link: ^1.1.0
import 'package:link/link.dart';
...
Link(
child: Text('This is a link to Flutter', style: TextStyle(
decoration: TextDecoration.underline, // add add underline in text
),),
url: 'https://google.com',
onError: _showErrorSnackBar,
),
void _showErrorSnackBar() {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('Oops... the URL couldn\'t be opened!'),
),
);
}
...
Output:
Have you tried url_launcher,
RaisedButton(
onPressed: _launchURL,
child: Text('Text'),
),
_launchURL() async {
const url = 'https://flutter.dev';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
You can also try linkify
linkify("Made by https://cretezy.com");
You could try flutter_linkify.
You need to replace Text with Linkify widget.
With this plugin you code might look like this:
Linkify(
onOpen: (link) => print("Clicked ${link.url}!"),
text: "Made by https://cretezy.com",
);