I've been trying to use Flutter Linkify to show a webpage. The link I am planning to use is long (and non-esthetic). I would like to edit the link, for example I want for the screen to read 'Privacy Policy' while the link being 'https://www.long.web/address/.../'
Has anyone done something similar?
SelectableLinkify(
onOpen: (link) async {
if (await canLaunch(link.url)) {
await launch(link.url);
} else {
throw 'Could not launch $link';
}
},
text: "Terms of use: https://www.long.web/.../...",
style: Sizes.style2,
linkStyle: Sizes.styleLink,
),
I was able to find a solution for this, in case anyone needs it.
Container(
width: SizeConfig.safeBlockHorizontal * 75,
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: 'I accept the ',
style: Sizes.style2,
),
TextSpan(
style: Sizes.styleLink,
text: 'Terms of Use',
recognizer: TapGestureRecognizer()
..onTap = () async {
final url = 'web where doc is hosted';
if (await canLaunch(url)) {
await launch(
url,
forceSafariVC: false,
);
}
},
),
TextSpan(
text: ' and ',
style: Sizes.style2,
),
TextSpan(
style: Sizes.styleLink,
text: 'Privacy Policy',
recognizer: TapGestureRecognizer()
..onTap = () async {
final url = 'web where doc is hosted';
if (await canLaunch(url)) {
await launch(
url,
forceSafariVC: false,
);
}
},
),
],
),
),
)
All you have to do is add your own webpage so the link opens it, we just used the same page where our privacy policy is so users could access it.
Related
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"/>
This question already has answers here:
Flutter (Dart) How to add copy to clipboard on tap to a app?
(5 answers)
Closed 8 months ago.
I have to copy data of a certian field to the clipboard when the textbutton.icon is pressed.How can I do that?
Try below code, refer setData and ClipboardData or you can be use clipboard package
import below library/package in your code
import 'package:flutter/services.dart';
Your Widget:
InkWell(
onTap: () {
Clipboard.setData(
ClipboardData(
text: "Your Copy text",
),
);
},
child: Text("Your Copy text"),
),
Have you tried googling?
First google result:
import 'package:flutter/services.dart';
onTap: () {
Clipboard.setData(ClipboardData(text: "your text"));
},
source: https://www.codegrepper.com/code-examples/dart/how+to+copy+text+to+clipboard+flutter
Please try below code
Using ClipBoard
// copy to clipboard
Future<void> _copyToClipboard() async {
await Clipboard.setData(ClipboardData(text: "Text to be copied"));
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Copied to clipboard'),
));
}
IconButton(
icon: const Icon(Icons.copy),
onPressed: _copyToClipboard,
),
// Get Data From System Clipboard
String _textValue = '';
void _getClipboard() async {
ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);
setState(() {
_textValue = data.text;
});
}
// or You can replace Clipboard.kTextPlain with text/plain to getData // from Clipboard.
ClipboardData data = await Clipboard.getData('text/plain');
Using Selectable Text
SelectableText(
"text to be copied" ?? "",
style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold, fontSize: 48),
textAlign: TextAlign.center,
toolbarOptions: ToolbarOptions(copy: true, selectAll: true),
),
I have a line that has three buttons inside it. I know that I can use RichText with Gesture Recogniser to make them clickable like HTML <a href=""> tag. But I am new to flutter and don't know how can I achieve that behaviour.
Also is there any better option to do similar thing?
Also I don't want to open an External Link. Just navigate to another page.
I have attached a picture containing what I want to do. Picture of what I want to do
import 'package:flutter/gestures.dart';
and the widget
RichText(
text: TextSpan(
children: [
TextSpan(
text: "By Clicking Sign up you are agree to the ",
),
TextSpan(
text: "Privacy Policy ",
style: TextStyle(
color: Colors.lightGreen,
),
recognizer: new TapGestureRecognizer()
..onTap = () => print("Privacy Policy"),
),
TextSpan(text: "and our "),
TextSpan(
text: "Terms and Conditions ",
style: TextStyle(
color: Colors.lightGreen,
),
recognizer: new TapGestureRecognizer()
..onTap = () => print("Terms and Conditions"),
),
TextSpan(text: "and "),
TextSpan(
text: "Cookie Statement",
style: TextStyle(
color: Colors.lightGreen,
),
recognizer: new TapGestureRecognizer()
..onTap = () => print("Cookie Statement"),
),
TextSpan(text: "."),
],
),
),
does it help?
you can wrap the Text widget with GestureDetector()
GestureDetector(
child: Text(
'Clickable text'
),
onTap: () {
//go to destination page
},
)
In case if you're opening to an external link you can check URL launcher to open websites
I am doing just a chat on Firebase. But I want to add the ability to mark a person in the chat, and then send a notification to him by push notification.
How can this be done with flutter?
This should be implemented like any chat application, like: #Mike, How are you?
You will need to accomplish it using the following steps. Haven't implemented it yet, but I planned do to something similar for my app.
Create a future that returns userId taking username as a parameter.
Future<String> getUserFromUsername(String word) async {
final result = await FirebaseFirestore.instance
.collection('users')
.where('username', isEqualTo: word)
.limit(1)
.get();
return result.docs.map((doc) => doc.data()['userID'].toString()).first;
}
Use RichText to use separate styles for separate words.
RichText(
text: TextSpan(
children: userData.wildestGoal.split(' ').map((word) {
if (word.startsWith('#')) {
return TextSpan(
text: '$word ',
style: TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 16.0
),
recognizer: TapGestureRecognizer()
..onTap = () async {
final String userId =
await FireStoreService(
userId: FirebaseAuth
.instance.currentUser.uid)
.getUserFromUsername(word
.split('#')[1]
.toString());
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
ProvidersForUserProfile(
userId: userId,
)
)
);
}
);
}
return TextSpan(
text: '$word ',
style: const TextStyle(fontSize: 16.0));
}).toList()),
)
In my app, I have some (youtube)links, which always have the text value of "Link", but their tap launch parameter is different as the content of the page is changing.
I recognized that the app first builds these links with the initial correct value, then, upon changes, they won't get updated.
RichText _createExerciseLink(String url) {
return new RichText(
text: new TextSpan(
children: [
new TextSpan(
text: Translate.of(context).trans("exercise.link"),
recognizer: new TapGestureRecognizer()
..onTap = () {
print("Launcihng: ${url}");
launch(url);
},
),
],
),
);
}
But, if I change the text "link" to the corresponding HTTP link then it gets rerendered...
RichText _createExerciseLink(String url) {
return new RichText(
text: new TextSpan(
children: [
new TextSpan(
text: url,
recognizer: new TapGestureRecognizer()
..onTap = () {
print("Launcihng: ${url}");
launch(url);
},
),
],
),
);
}
I assume this is some kind of a caching mechanism?
Is there any way to force to recreate the whole widget?
Seems like adding a Key is the trick here:
Parent widget:
Key key = new Key(link);
...
link != null ? _createExerciseLink(link, key) : Container(),
and in the text:
RichText _createExerciseLink(String url, Key key) {
return new RichText(
key: key,
text: new TextSpan(
children: [
new TextSpan(
text: Translate.of(context).trans("exercise.link"),
recognizer: new TapGestureRecognizer()
..onTap = () {
print("Launcihng: ${url}");
launch(url);
},
),
],
),
);
}