Here is my code
Container(
child: Positioned(
left: offset.dx,
top: offset.dy,
child: GestureDetector(
onPanUpdate: (details) {
setState(() {
offset = Offset(offset.dx + details.delta.dx, offset.dy + details.delta.dy);
print('offset $offset');
});
},
child: Text(_controller.text, textAlign: TextAlign.center, maxLines: 3,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0, color: colorrr)),),
),
)
The easiest way would be for you to put the fontSize in the State of your Widget, and update it from a button or tap somewhere.
Essentially, just like you update the offset, you can update a state variable named fontSize and use it in your TextStyle.
For resize text you may use
dependencies:
auto_size_text: ^3.0.0
import dart code:
import 'package:auto_size_text/auto_size_text.dart';
GestureDetector ontap inside use that code :
onTap: () {
child: AutoSizeText(
'This string will be automatically resized to fit in two lines.',
style: TextStyle(fontSize: 30),
maxLines: 2,
);
Hope you will get solution .
Also to know about package: https://pub.dev/packages/auto_size_text/install
Related
I have two FlatButtons in a row. The row is a column which is in turn in a container that is used for the padding of the screen. This container has padding of right:20 and left:20. I want my buttons to be 20 in from left and 20 in from the right to keep everything aligned in my column. I know the FlatButton widget has default padding and I've tried the solutions in this question Changing the buttons to GestureDectectors widgets works with alignment but doesn't have a very nice user experience, the area to click is too small maybe. Ideally I'd like to remove the padding of the FlatButton on one side left/right depending on which button. Here is my code
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
FlatButton(
child: new Text('SIGN IN',
style: Theme.of(context).textTheme.body1),
onPressed: () {
Navigator.push(
context, SignInPage(widget.onSignedIn));
},
),
new FlatButton(
child: new Text(
'SIGN UP',
style: Theme.of(context).textTheme.body1,
),
onPressed: () {
Navigator.push(
context, SignUpPage(widget.onSignedIn));
},
),
]),
Now with the deprecation of the FlatButton for TextButton
TextButton(
style: TextButton.styleFrom(padding: EdgeInsets.all(0)),
...
)
Use zero padding, for example
padding: EdgeInsets.zero,
To remove extra space from around TextButton and OutlineButton which replace FlatButton use this:
style: OutlinedButton.styleFrom(
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
)
try to use option 'padding: EdgeInsets.only( depends which side u wanna padding )'
FlatButton(
padding: EdgeInsets.only(left: 10.0, right:10.0),
child: new Text('SIGN IN',
style: Theme.of(context).textTheme.body1),
onPressed: () {
Navigator.push(
context, SignInPage(widget.onSignedIn));
},
),
SizedBox(
height: 25,
child: ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
padding: EdgeInsets.zero,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(kDefaultPadding))),
child: const Text("More"),
)
)
The FlatButton is now deprecated and you can use TextButton.
Even if you set padding to 0, there is still some padding left. To remove it completely you can use RawMaterialButton as suggested by #Arshak here.
Example:
RawMaterialButton(
constraints: BoxConstraints(),
padding: EdgeInsets.all(5.0), // optional, in order to add additional space around text if needed
child: Text('Button Text')
)
I know answer is right there but It took me hours to do this. So I thought to add it as answer also.
UI stutters whenever CupertinoDatePicker is rotated due to using the picker in a FormField and calling formFieldState.didChange() on the picker's onDateTimeChanged
I'm trying to use Cupertino input fields in my app.
I've followed the steps in the Flutter Gallery to use CupertinoDatePicker. However, I'm wrapping the input fields inside a Form for validation.
This works, as I was able to tie the state of the picker to the FormFieldState. However, UI performance is abysmal. Every time the picker is rotated, I experience stutters. This is due to continuous call of state.didChange() on onDateTimeChanged
I tried calling state.didChange() only after the modal popup was dismissed, and it got rid of the stutters. But I want the state of the FormField to update as the user spins the picker.
Here's my code snippet.
Widget _getCupertinoDateTimeFormField(FormFieldState<DateTime> state) {
return GestureDetector(
onTap: () async {
await showCupertinoModalPopup<void>(
context: state.context,
builder: (BuildContext context) {
return _buildBottomPicker(
CupertinoDatePicker(
mode: CupertinoDatePickerMode.date,
initialDateTime: state.value?? DateTime.now(),
onDateTimeChanged: (DateTime newDateTime) {
state.didChange(newDateTime);
},
)
);
},
);
},
child: cupertinoDecoration(state, description,
Text(
_formatUI(state.value),
style: const TextStyle(color: CupertinoColors.inactiveGray),
),
),
);
}
Widget _buildBottomPicker(Widget picker) {
return Container(
height: _kPickerSheetHeight,
padding: const EdgeInsets.only(top: 6.0),
color: CupertinoColors.white,
child: DefaultTextStyle(
style: const TextStyle(
color: CupertinoColors.black,
fontSize: 22.0,
),
child: GestureDetector(
// Blocks taps from propagating to the modal sheet and popping.
onTap: () {},
child: SafeArea(
top: false,
child: picker,
),
),
),
);
}
Widget cupertinoDecoration <T> (FormFieldState<T> state, String description, Widget field) {
bool isError = state.errorText != null;
Widget label = Padding(
padding: EdgeInsets.symmetric(vertical: 1.0),
child: Text(
description,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 14.0,
color: CupertinoColors.black,
),
),
);
List<Widget> fieldWidgets = [label, field];
if (isError) {
// add our error message
fieldWidgets.add(Text(
state.errorText,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
color: CupertinoColors.destructiveRed,
fontSize: 12.0,
),
));
}
return Container(
decoration: const BoxDecoration(
color: CupertinoColors.white,
border: Border(
bottom: BorderSide(
color: CupertinoColors.inactiveGray, width: 0.0),
),
),
child: Padding(
padding:
const EdgeInsets.all(6.0),
child: SafeArea(
top: false,
bottom: false,
child: DefaultTextStyle(
style: const TextStyle(
letterSpacing: -0.24,
fontSize: 17.0,
color: CupertinoColors.black,
),
child: Align(
alignment: Alignment.centerLeft,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: fieldWidgets,
),
),
),
),
),
);
}
Am I out of luck here? Are we not supposed to link the state of the picker to the state of the FormField?
Thanks in advance!
I have a RichText with some TextSpan. I want one of the TextSpan widgets to have a background on it so I used background: Paint()..color = Colors.redAccent as its text style.
Is there a way to add some extra red space/padding to the background.
This is how it currently looks:
This is how I want it to look (notice the extra red on the top and bottom of the highlighted text):
This is the code used:
Scaffold(
appBar: new AppBar(),
body: new RichText(
text: new TextSpan(
text: 'This is a one the lines in the span \n ',
style: TextStyle(fontSize: 15.0, color: Colors.black),
children: <TextSpan>[
new TextSpan(
text: 'This is the second line',
style: new TextStyle(fontWeight: FontWeight.bold)),
new TextSpan(
text: ' background on me ',
style: new TextStyle(
fontWeight: FontWeight.bold,
background: Paint()..color = Colors.redAccent,
)),
new TextSpan(text: ' This is another of the lines in the span!'),
],
),
), // This trailing comma makes auto-formatting nicer for build methods.
)
I tried adding height to the text style, but it doesn't affect the height of the background.
The new solution in 2019 with flutter 1.7.8 is WidgetSpan, you could just add a Container and a Padding or any combination of widget to make more variety!
Here is an example to use on the case:
WidgetSpan(
child: Container(
color: Colors.red,
padding: EdgeInsets.all(8.0),
child: Text("background on me", style: ...),
)
)
And don't forget to change <TextSpan>[] to <InlineSpan>[], that's It!
Quite ugly solution, but I haven't found other (
TextStyle(fontWeight: FontWeight.bold,
background: Paint()..color = Colors.redAccent
..strokeWidth = 16.5
..style = PaintingStyle.stroke,)
strokeWidth have to be big enough to cover height of text. In my case 16.5 is minimal suitable value
you can use widgetSpan that inside RichText so that you can add any widget inside RichText.
RichText(
maxLines: 1,
overflow: TextOverflow.ellipsis,
text: TextSpan(
text: "text one", // text of exemple
children: [
// add a space between texts
WidgetSpan(
child: Padding(
padding: const EdgeInsets.only(left: 10.0),
),
),
TextSpan(
text: "text two", // text exemple
),
],
),
),
This question already has answers here:
Flutter - Wrap text on overflow, like insert ellipsis or fade
(23 answers)
Closed 3 years ago.
I want wrap text as text grows. I searched through and tried wrap with almost everything but still text stays one line and overflows from the screen.
Does anyone know how to achieve this?
Any help is highly appreciated!
Positioned(
left: position.dx,
top: position.dy,
child: new Draggable(
data: widget.index,
onDragStarted: widget.setDragging,
onDraggableCanceled: (velocity, offset) {
setState(() {
position = offset;
widget.secondCallback(offset, widget.index);
widget.endDragging();
});
},
child: new GestureDetector(
onTap: () {
widget.callback(widget.caption, widget.index);
},
child: new Text(
widget.caption.caption,
style: new TextStyle(
color: widget.caption.color,
fontSize: widget.caption.fontSize,
),
),
),
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,
),
),
));
The Flexible does the trick
new Container(
child: Row(
children: <Widget>[
Flexible(
child: new Text("A looooooooooooooooooong text"))
],
));
This is the official doc https://flutter.dev/docs/development/ui/layout#lay-out-multiple-widgets-vertically-and-horizontally on how to arrange widgets.
Remember that Flexible and also Expanded, should only be used within a Column, Row or Flex, because of the Incorrect use of ParentDataWidget.
The solution is not the mere Flexible
In a project of mine I wrap Text instances around Containers. This particular code sample features two stacked Text objects.
Here's a code sample.
//80% of screen width
double c_width = MediaQuery.of(context).size.width*0.8;
return new Container (
padding: const EdgeInsets.all(16.0),
width: c_width,
child: new Column (
children: <Widget>[
new Text ("Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 ", textAlign: TextAlign.left),
new Text ("Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2", textAlign: TextAlign.left),
],
),
);
[edit] Added a width constraint to the container
Using Ellipsis
Text(
"This is a long text",
overflow: TextOverflow.ellipsis,
),
Using Fade
Text(
"This is a long text",
overflow: TextOverflow.fade,
maxLines: 1,
softWrap: false,
),
Using Clip
Text(
"This is a long text",
overflow: TextOverflow.clip,
maxLines: 1,
softWrap: false,
),
Note:
If you are using Text inside a Row, you can put above Text inside Expanded like:
Expanded(
child: AboveText(),
)
Use Expanded
Expanded(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text(_name, style: Theme.of(context).textTheme.subhead),
new Container(
margin: const EdgeInsets.only(top: 5.0),
child: new Text(text),
),
],
),
If it's a single text widget that you want to wrap, you can either use Flexible or Expanded widgets.
Expanded(
child: Text('Some lengthy text for testing'),
)
or
Flexible(
child: Text('Some lengthy text for testing'),
)
For multiple widgets, you may choose Wrap widget. For further details checkout this
Try Wrap widget to wrap text as text grows:
Example:
Wrap(
direction: Axis.vertical, //Vertical || Horizontal
children: <Widget>[
Text(
'Your Text',
style: TextStyle(fontSize: 30),
),
Text(
'Your Text',
style: TextStyle(fontSize: 30),
),
],
),
You Can Wrap your widget with Flexible Widget and than you can set property of Text using overflow property of Text Widget.
you have to set TextOverflow.clip
for example:-
Flexible
(child: new Text("This is Dummy Long Text",
style: TextStyle(
fontFamily: "Roboto",
color: Colors.black,
fontSize: 10.0,
fontWeight: FontWeight.bold),
overflow: TextOverflow.clip,),)
hope this help someone :)
Container(
color: Color.fromRGBO(224, 251, 253, 1.0),
child: ListTile(
dense: true,
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
RichText(
textAlign: TextAlign.left,
softWrap: true,
text: TextSpan(children: <TextSpan>
[
TextSpan(text: "hello: ",
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold)),
TextSpan(text: "I hope this helps",
style: TextStyle(color: Colors.black)),
]
),
),
],
),
),
),
You can use Flexible, in this case the person.name could be a long name (Labels and BlankSpace are custom classes that return widgets) :
new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Flexible(
child: Labels.getTitle_2(person.name,
color: StyleColors.COLOR_BLACK)),
BlankSpace.column(3),
Labels.getTitle_1(person.likes())
]),
BlankSpace.row(3),
Labels.getTitle_2(person.shortDescription),
],
)
I want a way to make a TextField or EditableText's text wrap onto another line.
And how to make them multiline.
I don't know if it matters, but the EditableText sits inside a ListTile > Card > Container.
This is my code:
return ListTile(
title: Card(
child: Container(
padding: EdgeInsets.all(10.0),
child: EditableText(
textAlign: TextAlign.start,
focusNode: _focusNode,
controller: _textEditingController,
style: TextStyle(
color: Colors.black,
fontSize: 18.0,
),
keyboardType: TextInputType.multiline,
cursorColor: Colors.blue,
),
),
),
);
It doesn't work please help. I have searched everywhere now!
I'm running flutter version: 0.4.4 and dart version: 2.0.0-dev.54.0
You need to set the property maxLines to either null (for infinite growth) or a fixed number.
By default maxLines is equal to 1.