I am building a flutter app layout which requires to have some widgets to be put horizontally together in a row ,
some Rows should be put together in a column,
i tried to put the rows inside the column directly but it didn't work,
that's the code used:
child: new SingleChildScrollView(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center ,
crossAxisAlignment: CrossAxisAlignment.center ,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(0.0),
child: new Container(
margin: EdgeInsets.all(20.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
//the problem shows up here
],
),
),
),
//card to hold the addition form
Padding(
padding: const EdgeInsets.all(0.0),
child: new Container(
margin: EdgeInsets.all(20.0),
child:new Card(
color:Colors.white,
margin: EdgeInsets.all(1.0),
shape: new RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
...
],
),
),
decoration: new BoxDecoration(boxShadow: [
new BoxShadow(
color: Colors.black12,
blurRadius: 2.0,
offset: new Offset(0.0, 0.0),
),
]),
),
),
],
),
),
),
update:
that's exactly what i want to make
a main column that contain rows and columns to build this view
putting the card inside the column is working fine but nothing work when i add row inside the column
i also updated the code and hope it's completely clear now
the problem was in the alignment
this worked fine
'''
child: new SingleChildScrollView(
// the main content container
child: new Column(
children: [
Padding(
padding: const EdgeInsets.all(1.0),
child: new Container(
margin: const EdgeInsets.only(
left: 20.0, top: 20.0, right: 20.0, bottom: 0.0),
//Current date and time container
child: new Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
],
),
),
),
//card to hold the addition form
Padding(
padding: const EdgeInsets.all(1.0),
child: new Container(
margin: EdgeInsets.all(20.0),
//the addition shadow handler
child: new Card(
color: Colors.white,
margin: EdgeInsets.all(1.0),
shape: new RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4.0)),
//the addition form container
child: new Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
//Type and amount container
Padding(
padding: EdgeInsets.all(12.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
//Amount container
new Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
],
),
//Type container
new Column(
children: <Widget>[
],
),
],
),
),
],
),
),
decoration: new BoxDecoration(boxShadow: [
new BoxShadow(
color: Colors.black12,
blurRadius: 2.0,
offset: new Offset(0.0, 0.0),
),
]),
),
),
],
),
),
'''
Related
Here is the code without body of the widget:
Column(
children: [
Material(
elevation: 0,
child: Image.asset(
i.first,
fit: BoxFit.fitWidth,
),
),
SizedBox(
width: 130,
child: FittedBox(
fit: BoxFit.fitWidth,
child: Padding(
padding: const EdgeInsets.all(30.0),
child: Text(
i.last
),
),
),
),
],
Need to add space between appbar and image.
How can i achieve additional space between image and appbar widgets?
You can use Padding Widget:
body: Padding(
padding: const EdgeInsets.all(8.0),
child: GridView.count(
crossAxisCount: 2,
children: [
...myImageAndCaption.map(
(i) => Column(
mainAxisSize: MainAxisSize.min,
children: [
Material(
elevation: 0,
child: Image.asset(
i.first,
fit: BoxFit.fitWidth,
height: 150,
width: 150,
),
),
SizedBox(
width: 130,
child: FittedBox(
fit: BoxFit.fitWidth,
child: Padding(
padding: const EdgeInsets.all(30.0),
child: Text(
i.last,
style: const TextStyle(
fontSize: 22,
letterSpacing: 1.0,
fontWeight: FontWeight.w600,
color: Colors.black,
),
),
),
),
),
],
),
),
],
),
),
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:
I tried putting a column in a row. But it does not work.
Then I tried putting the row and column in the same safe area using different child properties. It does not work.
The column is supposed to be in the middle of the row.
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
height: 100.0,
width: 100.0,
color: Colors.yellow,
child: Text('Container 1'),
),
Container(
height: 100.0,
width: 100.0,
color: Colors.green,
child: Text('Container 1'),
),
Container(
width: double.infinity,
),
],
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
height: 100.0,
color: Colors.red,
child: Text('Container 1'),
),
Container(
height: 100.0,
color: Colors.blue,
child: Text('Container 1'),
),
],
),
),
),
);
}
}
The error message is "BoxConstraints forces an infinite width."
Try expanded widget with flex attribute, you can put the column in a row:
Container(
width: 300,
height: 100,
child: Row(
children: <Widget>[
Expanded(flex: 1,
child: Container(
color: Colors.red,
width: double.infinity,
height: double.infinity,
),
),
Expanded(flex: 2,
child: Column(
children: <Widget>[
Expanded(flex:1,
child: Container(
color: Colors.yellow,
width: double.infinity,
height: double.infinity,
),
),
Expanded(flex:1,
child: Container(
color: Colors.blue,
width: double.infinity,
height: double.infinity,
),
),
],
),
),
Expanded(flex: 1,
child: Container(
color: Colors.green,
width: double.infinity,
height: double.infinity,
),
),
],
),
),
also, you can place column using a container
body: SafeArea(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: 100.0,
color: Colors.red,
),
Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 100.0,
height: 100.0,
color: Colors.yellow,
),
Container(
width: 100.0,
height: 100.0,
color: Colors.green,
),
],
),
),
Container(
width: 100.0,
color: Colors.blue,
),
],
),
),
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.
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(),
),
),