i just started to learn flutter and I have a problem for making layout like this picture below, what code for make layout enter image description here
Here is my code :
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
height: 100.0,
color: Colors.white,
child: Text('Container 1'),
),
SizedBox(
height: 20.0,
),
Container(
height: 100.0,
color: Colors.blue,
child: Text('Container 2'),
),
SizedBox(
height: 20.0,
),
Container(
height: 100.0,
color: Colors.red,
child: Text('Container 3'),
),
],
),
),
),
);
}
}
From that code that I write, absolutely different from what i expected to be, please help, i'm new on this and want to make one step ahead for my skill to be better. Thank you...
I just convert your Column Widget to Row and add one center widget inside with Expand and add Column widget there.
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
color: Colors.red,
height: 100.0,
width: 100.0,
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
color: Colors.yellow,
height: 100.0,
width: 100.0,
),
Container(
color: Colors.green,
height: 100.0,
width: 100.0,
)
],
),
),
Container(
color: Colors.blue,
height: 100.0,
width: 100.0,
),
],
),
Here you go
Scaffold(
backgroundColor: Colors.teal,
body: Container(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
color: Colors.red,
height: 100.0,
width: 100.0,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
color: Colors.yellow,
height: 100.0,
width: 100.0,
)
],
),
),
Container(
color: Colors.blue,
height: 100.0,
width: 100.0,
),
],
),
),
),
Related
I can do that using VerticalDirection.down in the Colum widget, but there is no horizontal direction property in the Row widget. Is there a way to put this into practice?
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.blueGrey,
body: SafeArea(
child: Row(
children: <Widget>[
Container(
child: Text("Hello world"),
height: 100.0,
width: 100.0,
color: Colors.blueAccent,
),
Container(
child: Text("Hello World"),
height: 100.0,
width: 100.0,
color: Colors.greenAccent,
),
Container(
child: Text("Hello World"),
height: 100.0,
width: 100.0,
color: Colors.amberAccent,
),
],
)),
),
);
}
Try adding
Row(
textDirection: TextDirection.rtl
//...
)
Add mainAxisAlignment: MainAxisAlignment.end If you want the items to remain on the left side
Row(
textDirection: TextDirection.rtl,
mainAxisAlignment: MainAxisAlignment.end,
//...
)
Just add to this line, mainAxisAlignment: MainAxisAlignment.end
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
child: Text("Hello world"),
height: 100.0,
width: 100.0,
color: Colors.blueAccent,
),
Container(
child: Text("Hello World"),
height: 100.0,
width: 100.0,
color: Colors.greenAccent,
),
Container(
child: Text("Hello World"),
height: 100.0,
width: 100.0,
color: Colors.amberAccent,
),
],
)
put mainAxisAlignment: MainAxisAlignment.end inside row
There is a gap between the two elements , how to eliminate them?
sample code and effect screenshots are as follows :
code:
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
color: Colors.red, // Color(0xFF275FF0),
child: Column(
// mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 5),
Container(
height: 100,
color: Colors.white,
),
Container(
height: 100,
color: Colors.white,
),
Container(
height: 100,
color: Colors.white,
),
])))
}
I did not notice those pixels before. To avoid that you should use ListView instead of Column.
Here is an example code with a similar code than yours.
return Scaffold(
body: SafeArea(
child: ColoredBox(
color: Colors.red,
child: ListView(
shrinkWrap: true,
children: [
const SizedBox(
height: 5,
),
Container(
height: 100,
color: Colors.white,
),
Container(
height: 200,
color: Colors.white,
),
Container(
height: 300,
color: Colors.white,
)
],
),
),
),
);
I also use ColoredBox as it is more optimized and specific than Container
This is what I have:
source code:
Container(
width: 150,
height: 150,
color: Colors.grey,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 100,
width: 100,
color: Colors.green,
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
child: Text('text'),
color: Colors.blue,
),
Container(
child: Text('text'),
color: Colors.red,
),
],
),
],
),
),
),
How can I make the blue and red container to match the green container's width without making them larger as the green container?
this is what I want to get without using a fixed with for the containers inside the row. Also I have more than one element in the column and I don't want to use fixed sizes.
If the size if fixed then you can add a width to the container which are wrap with the row widget.
The reason i gave width 50 is beacause the parent container is having width of 100 already so remaning width are given to containers
Container(
width: 150,
height: 150,
color: Colors.grey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 100,
width: 100,
color: Colors.green,
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 50,
child: Text('text'),
color: Colors.blue,
),
Container(
width: 50,
child: Text('text'),
color: Colors.red,
),
],
),
],
),
),
You can use flexible widget to give row fixible width
Container(
width: 150,
height: 150,
color: Colors.grey,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 100,
width: 100,
color: Colors.green,
),
Container(
width: 100,
child: Row(
children: [
Flexible(
flex: 1,
fit: FlexFit.tight,
child: Container(
child: Text('text'),
color: Colors.blue,
),
),
Flexible(
flex: 1,
fit: FlexFit.tight,
child: Container(
child: Text('text'),
color: Colors.red,
),
),
],
),
),
],
),
),
)
Container(
width: 150,
height: 150,
color: Colors.grey,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 100,
width: 100,
color: Colors.green,
),
Container(
width: 100,
Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child:
Container(
child: Text('text'),
color: Colors.blue,
),
),
Expanded(
child:
Container(
child: Text('text'),
color: Colors.red,
),
),
],
),
),
],
),
),
),
You can wrap only children of Row with Expanded widget.
Try this, you have to add Exapnded to the childeren of row widgets
Container(
padding: EdgeInsets.all(16),
color: Colors.grey,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
color: Colors.green,
child: Text('Hdddfhlsd', style: primaryTextStyle()),
),
Row(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: Container(
child: Text('text'),
color: Colors.blue,
),
),
Expanded(
child: Container(
child: Text('text'),
color: Colors.red,
),
),
],
)
],
),
)
**Replace your container by this **
Container(
width: 150,
height: 150,
color: Colors.grey,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 100,
child: Column(
children: [
Container(
height: 100,
width: 100,
color: Colors.green,
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Container(
child: Text('text'),
color: Colors.blue,
),
),
Expanded(
child: Container(
child: Text('text'),
color: Colors.red,
),
),
],
)
],
),
),
],
),
),
)
My aim is to reproduce this UI layout in Flutter:
So far I have produced a layout with the below code which is missing the green square below the yellow square in the middle of the screen. How can i include the green square below the yellow square and center them? Can this be done by inserting a column inside a row? Thanks.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: 100.0,
color: Colors.red,
),
Container(
width: 100.0,
height: 100.0,
color: Colors.yellow,
),
Container(
width: 100.0,
color: Colors.blue,
),
],
),
),
),
);
}
}
Simply wrap the inner container with a Column widget.
Here you go
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: 100.0,
color: Colors.red,
),
Column(
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,
),
],
),
),
),
);
}
}
import 'package:flutter/material.dart';
void main() => runApp(DiceApp());
class DiceApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
appBar: AppBar(
title: Text('Dice App'),
backgroundColor: Colors.teal,
centerTitle: true,
),
body: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
width: 100,
color: Colors.red,
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 100,
width: 100,
child: Text('2'),
color: Colors.yellow,
),
Container(
height: 100,
width: 100,
child: Text('2'),
color: Colors.green,
),
],
),
Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Container(
width: 100,
// alignment: Alignment.center,
child: Text('1'),
color: Colors.blue,
),
])
],
),
),
);
}
}
This code gives you the same layout shown in this example
// Scroll Area
child: ListTile(
// Horizontal Row
title: Row(
children: <Widget>[
// First element in Row
ImageIcon(
AssetImage("assets/images/my_photo.png"),
),
// Second element in Row
Column(
children: [
child: Text('My name'),
Row(
children: [
child: Text('My title'),
],
Image.asset(
'assets/images/more.png',
),
),
], // End of Column children
), // End of Column
], // End of Row children
), // End of Row
), // End of Scroll
remember to add infinite height to the containers in red and blue
here is my flutter code that I made .
Create 04 containers and wrap the middle one in a column.
body: SafeArea(
child:Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
child: Text(' Container 1'),
color: Colors.red,
width: 100.0,
height: double.infinity,
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Text(' Container 2'),
color: Colors.yellow,
width: 100.0,
height:100.0,
),
Container(
child: Text(' Container 3'),
color: Colors.green,
width: 100.0,
height:100.0,
),
],
),
Container(
child: Text('Container 4'),
color: Colors.blue,
width: 100.0,
height: double.infinity,
)
],
)),
),
);
}
}
i'm currently taking a course where i need to recreate this layout:
Layout design
I'm struggling to set that transparent yellow color on the second box in the center of the scaffold. This is my code:
class myApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(scaffoldBackgroundColor: Colors.teal),
home: Scaffold(
body: SafeArea(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
color: Colors.red,
height: double.infinity,
width: 100.0,
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
color: Colors.yellow,
height: 100.0,
width: 100.0,
),
Container(
width: 100.0,
height: 100.0,
color: Colors.yellowAccent,
)
]),
Container(
color: Colors.blue,
height: double.infinity,
width: 100.0,
)
],
),
),
),
);
}
}
Thank you for the help.
Just use with Colors.yellow.withOpacity(0.3) for the second box
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(scaffoldBackgroundColor: Colors.teal),
home: Scaffold(
body: SafeArea(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
color: Colors.red,
height: double.infinity,
width: 100.0,
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
color: Colors.yellow,
height: 100.0,
width: 100.0,
),
Container(
width: 100.0,
height: 100.0,
color: Colors.yellow.withOpacity(0.3),
)
]),
Container(
color: Colors.blue,
height: double.infinity,
width: 100.0,
)
],
),
),
),
);
}
}