I want to implement text overflow wrap inside gesture detector - flutter

I am making barcode scanner application and for that after scanning i want my text to be appear inside child: Text in GestureDetector.
And some time my text may be longer so that i want it to be wrap inside child: Text like TextOverflow.ellipsis. Following is the code.
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new GestureDetector(
child: new Flexible(
child: new Container(
child: new Text(
result,
overflow: TextOverflow.ellipsis,
style: new TextStyle(
fontSize: 13.0,
fontFamily: 'Roboto',
color: new Color(0xFF212121),
fontWeight: FontWeight.bold,
),
),
),
),
onLongPress: () {
Clipboard.setData(new ClipboardData(text: result));
key.currentState.showSnackBar(new SnackBar(
content: new Text("Copied to Clipboard"),
));
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text("Sending Message"),
));
},
),
],
),
<br>
Here i had used GestureDetector because i want to copy the text of child:Text on long press.

We have to put GestureDetector inside Container child inside Flexible
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Flexible(
child: new Container(
child: GestureDetector(
child: new Text(
result,
overflow: TextOverflow.ellipsis,
style: new TextStyle(
fontSize: 13.0,
fontFamily: 'Roboto',
color: new Color(0xFF212121),
fontWeight: FontWeight.bold,
),
),
onLongPress: () {
Clipboard.setData(new ClipboardData(text: result));
key.currentState.showSnackBar(new SnackBar(
content: new Text("Copied to Clipboard"),
));
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text("Sending Message"),
));
},
),
),
),
],
),

Related

Modal with centered title and right aligned close/X button

I recently picked up Flutter and Dart and am attempting to create an app that has a modal with three parts: a header, actual content, and a footer.
For the header I am looking to add a title (Text) center aligned and a close button right aligned.
I have the following code:
Column(
children: [
Row(
children: [
Expanded(
child: Text(
"Filters",
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
),
IconButton(
icon: const Icon(Icons.close),
onPressed: () {
Navigator.pop(context);
},
),
],
),
],
),
)
Visually, this looks like so:
At a glance this looks fine but if you stare at it for a bit it doesn't. The "Filters" title isn't actually centered because (I assume) of the width of the X-button. I am struggling to figure out how to deal with this.
What is the proper way to solve this?
You can add empty SizedBox with width as much as IconButton takes, try this:
Row(
children: [
SizedBox(
width: width: 24 +
8 +
8, // the default size of icon + two default horizontal padding for IconButton
),
Expanded(
child: Text(
"Filters",
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
),
IconButton(
icon: const Icon(Icons.close),
onPressed: () {
Navigator.pop(context);
},
),
],
),
or you can add on other IconButton with opacity 0 to hide the button and make title center, like this:
Row(
children: [
Opacity(
opacity: 0,
child: IconButton(
icon: const Icon(Icons.close),
onPressed: () {},
),
),
Expanded(
child: Text(
"Filters",
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
),
IconButton(
icon: const Icon(Icons.close),
onPressed: () {
Navigator.pop(context);
},
),
],
),

How to break this text so it doesn't overflow? - Flutter

I have a widget that builds a list from an array. Each item in the array builds a widget with some columns and rows. I want the text to break so It doesnt overflow.
I've tried adding overflow: TextOverflow.ellipsis to the Text or wrapping the Row that wraps the icon and the text that overflow with a Expanded widget, but both didn't work.
Actually it looks like this:
This is my code:
child: Column(
verticalDirection: VerticalDirection.down,
textBaseline: TextBaseline.alphabetic,
textDirection: TextDirection.ltr,
children: < Widget > [
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: < Widget > [
Row(
children: [
const Icon(Icons.medication),
//
// This is the text that overflows
//
Text(
entries[index].pillname,
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
],
),
Text(
entries[index].pillTimeStr,
style: const TextStyle(
fontSize: 28,
color: Color.fromARGB(255, 79, 79, 79),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: < Widget > [
Container(
margin: const EdgeInsets.all(4),
child: FloatingActionButton(
onPressed: () {
setState(() {
entries[index].pilldone = !entries[index].pillDone;
});
},
tooltip:
entries[index].pillDone ? 'Tomada' : 'No tomada',
backgroundColor: entries[index].pillDone ?
Colors.green :
Theme.of(context).primaryColor,
child: const Icon(Icons.check),
),
),
Container(
margin: const EdgeInsets.all(4),
child: FloatingActionButton(
onPressed: () {
showAdaptiveActionSheet(
context: context,
cancelAction: CancelAction(
title: const Text('Cancelar'),
),
actions: < BottomSheetAction > [
BottomSheetAction(
title: const Text('Modificar'),
onPressed: () {}),
BottomSheetAction(
title: const Text('Eliminar'),
onPressed: () {
Navigator.pop(context);
_askRemovePill(entries[index].iD);
}),
]);
},
tooltip: 'Eliminar',
backgroundColor: Colors.blue,
child: const Icon(Icons.mode_edit),
)),
],
),
],
),
In your case, all you need to do is wrap your Text inside the Row with an Expanded widget
Expanded(
child: Text(/*......your text widget........*/),
),
The Expanded widget gives your Text widget horizontal constraints
IF you don't wish to see the text go on to the next line,
use the overflow property on the text
overflow : TextOverflow.ellipsis, //inside Text
Use the Wrap widget instead of the Row widget. It will break the text to the next line should there be an overflow.
Like this,
Wrap(
children: [
const Icon(Icons.medication),
//
// This is the text that overflows
//
Text(
entries[index].pillname,
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
],
),
Text(
entries[index].pillTimeStr,
style: const TextStyle(
fontSize: 28,
color: Color.fromARGB(255, 79, 79, 79),
),
),
],
),

Flutter "RangeError (index): Invalid value: Valid value range is empty: 0 with WidgetSpan

I am trying to create the picture below in Flutter. As you can see, I need a link and an image icon. However, I am getting the "range" error in the console.
Here a snippet of my code
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Image.asset(
"assets/images/checkbox-check.png",
),
Expanded(
child: Container(
child: Wrap(
children: [
RichText(
text: new TextSpan(
children: [
TextSpan(
text: 'Email opportunities. ',
style: TextStyle(
color: Palette.DARK_BLUE, fontSize: 16.0),
),
TextSpan(
text: 'privacy policy',
style: TextStyle(
color: Palette.DARK_BLUE,
decoration: TextDecoration.underline),
recognizer: TapGestureRecognizer()
..onTap = () {
launch();
},
),
TextSpan(
text: ' and ',
),
TextSpan(
text: 'terms of use',
style: TextStyle(
decoration: TextDecoration.underline),
recognizer: TapGestureRecognizer()
..onTap = () {
launch();
},
),
WidgetSpan(
child: Icon(
Icons.info_outline,
color: Colors.red,
),
)
],
),
),
],
),
),
)
],
);
}
}
Is there another method to create my UI? I don't get an error screen but I see the error in the console. I have a wrap in the widget tree but don't think I need it. but never the less, same issue if I remove it.
This is a Flutter issue when you use TextSpan with recognizer together with WidgetSpan: https://github.com/flutter/flutter/issues/51936
A workaround is to wrap RichText in ExcludeSemantics:
ExcludeSemantics(
excluding: true,
child: RichText( ... ),
),

Flutter: ShowDialog not working with the OnTap() method of a ListTile

I am using a drawer to create a menu that houses ListTiles for the options. I want to create a popup when the tiles are tapped by the user. Currently, the code displays nothing when the tiles are tapped even though after the showDialog there is a Navigator.pop().
// Drawer that includes the ListTiles in question
Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Smash Tracker'),
),
),
ListTile(
title: Text(
'About',
style: TextStyle(
fontFamily: 'Smash',
fontSize: 15.0,
color: Color.fromRGBO(77, 114, 152, 1.0),
),
),
onTap: () {
// Show PopUp
showDialog(context: context, child:
new AlertDialog(
title: new Text(
'About',
style: TextStyle(fontFamily: "Smash"),
),
content: new Text(
'This is a placeholder. This is a placeholder. This is a placeholder. This is a placeholder.',
style: TextStyle(fontFamily: "Smash"),
),
)
);
// Doesn't run
Navigator.pop(context);
},
),
Here is a demo:
via GIPHY
The other ListTiles have the only have Navigator.pop() inside of their onTap() method.
The dialog isn't showing because you are popping it immediately with the Navigator.pop(context). You can await the Dialog since it returns a Future<T> before popping.
I added a demo using your widget tree as an example:
Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Smash Tracker'),
),
ListTile(
title: Text(
'About',
style: TextStyle(
fontFamily: 'Smash',
fontSize: 15.0,
color: Color.fromRGBO(77, 114, 152, 1.0),
),
),
onTap: () async { // mark the function as async
print('tap');
// Show PopUp
// await the dialog
await showDialog(
context: context,
child: new AlertDialog(
title: new Text(
'About',
style: TextStyle(fontFamily: "Smash"),
),
content: new Text(
'This is a placeholder. This is a placeholder. This is a placeholder. This is a placeholder.',
style: TextStyle(fontFamily: "Smash"),
),
));
// Doesn't run
Navigator.pop(context);
},
),
],
),
),
OUTPUT:

Flutter - Draggable text doesn't go to next line

I'd like to make draggable text go to next line if text is too long.
draggable text look like this
return Positioned(
left: position.dx,
top: position.dy,
child: Draggable(
data: widget.index,
child: new GestureDetector(
child: new Text(
widget.caption.caption,
style: new TextStyle(
color: widget.caption.color,
fontSize: widget.caption.fontSize,
),
softWrap: true,
),
onTap: () {
widget.callback(widget.caption, widget.index);
},
),
onDragStarted: widget.setDragging,
onDraggableCanceled: (velocity, offset) {
setState(() {
position = offset;
widget.secondCallback(offset, widget.index);
widget.endDragging();
});
},
feedback: new Material(
type: MaterialType.transparency,
child: new Text(
widget.caption.caption,
style: new TextStyle(
color: widget.caption.color,
fontSize: widget.caption.fontSize),
softWrap: true,
),
),
));
But when I typed long text, still one line and can't see text that overflowed.
I wrapped text with column, flex, flexible, but not wrapping text.
How can I fix this?
Thanks in advance.
Can you wrap your Text like below,
new Row(children: <Widget>[
new Flexible(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text(
widget.caption.caption,
style: new TextStyle(
color: widget.caption.color,
fontSize: widget.caption.fontSize,),
softWrap: true,
)
],
),
),
])
Hope it helps