Flutter question: How to insert a column inside a row in Flutter - flutter

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,
)
],
)),
),
);
}
}

Related

How to remove space between widgets in Column or Row in flutter?

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

Container is not center aligned in stack

I have simple application, and I try to center a circular image in the screen with the following:
main.dart:
import 'package:flutter/material.dart';
import './widgets/mashoff.dart';
void main() => runApp(_MyApp());
class _MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
toolbarHeight: 0,
backgroundColor: Colors.black, // status bar color
),
body: WMashoff(),
),
);
}
}
WMashoff widget:
import 'package:flutter/material.dart';
import './mashoff-item.dart';
class WMashoff extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Stack(
children: <Widget>[
Column(
children: <Widget>[
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
WMashoffItem(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTkMKry-Vcp7oU4erjWxG4pXhfva0weO4n4_g&usqp=CAU',
Alignment.topLeft,
),
WMashoffItem(
'https://hotforsecurity.bitdefender.com/wp-content/uploads/2019/12/pexels-photo-1090393-1024x682.jpg',
Alignment.topRight,
),
],
),
),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
WMashoffItem(
'https://images.indianexpress.com/2019/07/tiktok.jpg',
Alignment.bottomLeft,
),
WMashoffItem(
'https://www.gannett-cdn.com/-mm-/b4f5f67b6112c6935b66ff0cbffc204774fdc87c/c=0-0-3022-1700/local/-/media/2020/08/18/PalmBeachPost/ghows-LK-200819292-73c48e6d.jpg?width=660&height=372&fit=crop&format=pjpg&auto=webp',
Alignment.bottomRight,
),
],
),
),
],
),
Container(
height: 70,
width: 70,
alignment: Alignment.center,
decoration: BoxDecoration(
boxShadow: <BoxShadow>[
const BoxShadow(
color: Colors.black87,
spreadRadius: 5,
blurRadius: 3,
offset: const Offset(0, 3), // changes position of shadow
),
],
image: const DecorationImage(
image: const NetworkImage('http://i.imgur.com/QSev0hg.jpg'),
fit: BoxFit.cover,
),
borderRadius: const BorderRadius.all(const Radius.circular(50)),
border: Border.all(
color: Colors.white,
width: 4,
),
),
),
],
),
);
}
}
WMashoffItem widget:
import 'package:flutter/material.dart';
class WMashoffItem extends StatelessWidget {
final Alignment _favoriteIconAlignment;
final String _img;
WMashoffItem(
this._img,
this._favoriteIconAlignment,
);
#override
Widget build(BuildContext context) {
return Expanded(
child: Stack(
fit: StackFit.expand,
children: <Widget>[
Image.network(
this._img,
fit: BoxFit.cover,
),
Container(
alignment: this._favoriteIconAlignment,
child: const IconButton(
iconSize: 50,
icon: Icon(
Icons.favorite_border,
color: Colors.red,
),
onPressed: null,
),
),
],
),
);
}
}
Please focus the 2nd stack child in the WMashoff widget code (The Container element).
Once I give this container height and width, it is no more centered.
This is the result:
As you can see the image is in the top left. But I want it in the center. Where am I wrong?
When you do:
Container(
height: 70,
width: 70,
alignment: Alignment.center,
child: MyWidget(),
),
You create a 70x70 Container and align its child in the center.
Now, what you want is to align your container at the center of the Stack:
Try this:
Stack(
children: <Widget>[
Column([...]),
Align(
alignment: Alignment.center,
child: Container(
height: 70,
width: 70,
alignment: Alignment.center,
child: MyWidget(),
),
),
],
),

SingleChildScrollView not working in a nested Column. My second column is not scrolling using SingleChildScrollView

SingleChildScrollView not working in a nested Column. My second column is not scrolling using SingleChildScrollView
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
children: [
Container(
height: 200,
),
Container(
child: SingleChildScrollView(
child: Column(
children: [
Container(
color: Colors.purple,
height: 200,
),
Container(
color: Colors.orange,
height: 200,
),
Container(
color: Colors.red,
height: 200,
),
Container(
color: Colors.yellow,
height: 200,
),
],
),
),
),
],
),
),
);
}
In the second column is not scrolling when i using SingleChildScrollView
Try to wrap Container into Expanded Widget. If it doesn't work try to wrap either SingleChildScrollView or second Column
You should use Expanded as a parent of SingleChildScrollView:
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
children: [
Container(
height: 200,
),
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
Container(
color: Colors.purple,
height: 200,
),
Container(
color: Colors.orange,
height: 200,
),
Container(
color: Colors.red,
height: 200,
),
Container(
color: Colors.yellow,
height: 200,
),
],
),
),
),
],
),
),
);
}

Set transparent color of a child to recreate this layout in Flutter

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,
)
],
),
),
),
);
}
}

How to make flutter layout in both column & row as well?

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,
),
],
),
),
),