How can i format(bold) a word within a text? - android-studio

In the code below, i want to make the value of "$_counter" bold. How to accomplish that?
new Text(
'You have pushed the button $_counter time${_counter > 1
? 's'
: ''}',
style: new TextStyle(fontSize: fontSize),
textAlign: TextAlign.center,
),
I want output to be something like this:
You have pushed the button 2 times

you have to use RichTextfor that
new RichText(
textAlign: TextAlign.center,
text: new TextSpan(
style: TextStyle(color: Colors.black),
children: <TextSpan>[
new TextSpan(text: 'You have pushed the button '),
new TextSpan(text: '$_counter', style: new TextStyle(fontWeight: FontWeight.bold)),
new TextSpan(text: ' time!'),
],
),
)

You can try appending Text Widgets in a row to achieve this.
final prefixText = Text('You have clicked the button');
final counterText = Text(' $_counter', style: TextStyle(fontWeight: FontWeight.bold),);
final suffixText = Text(' times');
return Scaffold(
body: Center(
child: Row(
children: <Widget>[prefixText, counterText, suffixText],
),
),
);
NOTE: Though you get the desired output using this, I think the answer of #Raouf Rahiche is more appropriate.

You can fontWeight for it.
child: new Text(
StringUtil.FORGOT_PASSWORD,
style: new TextStyle(
color: const Color(0xFF8C919E),
fontSize: 12.0,
letterSpacing: 0.3,
fontWeight: FontWeight.bold),
),

Related

[FLUTTER]: Cut text depending of height of ListTile

I have the following widget. The problem is I need to cut my subtitle string depending on the height and width of the ListTile. Now, height of the ListTile depends on length of substring I have. Longer is my substring, longer is the height of the ListTile. It looks awkward. Putting ListTile inside Container widget is good, but the substring overlaps the next ListTile content. it looks terrible too. Another solution was using FittedBox, I thought. But, I need the fixed size of each ListTile, without changing the fontSize. That's why the FixedBox doesn't fit me:) It changes the fontSize. I need to cut my string when it reaches the end of the ListTile. How can I solve this?
InkWell(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => ProductScreen(product),
),
);
},
child: Column(
children: [
Container(
padding: const EdgeInsets.symmetric(
vertical: 8.0,
horizontal: 4.0,
),
child: ListTile(
title: Text(
product.title.toUpperCase(),
style: Styles.listTitleStyle,
),
subtitle: Text(
product.description,
style: Styles.listBodyStyle,
),
),
),
const Divider(height: 1.0),
],
For your text widget add something like this,
subtitle: Text(
maxLines: 2, //customize your number of lines
overflow: TextOverflow.ellipsis, //add this to set (...) at the end of sentence
product.description,
style: Styles.listBodyStyle,
),

the argument for the named parameter 'child' was already specified

I can't put icon and text at the same time for floatbuttom?
The error in the title.
return new Scaffold(
appBar: new AppBar(
title: new Text('Hello'),
backgroundColor: Colors.blue,
),
body: new Container(
padding: new EdgeInsets.all(22.0),
child: new Column(
children: <Widget>[
new Text("it's working, $name"),
new FlatButton(onPressed:()=> onClick('test'), child: new Icon(Icons.accessibility),child : new Text('data'))
],
)));
}
}
Your problem is inside the FlatButton widget, you are using child attribute two times.
You can put text and icon, this way:
FlatButton.icon(
icon: Icon(Icons.accessibility),
label: Text('data'),
onPressed: () {
//Code to execute when Button is clicked
},
)
I believe wrapping both children in either a row or column would solve your problem.
FlatButton can only take one widget as the child parameter.
FlatButton(
onPressed:()=> onClick('test')},
child: Row(
children: <Widget>[
new Icon(Icons.accessibility),
new Text('data'),
],
),
),

How do I add both onPressed and onLongPress to a Flutter Center Widget inside a Row?

I am trying in vain to add onLongPress to an image button along with onPressed. I get error: The named parameter 'onDoubleTap' isn't defined.
I have multiple rows of 2 horizontal image buttons using rows and expanded. Everything works except onLongPress (or onDoubleTap). What am I doing wrong, do I have to rebuild the entire code in a different format, or am I overcomplicating this?
I also tried to add a new child (error: already defined), GestureDetector, InkWell, with no success.
body: SingleChildScrollView(
child: Container(
child: Column(
children: <Widget>[
Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
launchURL();
});
},
//Trying to add onLongPress , error: "onLongPress not defined
//If I try add a new child it says child already defined
onLongPress: () => _showAlertMessage(context, "message"),
padding: EdgeInsets.all(6.0),
child: Image.asset(
'images/image1.png',
))),
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
launchURL();
});
},
padding: EdgeInsets.all(6.0),
child: Image.asset(
'images/image2.png',
)
)//flat button
),//expanded
])), //row-center
//Repeat above for rows of 2 more image buttons
The code runs with a single onPressed for each button and does not display any errors, but adding any second click event displays errors.
Flatbutton only supports the onPressed callback, not onLongpressed, etc. Suggest you re-look at using gesture detector or other widgets that support long press.
I would try using two containers, each wrapped in a gesture detector widget, as children in the row. Each container having a text and/or image content. You can then pick up onTap, double tap, long press, etc on each container. Not tested this as I am on my phone but should work. They are probably more elegant widgets to use than container.
Thanks for the feedback, really appreciated. I replaced flatButton with Gesture detector inside a column, used onTap instead of onPressed, put image in new Column, added onLongPress. The code runs. When I added the second Expanded I got this error:
E/InputMethodManager( 7133): prepareNavigationBarInfo() rootView is null
I read here that "RootView is the View in which all the other views are placed. It is like the root node in a tree structure which is the parent to all the children.", but I can't see my mistake here. I did a full restart and that error is gone.
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: GestureDetector(
onLongPress: () {
setState(() {
_showAlertMessage(context, "message");
});
},
onTap: () {
setState(() {
_launchUrl;
});
},
child: Container(
padding: EdgeInsets.all(6.0),
child: Image.asset(
'images/image1.png',
)
)),
), //Expanded
Expanded(
child: GestureDetector(
on: () {
setState(() {
_showAlertMessage(context, "message");
});
},
onTap: () {
setState(() {
_launchUrl;
});
},
child: Container(
padding: EdgeInsets.all(6.0),
child: Image.asset(
'images/image2.png',
)
)),
),
])),
NOTE: As version 2 of flutter FlatButton is deprecated. You should use TextButton instead.
I don't know how you are using the GestureDetector but it's the best solution fit for your question and supports different aspects of user press Gestures. So I prepared the sample code below to see how it works.
body: SingleChildScrollView(
child: Container(
child: Column(children: <Widget>[
Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: GestureDetector(
onTap: () => print('onTap'),
onLongPress: () => print('onLongPress'),
onDoubleTap: () => print('onDoubleTap'),
child: Image.network(
'https://lp-cms-production.imgix.net/image_browser/social-beast-GettyRF_136327669.jpg?auto=format&fit=crop&sharp=10&vib=20&ixlib=react-8.6.4&w=850&q=50&dpr=2'),
) //flat button
),
Expanded(
child: GestureDetector(
onTap: () => print('onTap'),
onLongPress: () => print('onLongPress'),
onDoubleTap: () => print('onDoubleTap'),
child: Image.network(
'https://lp-cms-production.imgix.net/image_browser/lions-hunting-500pxRF_7416880.jpg?auto=format&fit=crop&sharp=10&vib=20&ixlib=react-8.6.4&w=850&q=50&dpr=2'),
)
), //expanded
])
), //row-center
]
)
)
)
There are yet so many other gestures for GestureDetector that can be found here.
You can use InkWell widget
child: InkWell(
onLongPress: () {
setState(() {
tip++;
});
},
child: FloatingActionButton(
heroTag: "btn+",
onPressed: () {
setState(() {
tip++;
});
},
backgroundColor: Colors.grey.shade400,
child: Icon(
Icons.add,
color: Colors.black,
),
),
),

Unwanted action button in flutter appbar

I'm building a screen which has a bottom navigation bar, where I placed an burger action button and a floating button, and an appbar where I want to place my app logo.
Even though i didn't give any action to the appbar, the burger icon still appears at the left of the title.
I would like to only have the title in the appbar.
This is my code:
appBar: new AppBar(
title: new Image.asset('assets/logo/logo-home.png', fit: BoxFit.fitHeight),
actions: null,
backgroundColor: Colors.black,
elevation: 0.0,
),
key: _scaffoldKey,
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
child: ImageIcon(new AssetImage("assets/icons/star.png"), color: Colors.black),
backgroundColor: Colors.white,
onPressed: scan,
),
bottomNavigationBar: BottomAppBar(
color: Color.black,
child: new Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(icon: Icon(Icons.menu), color: Colors.white, onPressed: ()=> _scaffoldKey.currentState.openDrawer(),),
],
),
),
If the scaffold has a drawer and the appbar doesn't contain 'leading' property (or with null value), Appbar will append a menu icon in its actions.
It's mentioned in the documents:
If the leading widget is omitted, but the AppBar is in a Scaffold with a Drawer, then a button will be inserted to open the drawer. Otherwise, if the nearest Navigator has any previous routes, a BackButton is inserted instead. This behavior can be turned off by setting the automaticallyImplyLeading to false. In that case a null leading widget will result in the middle/title widget stretching to start.
The solution is easy, just replace title property with leading in your appbar
appBar: new AppBar(
leading: new Image.asset('assets/logo/logo-home.png', fit: BoxFit.fitHeight),
actions: null,
backgroundColor: Colors.black,
elevation: 0.0,
),

Flutter: How to crop text depending on available space?

How to crop text depending on available space?
There's a similar question here, but there's no working answer.
Currently, this code
return new GridTile(
child: new Card(
elevation: 3.0,
child: new Container(
padding: new EdgeInsets.all(10.0),
child: new Column(
children: <Widget>[
Text(
document['title'],
style: new TextStyle(fontWeight: FontWeight.bold),
),
Text(
document['text'],
),
],
),
),
)
);
Gives this result:
I tried using overflow: TextOverflow.ellipsis, but the text becomes 1 line long. Specifying the text line length is a bad idea, because on different screen sizes results will vary.
How to make so that the text crops, fits or wraps itself into available space?
Setting overflow behavior should do
new Text(
widget.text,
// softWrap: true,
overflow: TextOverflow.fade,
)
See also Flutter: How to hide or show more text within certain length
Wrap the text widget Inside Expanded or Flexible widget and use overflow.ellipsis this worked for me
Column(
children: <Widget>[
Flexible(
child: Text(
"Some Very long Text or random generated strings ",
overflow: TextOverflow.ellipsis,
softWrap: true,
),
),
],
),
Try it:
extension StringX on String {
String get overflow => Characters(this)
.replaceAll(Characters(''), Characters('\u{200B}'))
.toString();
}
Text(
textString.overflow,
overflow: TextOverflow.ellipsis,
maxLines: 1,
)
It works for me.
source: https://github.com/flutter/flutter/issues/18761#issuecomment-812390920

Resources