'How To Copy text from a flutter button class while adding additional texts to clipboard?
ive created a custom button that I use in multiple places in my flutter app. and I've also used the toast package to copy and show text anytime the button is clicked. the problem is i have to manually write out what text the button will copy every single time the button is clicked, this is my code.
code for button, the toast and also how i call the button.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import 'package:deciphernigeria/widgets/toastmessage.dart';
class InformationCard extends StatelessWidget {
final String toptitle;
final String bottomtext;
final String copiedtext;
const InformationCard({
Key key,
this.toptitle,
this.bottomtext,
this.copiedtext,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return InkWell(
onTap:
() {
showtoast();
Clipboard.setData(ClipboardData(text: copiedtext));
},
child: Column(
children: [
Container(
margin: EdgeInsets.symmetric(vertical: 10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
offset: Offset(0.0, 10.0),
blurRadius: 2.0,
spreadRadius: 0.0,
color: Color.fromRGBO(51, 51, 51, 0.16),
),
],
),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(10, 15, 0, 15),
child: SvgPicture.asset(
'assets/icons/information.svg',
height: 30,
),
),
SizedBox(width: 20),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(0,10,30,0),
child: Text(
toptitle,
style: Theme.of(context).textTheme.subtitle2,
),
),
SizedBox(
height: 5,
),
Padding(
padding: const EdgeInsets.fromLTRB(0,0,30,10),
child: Text(bottomtext),
)
],
),
),
],
),
),
],
),
);
}
}
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
void showtoast() => Fluttertoast.showToast(
msg: 'copied text to clipboard',
fontSize: 15,
backgroundColor: Colors.lightGreen,
);
InformationCard(
toptitle: 'the boy',
bottomtext: 'handsome',
copiedtext: 'handsome',
),
you can see when i call the button, i have to manually input the word handsome in the copied text so that handsome is copied when they click the button. i have to also manually write handsome in the bottom text so they know what they are clicking and copying. Please how can i do it that i don't need to manually rewrite the copied text before it is copied and also add some extra text to the copied word. for example since the top text is the boy and the bottom text is handsome, how do i code the button so that it will automatically copy the boy is tall and handsome when clicked without writing the bottom text? so i can use the button multiple times without rewriting. Thanks
Solution 1:[1]
Data classes can prove very handy in this case. All you have to do is, create a class representing the structure of data you want to - display in the UI & copy to clipboard. See the example below:
class InformationToBeCopied {
final String bottomText;
final double pi;
const InformationToBeCopied({
required this.bottomText,
this.pi = 3.14,
});
@override
toString() {
return 'This is the bottom text: $bottomText \nValue of PI: $pi';
}
}
Notice the toString()
method, that is how you will structure the string that needs to be copied to clipboard. You can add as many properties as you want in the InformationToBeCopied
class. Then in your widget, you can make use of toString()
for clipboard and info.bottomText
in the UI.
class InformationCard extends StatelessWidget {
final String toptitle;
final InformationToBeCopied info;
const InformationCard({
Key? key,
required this.toptitle,
required this.info,
}) : super(key: key);
void copyInfoToClipboard() {
showToast();
Clipboard.setData(
ClipboardData(text: info.toString()),
);
}
@override
Widget build(BuildContext context) {
// ..
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | dkpk |