Related
I am an android developer new to flutter and i am trying to create 3 grey text widgets where when a user clicks one, the clicked one becomes blue while the others remain grey. This is the code i currently have
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: const <Widget>[
InkWell(
child: Text('Click 3',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
InkWell(
child: Text('Click 2',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
InkWell(
child: Text('Click 3',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
],
),
Also whatever i put in onTap() i get the error that says
invalid constant value
You should create three separate variables for each button's color and then remove the const keyword in Row,
Color firstColor = Colors.indigo;
Color secondColor = Colors.indigo;
Color thirdColor = Colors.indigo;
...
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[ /// remove const keyword
InkWell(
child: Text('Click 3',
style: TextStyle(
color: firstColor,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
firstColor=Colors.blue;
});
},
),
InkWell(
child: Text('Click 2',
style: TextStyle(
color: secondColor,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
secondColor=Colors.blue;
});
},
),
InkWell(
child: Text('Click 3',
style: TextStyle(
color: thirdButton,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
thirdColor=Colors.blue;
});
},
),
],
),
I want to use Appbar with text "Back" button I a using below code but
"Back" is coming in two line like below, also Appbar title is moving down side.
Ba
ck
Flutter code for same
final topAppBar = AppBar(
// elevation: 0.1,
backgroundColor: Color.fromRGBO(0, 113, 188, 1.0),
title: Text(
"MyAppBar",
style: TextStyle(
color: Colors.white,
fontFamily: 'Raleway-ExtraBold',
fontWeight: FontWeight.w900,
fontSize: 20.0,
),
),
leading: Padding(
padding: const EdgeInsets.only(left: 0),
child: FlatButton(
child: Text(
"Back",
// textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontFamily: "Raleway-Medium",
fontSize: 14.0,
),
),
),
),
);
Is there any thing which I am missing here??
Use - FittedBox - fit: property to adjust leading widget.
leading: FittedBox(
fit: BoxFit.cover,
child: FlatButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, // add this to remove padding.
onPressed: () {},
child: Text(
"Back",
// textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontFamily: "Raleway-Medium",
fontSize: 14.0,
),
),
),
),
Use leadingWidth with enough width.
leadingWidth: 80,
leading: Padding(
padding: const EdgeInsets.only(left: 0),
child: FlatButton(
child: Text(
"Back",
// textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontFamily: "Raleway-Medium",
fontSize: 14.0,
),
),
),
Sorry if this sounds a bit obvious but this framework is still fairly unknown to me but I have created a bunch of other pages in my app (saved as .dart files) and also have created paths to them through the use of a drawer. I now have have a bottomNavigationBar that has 5 tabs. I want to be able to route to my previously created page using the tabs. I am currently using the defaultTabController.
bottomNavigationBar: Container(
color: Colors.grey[200],
child: Container(
margin: EdgeInsets.symmetric(vertical: 16),
child: TabBar(
isScrollable: true
labelColor: Colors.black,
labelPadding: EdgeInsets.fromLTRB(21, 0, 21, 16),
unselectedLabelColor: Colors.black45,
indicatorColor: Colors.orange,
indicatorPadding: EdgeInsets.fromLTRB(21, 0, 21, 16),
labelStyle: TextStyle(
fontSize: 16, fontWeight: FontWeight.bold
),
tabs: <Tab>[
new Tab(
text: "Home",
),
new Tab(
text: "Page 1",
),
new Tab(
text: "Page 2",
),
new Tab(
text: "Page 3",
),
new Tab(
text: "Page 4",
),
],
),
),
),
For example, when someone clicks on the "Page 1" tab, I want to be able to route the user to my "page1.dart" file. Thanks in advance!
You do not need TabBar if you're using bottomNavigationBar. You can implement bottomNavigationBar by code below.
int currentIndex = 0;
List<Widget> children = [
Home();
Page1();
];
onTapped(int index){
setState(() {
currentIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
onTap: onTapped,
currentIndex: currentIndex,
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
BottomNavigationBarItem(icon: Icon(Icons.add), title: Text('Page 1'))
]),
body: children[currentIndex]);
}
currentIndex will keep the record of the current opened tab. children is the List of pages you want to route in body. onTapped will change currentIndex to index of Navigation Bar.
Doing UI for a flutter app at uni, I just want the text typed into the TextFormField to be white. It seems unnecessarily difficult. I've tried googling etc but can't see an obvious answer.
new Theme(
// this colors the underline
data: theme.copyWith(
primaryColor: Colors.white,
hintColor: Colors.transparent,
),
child: new Padding(
padding: const EdgeInsets.fromLTRB(32.0, 40.0, 32.0, 4.0),
child: TextFormField(
key: Key('username'),
keyboardType: TextInputType.text,
controller: usernameController,
decoration: InputDecoration(
fillColor: Colors.black.withOpacity(0.6),
filled: true,
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(8.0),
),
borderSide: new BorderSide(
color: Colors.transparent,
width: 1.0,
),
),
labelText: 'Username',
labelStyle:
new TextStyle(color: Colors.white, fontSize: 16.0)),
style:
TextStyle(fontSize: 20.0, color: textTheme.button.color),
validator: validateUserName,
onSaved: (val) => this.loginFields._username = val),
),
),
This will do:
TextFormField(
style: TextStyle(color: Colors.white),
)
For anyone trying to do this from a material app's theme: ThemeData property, the color can be changed using the subtitle1 text theme style.
MaterialApp(
...
theme: ThemeData(
...
textTheme: const TextTheme(
...
subtitle1: const TextStyle(
color: Colors.red, // <-- TextFormField input color
),
),
),
)
Puedes usar style: TextStyle
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
TextFormField(
TextFormField(
controller: field,
style: TextStyle(fontSize: 18, color: Colors.red),
decoration: const InputDecoration(
contentPadding: const EdgeInsets.only(
left: 15,
top: 8,
right: 15,
bottom: 0
),
hintText: 'name',
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
)
]
)
)
]
)
)
You can use style inside TextFormField.
Example :
TextFormField(
style: const TextStyle(color: Colors.white),
),
You can use this to change everything
TextFormField(
//controller: _phoneController,
cursorColor: Colors.black,
keyboardType: TextInputType.text,
style: TextStyle(
color: Colors.black
),
decoration: new InputDecoration(
hintStyle: TextStyle(
color: Colors.white
),
border: InputBorder.none,
//contentPadding:
//EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
hintText: "New Playlist"),
),
Padding(
padding: EdgeInsets.all(10),
child: TextFormField(
cursorColor: Color(0XFFFFCC00)//Cursor color change
style: TextStyle(
color: Color(0XFFFFCC00),
decorationColor: Color(0XFFFFCC00),//Font color change
backgroundColor: Color(0XFFFFCC00),//TextFormField title background color change
),
),
You are changing input text color in this line TextStyle(fontSize: 20.0, color: textTheme.button.color), so in order to set in to white just use Colors.white constant instead of textTheme.button.color.
More about text style here.
Overview: The textFormField style is set to a TextStyle defined as MediumStyle. The style affects the characters being displayed in the textbox. Whereas, the labelStyle affect the font displays of the inputdecoration.
TextFormField(
style: MediumStyle,
keyboardType: TextInputType.emailAddress,
focusNode: _employeeEmailFocus,
decoration: InputDecoration(labelText: "Employee Email", labelStyle:MediumBoldStyle),
validator: (val)=> null,
onSaved:(value)=> this._employeeEmail=value,
onFieldSubmitted: (term){
_fieldFocusChange(context,_employeeEmailFocus,_passwordFocus);
},
),
decoration: const InputDecoration(
labelStyle: TextStyle(color: Colors.white),
),
If you want to use Global Change then use this:
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
textTheme: Theme.of(context).textTheme.copyWith(
subtitle1: const TextStyle(color: Colors.green),
)),
home: const MyHomePage(),
);
Otherwise you can do this:
TextFormField(
style: TextStyle(color: Colors.green),
)
If you are trying to change the textColor using the ThemeData.
Now TextField is using titleMedium.
So,
ThemeData(
...
textTheme: const TextTheme(
...
titleMedium: const TextStyle(
color: Colors.red, // <-- TextFormField input color
),
),
),
Add a inputdecoration class for textformfield
this is i think so
decoration: InputDecoration(
prefixStyle: new TextStyle(
),
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),
],
)