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),
),
Related
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'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.
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);
}
}
}
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",
);
I have applied sweetalert in my project my application is in landscape direction and sweetalert take the full screen I want to set custom width and height to sweetalert.
SweetAlert.show(context,
title: "Just show a message",
subtitle: "Sweet alert is pretty",
style: SweetAlertStyle.confirm,
showCancelButton: true, onPress: (bool isConfirm) {
if (isConfirm) {
SweetAlert.show(context,style: SweetAlertStyle.success,title: "Success");
// return false to keep dialog
return false;
}
});
There is no any option according to my assessment for your question. but I think rflutter_alert is the best option to use instead of sweetalert here you can use the width and height to customize it. Hope that will help you. you can add multiple buttons in buttons: [],
import 'package:rflutter_alert/rflutter_alert.dart';
Alert(
context: context,
type: AlertType.error,
title: "RFLUTTER ALERT",
desc: "Flutter is more awesome with RFlutter Alert.",
buttons: [
DialogButton(
child: Text(
"COOL",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () => Navigator.pop(context),
width: 120,
)
],
).show();
Okay, wrap your string with three quote (''' String '''), Add space or new lines what you want base on your expected height.
https://api.dartlang.org/stable/2.5.0/dart-core/String-class.html
SweetAlert.show(
context,
title: '''
Just show a message
''',
subtitle: '''
Sweet alert is pretty
''',
style: SweetAlertStyle.confirm,
showCancelButton: true,
onPress: (bool isConfirm) {
if (isConfirm) {
SweetAlert.show(context,
style: SweetAlertStyle.success, title: "Success");
// return false to keep dialog
return false;
}
},
);