"RenderBox was not laid out" text field error in flutter - flutter-layout

I'm trying to put a textfield, the error does not appear, but I think I'm getting a logical error.
I guess sizing is required. But I couldn't understand where to specify.
And the screen is blank.
I don't know if I could explain it properly because my English is not good.
child: Row(
children: <Widget>[
Column(
children: <Widget>[
TextFormField(
decoration: const InputDecoration(
hintText: 'Enter your email',
),
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: null,
child: Text('Submit'),
),
),
],
),
Column(
children: <Widget>[
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20),
backgroundColor: Colors.blueAccent,
),
onPressed: null,
child: const Text('Button'),
),
Row(
children: <Widget>[
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20),
backgroundColor: Colors.blueAccent,
),
onPressed: null,
child: const Text('Button2'),
),
],
)
],
)
],
),
),`

Somehow you have to tell Flutter how should it size the two columns in the row. One of the possible solutions is to wrap the first column in an Expanded widget. As a result the second column will be drawn, its size is determined by the text sizes of Button and Button2). And then the first column will occupy all the remaining space, that's what Expanded means. Code:
Row(
children: <Widget>[
Expanded( // <--- this is added, the rest is unchanged
child: Column(
children: <Widget>[
TextFormField(
decoration: const InputDecoration(
hintText: 'Enter your email',
),
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: null,
child: Text('Submit'),
),
),
],
),
),
Column(
children: <Widget>[
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20),
backgroundColor: Colors.blueAccent,
),
onPressed: null,
child: const Text('Button'),
),
Row(
children: <Widget>[
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20),
backgroundColor: Colors.blueAccent,
),
onPressed: null,
child: const Text('Button2'),
),
],
)
],
)
],
),

Related

How to make widgets not overlap in Stack

I have ListView.seperated and a TextFormField in a Stack, how can I make those widgets not overlap?
Stack(
children: [
Builder(builder: (context) {
if (messages == null) {
return const Center(child: CircularProgressIndicator());
}
return RefreshIndicator(
onRefresh: () async {
ref
.read(messagesProvider.notifier)
.clearMessagesForChat(chat['id']);
},
child: ListView.separated(
controller: chatScrollController,
itemCount: messages.length,
separatorBuilder: (context, index) {
return const Divider(
height: 1,
);
},
itemBuilder: (BuildContext context, int index) {
final message = messages[index];
return ListTile(
contentPadding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
leading: GestureDetector(
onTap: () => showModalBottomSheet(
context: context,
builder: (context) {
return UserProfile(message['author']);
}),
child:
getProfilePicture(context, message['author'], 30)),
title: Row(
children: [
Text(
message['author']['username'],
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(
width: 16,
),
Text(
DateFormat.yMMMd().format(
DateTime.parse(message['created_at']).toLocal()),
style:
const TextStyle(color: Colors.grey, fontSize: 12),
)
],
),
subtitle: Text(
message['content'],
style: const TextStyle(fontSize: 16),
),
onTap: () {},
);
},
),
);
}),
Align(
alignment: Alignment.bottomLeft,
child: Container(
padding: const EdgeInsets.only(left: 10, bottom: 10, top: 10),
height: 60,
width: double.infinity,
color: Colors.white,
child: Row(
children: <Widget>[
const SizedBox(
width: 15,
),
Expanded(
child: TextField(
controller: messageController,
keyboardType: TextInputType.multiline,
minLines: 1,
maxLines: null,
onSubmitted: (_) => sendMessage(chat['id']),
decoration: const InputDecoration(
hintText: 'Write message...',
hintStyle: TextStyle(color: Colors.black54),
border: InputBorder.none),
),
),
const SizedBox(
width: 15,
),
FloatingActionButton(
onPressed: () => sendMessage(chat['id']),
backgroundColor: Colors.blue,
elevation: 0,
child: const Icon(
Icons.send,
color: Colors.white,
size: 18,
),
),
],
),
),
),
],
),
Thanks

Anroid-layout How can make custom button like this?

those three things are clickable buttons, I don't know how to make it....
enter image description here
return Scaffold(
backgroundColor: Colors.grey[200],
body: Padding(
padding: const EdgeInsets.all(28.0),
child: Column(
children: [
Card(
child: new InkWell(
onTap: () {},
child: Container(
child: ListTile(
title: Text(
"Bignners Level Lessons",
),
subtitle: Text("write your text"),
),
height: 100.0,
),
),
),
Card(
child: new InkWell(
onTap: () {},
child: Container(
child: ListTile(
title: Text(
"Bignners Level Lessons",
),
subtitle: Text("write your text"),
),
height: 100.0,
),
),
),
Card(
child: new InkWell(
onTap: () {},
child: Container(
child: ListTile(
title: Text(
"Bignners Level Lessons",
),
subtitle: Text("write your text"),
),
height: 100.0,
),
),
),
],
),
),
);
}
}

Aligning Text in Card under Column and Row

I want to achieve the UI like this. The Title, SubTitle and BottomText are all neatly aligned.
What I have gotten so far by myself is this, it is a Card containing all the widgets. How to I do so that the Subtitle and the BottomText aligns with the Title?
Here is my code:
return ListView(
padding: EdgeInsets.all(8.0),
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: <Widget>[
Container(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0)
),
color: Colors.white,
elevation: 10,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(20.0, 8.0, 8.0, 8.0),
child: Text('Title', style: TextStyle(fontWeight: FontWeight.bold),),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Column(
children: <Widget>[
Text('Text2'),
Text('Text2'),
],
),
Column(
children: <Widget>[
Text('Text2'),
Text('Text2'),
],
),
Column(
children: <Widget>[
Text('Text2'),
Text('Text2'),
],
)
],
),
Container(
padding: EdgeInsets.all(8.0),
color: Colors.orange,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Text2'),
Text('Text2'),
Checkbox(value: false,)
],
),
)
],
),
),
)
],
);
Just a little improvement, and you will achieve want you want to achieve.
Final solution:
return ListView(
padding: EdgeInsets.all(8.0),
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: <Widget>[
Container(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(topLeft: Radius.circular(40.0), topRight: Radius.circular(40.0), bottomLeft: Radius.circular(15.0), bottomRight: Radius.circular(15.0))
),
color: Colors.white,
elevation: 10,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(20.0, 30.0, 8.0, 8.0),
child: Text(
'Title',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
Container(
padding: EdgeInsets.all(20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
children: <Widget>[
Text('Text2'),
Text('Text2'),
],
),
Column(
children: <Widget>[
Text('Text2'),
Text('Text2'),
],
),
Column(
children: <Widget>[
Text('Text2'),
Text('Text2'),
],
)
])),
Container(
padding:
EdgeInsets.symmetric(vertical: 8.0, horizontal: 20.0),
decoration: BoxDecoration(
color: Colors.orange,
borderRadius: BorderRadius.circular(15.0)),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Text2'),
Text('Text2'),
Checkbox(
value: false,
)
],
),
)
],
),
),
)
],
);
And here how your result will look like:

Flutter text display issue

Am writing a very basic app that just displays numbers on the screen and you can increase and decrease the value of this number with some buttons.
Works fine, except for some reason the text display often goes nuts. I'll show you what I mean:
The above text is trying to display a value of 20, but it is struggling for some reason, as you can see.
This only appears to be happening with double-digit or higher values. My code:
class _SinglePlayerModeParentState extends State<SinglePlayerMode> {
#override
void initState() {
super.initState();
SystemChrome.setEnabledSystemUIOverlays([]);
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft,]);
}
#override
Widget build(BuildContext context) {
Widget numberDisplay = GestureDetector(
onTap: () {
print("GestureDetector");
_incrementBy1();
},
child: Center(child: Text(_numberValue.toString(),
style: TextStyle(
fontSize: 200.0,
color: Colors.white,
),
),
),
);
This code was already displaying these text issues before I rearranged the code to include the following as well:
Widget buttonRow() {
return Row(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 40.0, right: 20.0),
child: FloatingActionButton(
heroTag: null,
onPressed: _numberValueMinus5,
child: Text('-5'),
),
),
Padding(
padding: EdgeInsets.only(left: 20.0, right: 40.0),
child: FloatingActionButton(
heroTag: null,
onPressed: _numberValueMinus1,
child: Text('-1'),
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 40.0, right: 20.0),
child: FloatingActionButton(
heroTag: null,
onPressed: _numberValuePlus1,
child: Text('+1'),
),
),
Padding(
padding: EdgeInsets.only(left: 20.0, right: 40.0),
child: FloatingActionButton(
heroTag: null,
onPressed: _numberValuePlus5,
child: Text('+5'),
),
),
],
),
],
);
}
return MaterialApp(
title: 'Bean Counter',
home: Scaffold(
backgroundColor: Colors.blueAccent,
body: Column(
children: [
Padding(
padding: EdgeInsets.only(top: 90.0, bottom: 40.0),
child: numberDisplay,
),
buttonRow(),
],
),
),
);
}
Not sure what I can do differently here, it's just text!
Any thoughts?
Edit: Testing the FittedBox solution.
#override
Widget build(BuildContext context) {
Widget numberDisplay = GestureDetector(
onTap: () {
print("GestureDetector");
_incrementBy1();
},
child: Center(
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(_numberValue.toString(),
style: TextStyle(
fontSize: 200.0,
color: Colors.white,
),
),
),
),
);
}
Results: this is trying to display the number 30:
As you can see, it still isn't displaying properly. I also tried BoxFit.contain, same issue. Information obtained from this reference: https://docs.flutter.io/flutter/painting/BoxFit-class.html
Any other ideas guys? Or did I implement FittedBox incorrectly?
Edit: Tried with AutoSizeText, as recommended below. Code:
#override
Widget build(BuildContext context) {
Widget scoreText = GestureDetector(
onTap: () {
print("GestureDetector");
_incrementBy1();
},
child: Center(
child: AutoSizeText(_numberValue.toString(),
style: TextStyle(
fontSize: 200.0,
color: Colors.white,
),
),
),
);
Results:
Argh! Displaying text on a clear screen. Level: difficult.

Flutter How to using text field inside ListTile?

I wanna implement a ListTile which in the tailing is a textfield, this is something like iOS static tableview, so that users can click it and directly edit text.
How to do this in Flutter anyway?
For the future people may also have this trouble, here is the solution:
new ListTile(
title: Text('签名'),
trailing: new Container(
width: 150.0,
child: new Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
new Expanded(
flex: 3,
child: new TextField(
textAlign: TextAlign.end,
decoration:
new InputDecoration.collapsed(hintText: '$userAddr'),
),
),
new Expanded(
child: new IconButton(
icon: new Icon(Icons.chevron_right),
color: Colors.black26,
onPressed: () {},
),
)
],
),
),
),
Always using a Expand for textfiled.
Right now, on V1.12 of flutter, you can put a Row in the title and populate it
ListTile(
title: Row(
children: <Widget>[
Expanded(
child: Text('签名')
),
Expanded(
child: TextField(
// your TextField's Content
),
),
],
),
trailing: IconButton(
icon: new Icon(Icons.chevron_right),
color: Colors.black26,
onPressed: () {},
)
),
Try the following code:
ListTile(
title: Text("Price"),
trailing: SizedBox(
width: MediaQuery.of(context).size.width * 0.2,
child: TextField(),
),
),

Resources