How to extend the full width of the child in Flutter? - flutter

I have a next markup:
The red block is ListView with a horizontal orientation.
The purple block is just Container for all content.
The white is Container as well but with paddings.
I want to expand the width of the red block on full width. How can I do it? It should look like this:
Code of the markup:
class FifaApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.ltr,
child: Container(
padding: EdgeInsets.symmetric(
vertical: 60.0,
horizontal: 30.0,
),
color: Color(0xFFffffff),
alignment: Alignment.topLeft,
child: Container(
color: Colors.purple,
margin: EdgeInsets.symmetric(vertical: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Math Reports"),
Container(
color: Colors.red,
padding: EdgeInsets.symmetric(vertical: 10.0),
height: 170.0,
child: ListView(
scrollDirection: Axis.horizontal,
// children: renderItems(), // the example of code without green blocks
),
),
],
),
),
),
);
}
}

You can use Stack as below
class FifaApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.ltr,
child: Container(
padding: EdgeInsets.symmetric(
vertical: 60.0,
),
color: Color(0xFFffffff),
alignment: Alignment.topLeft,
child: Stack(
children: <Widget>[
Container(
color: Colors.purple,
margin: EdgeInsets.symmetric(horizontal: 30),
),
Padding(
padding: const EdgeInsets.only(left: 30.0),
child: Text(
"Math Reports",
style: TextStyle(color: Colors.white),
),
),
Container(
height: 170,
color: Colors.red,
margin: EdgeInsets.only(top: 30),
child: ListView(
scrollDirection: Axis.horizontal,
),
)
],
),
),
);
}
}

Related

how to add margin in multiple Container at one time in flutter

how to add margin in tow or more Container at one time
child: Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 100,bottom: 100),
child: Row(
children: [
Expanded(
child: Container(
margin: new EdgeInsets.all(10),
decoration: BoxDecoration(color: Colors.blueAccent),
),
),
Expanded(
child: Container(
decoration: BoxDecoration(color: Colors.amber),
),
),
how to add margin in multiple container at one times or container warped in margin chilled
To do this you have to create a separate reusable widget like this:
class ReusableContainer extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(10),
decoration: const BoxDecoration(color: Colors.blueAccent),
);
}
}
And then use that ReusableContainer widget like this:
Padding(
padding: const EdgeInsets.only(top: 100, bottom: 100),
child: Row(
children: [
Expanded(
child: ReusableContainer(),
),
Expanded(
child: ReusableContainer(),
),
],
),
),

How to convert my horizontal listview in vertical mode like in Disney+ app?

:SliverToBoxAdapter(
child: SizedBox(
child: Column(
children: [
// ignore: prefer_const_constructors
Padding(
padding: const EdgeInsets.only(right: 210.0),
// ignore: prefer_const_constructors
child: Text(
"Great Personality",
// ignore: prefer_const_constructors
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20.0,
),
),
),
Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
height: 200,
child: ListView(
scrollDirection: Axis.horizontal,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
child: Container(
width: 120,
color: Colors.amber,
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 120,
color: Colors.white,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 120,
color: Colors.white,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 120,
color: Colors.white,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 120,
color: Colors.white,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 120,
color: Colors.white,
),
),
],
),
),
)
],
)
],
),
),
),
I want my UI to convert in vertical style whenever I tap on the arrow button like in Disney App Homepage
from this:
enter image description here
to this:
enter image description here
Which Widget I should Use so that Whenever Someone clicks on the right arrow button, it navigate to another page, and whatever Container in horizontal listview gets converted to vertical mode?
You might want a list view inside a list view
Here is an example code:
Widget build(BuildContext context) {
Widget horizontalList1 = new Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
height: 200.0,
child: new ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Container(width: 160.0, color: Colors.red,),
Container(width: 160.0, color: Colors.orange,),
Container(width: 160.0, color: Colors.pink,),
Container(width: 160.0, color: Colors.yellow,),
],
)
);
Widget horizontalList2 = new Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
height: 200.0,
child: new ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Container(width: 160.0, color: Colors.blue,),
Container(width: 160.0, color: Colors.green,),
Container(width: 160.0, color: Colors.cyan,),
Container(width: 160.0, color: Colors.black,),
],
)
);
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Container(
child: ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
horizontalList1,
horizontalList2,
],
),
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
Here is the result:
Horizontal List view inside list view

bottom overflowed by 1000 pixels

I'm trying to make a header (picture with a card half inside pic and half outside and it worked in a stack.
but I'm in trouble when I have to make many containers below so when adding below the stack im having bottom overflowed.
I removed from code the unnecessary code so u can test it in your editor for help :)
any one can help me??
import 'package:flutter/material.dart';
class ServiceDetails extends StatefulWidget {
#override
_ServiceDetails createState() => _ServiceDetails();
}
class _ServiceDetails extends State<ServiceDetails> {
#override
Widget build(BuildContext context) {
var h = MediaQuery.of(context).size.height;
var w = MediaQuery.of(context).size.width;
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
child: Stack(
children:[
Column(
children:[
Container(
height: h * .4,
width: w,
// color: Colors.grey[50],
child: Image.asset('images/hairdresser1.jpg', fit: BoxFit.fitWidth,)
),
new Container(
height: h * .0,
color: Colors.grey[50],
)
]
),
Container(
alignment: Alignment.topCenter,
padding: new EdgeInsets.only(
top: MediaQuery.of(context).size.height * .33,
right: 15.0,
left: 15.0
),
child: Container(
height: 150,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
elevation: 0.0,
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.only(left: 20, bottom: 5, top: 20),
child: Text(
"Hair Dresser",
style: new TextStyle(
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.bold
)
)
),
Container(
padding: EdgeInsets.only(left: 20, bottom: 5),
child: Text(
"With our proffisional barbers, new experience is guaranteed",
style: new TextStyle(
color: Colors.grey,
fontSize: 13
)
)
),
],
)
)
)
),
],
)
),
Container(
padding: EdgeInsets.all(15),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
elevation: 0.0,
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.only(left: 20, bottom: 5, top: 20),
child: Text(
"Frequently Used",
style: new TextStyle(
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.bold
)
)
),
Divider(),
Container(
padding: EdgeInsets.zero,
height: 210,
width: w,
)
],
)
)
),
Container(
padding: EdgeInsets.all(15),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
elevation: 0.0,
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.only(left: 20, bottom: 5, top: 20),
child: Text(
"Frequently Used",
style: new TextStyle(
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.bold
)
)
),
Divider(),
Container(
padding: EdgeInsets.zero,
height: 210,
width: w,
)
],
)
)
),
)
]
)
);
}
}
Wrap your column with SingleChildScrollView and widget tree be like
Scaffold
-SingleChildScrollView
- Column
Use SingleChildScrollView To scroll the page.
SingleChildScrollView requires a scroll controller to change your code as follows
class _ServiceDetails extends State<ServiceDetails> {
ScrollController scrollController = ScrollController();
#override
Widget build(BuildContext context) {
var h = MediaQuery.of(context).size.height;
var w = MediaQuery.of(context).size.width;
return Scaffold(
body: Scrollbar(
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
controller: scrollController,
child: Column(
------------------------------
),
),
),
);
}
}

How to position elements independently using Stack, Row, and similar elements in Flutter

I have been trying to replicate these two designs in Flutter using Stack, Positioned, Row, and similar related widgets, however, I've been getting stuck in the same stages again and again. I either can't center the text or cannot position the back button correctly. Also, I am trying not to use fixed sizes/positions as this is supposed to be somewhat adaptable to different screen sizes.
Could someone point me in the right direction, of how to create this or similar layouts, which would be reused in other screens?
Example 1:
Example 2:
For your example numero 1, I came with this solution:
class DetailScreen extends StatefulWidget {
#override
_DetailScreenState createState() => _DetailScreenState();
}
class _DetailScreenState extends State<DetailScreen> {
#override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
child: SafeArea(
child: Stack(
fit: StackFit.expand,
children: [
Container(
margin: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: Color(0xFF0B2746),
borderRadius: BorderRadius.circular(15),
),
child: Container(
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 18),
child: Text(
'Glossary',
style: TextStyle(
fontWeight: FontWeight.w800,
fontSize: 20,
),
),
)
],
),
),
),
Padding(
padding: const EdgeInsets.only(top: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Card(
margin: const EdgeInsets.only(left: 0),
child: Padding(
padding: const EdgeInsets.all(12),
child: Icon(Icons.arrow_back_ios),
),
elevation: 6,
),
],
),
)
],
),
),
);
}
}
and the result is the following:
For your second example, I had to add extra logic:
class DetailScreen extends StatefulWidget {
#override
_DetailScreenState createState() => _DetailScreenState();
}
class _DetailScreenState extends State<DetailScreen> {
#override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
child: SafeArea(
child: Stack(
fit: StackFit.expand,
children: [
Container(
margin: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: Color(0xFF0B2746),
borderRadius: BorderRadius.circular(15),
),
child: Column(
children: [
Container(
height: 64.0,
width: double.infinity,
margin: const EdgeInsets.only(left: 54, right: 8, top: 8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
),
child: Row(
children: <Widget>[
SizedBox(width: 20),
Icon(Icons.train, size: 35),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 18),
child: Text(
'Metro',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.w800,
fontSize: 20,
),
),
),
),
Icon(Icons.arrow_drop_down, size: 35),
SizedBox(width: 20),
],
),
),
Expanded(
child: Container(
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
)),
)
],
),
),
Padding(
padding: const EdgeInsets.only(top: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Card(
margin: const EdgeInsets.only(left: 0),
child: Padding(
padding: const EdgeInsets.all(12),
child: Icon(Icons.arrow_back_ios),
),
elevation: 6,
),
],
),
)
],
),
),
);
}
}
and the result for this one is the follwing:

Wrap text in container without using a fixed width in Flutter

I'm trying to create a basic chat application in Flutter and I want to display the conversation in simple containers which will adapt its length to the text inside. Everything works fine until the text does not fit the length of the container, when I get an overflow error.
The code that I'm using is this one
Widget _buildMessage(Message message) {
return Row(children: <Widget>[
message.author == username ? Expanded(child: Container()) : Container(),
Container(
padding: EdgeInsets.all(8.0),
margin: EdgeInsets.all(4.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: Row(
children: <Widget>[
Text(
message.text,
),
SizedBox(
width: 8.0,
),
Padding(
padding: EdgeInsets.only(top: 6.0),
child: Text(
message.time,
style: TextStyle(fontSize: 10.0, color: Colors.grey),
),
),
SizedBox(
width: 8.0,
),
Padding(
padding: EdgeInsets.only(top: 6.0),
child: Text(
message.author,
style: TextStyle(fontSize: 10.0, color: Colors.grey),
),
),
],
)),
message.author != username ? Expanded(child: Container()) : Container(),
]);
}
I'm using a Row within a Row so that I can get this alignment to the right or to the left depending on the author.
If I type something with a multiline in my input, the text is rendered properly, with the container expanding vertically as needed. The problem is when I just type beyond the width of the container.
I can fix this by wrapping the Text with a Container and a fixed with, but I don't want that as I want the width to be dynamic and adapt to the text.
I've seen other questions where people suggests using Flexible or Expanded, but I can't figure out how to do it.
Any ideas would be appreciated.
I tried to edit your code and here is what I've made:
return Row(
mainAxisAlignment: message.author == username ? MainAxisAlignment.end : MainAxisAlignment.start,
//this will determine if the message should be displayed left or right
children: [
Flexible(
//Wrapping the container with flexible widget
child: Container(
padding: EdgeInsets.all(8.0),
margin: EdgeInsets.all(4.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
//We only want to wrap the text message with flexible widget
child: Container(
child: Text(
message.text,
)
)
),
SizedBox(
width: 8.0,
),
Padding(
padding: EdgeInsets.only(top: 6.0),
child: Text(
message.time,
style: TextStyle(fontSize: 10.0, color: Colors.grey),
),
),
SizedBox(
width: 8.0,
),
Padding(
padding: EdgeInsets.only(top: 6.0),
child: Text(
message.author,
style: TextStyle(fontSize: 10.0, color: Colors.grey),
),
),
],
)),
)
]
);
Here is the full version of my dummy code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<String> username = ['foo', 'bar', 'foo', 'bar', 'foo'];
List<String> messages = ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bb', 'cccccccccccccccccccccccccccccc', 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd', 'ee'];
List<String> time = ['131', '6454', '54564', '54546', '88888'];
List<String> author = ['Jesus', 'Joseph', 'Mary', 'John', 'Cardo'];
#override
Widget build(BuildContext context){
return Scaffold(
body: Container(
color: Colors.blue[200],
child: ListView.builder(
itemCount: 5,
itemBuilder: (_, int index){
return Row(
mainAxisAlignment: username[index] == "foo" ? MainAxisAlignment.end : MainAxisAlignment.start,
children: [
Flexible(
child: Container(
padding: EdgeInsets.all(8.0),
margin: EdgeInsets.all(4.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
child: Container(
child: Text(
messages[index],
),
)
),
SizedBox(
width: 8.0,
),
Padding(
padding: EdgeInsets.only(top: 6.0),
child: Text(
time[index],
style: TextStyle(fontSize: 10.0, color: Colors.grey),
),
),
SizedBox(
width: 8.0,
),
Padding(
padding: EdgeInsets.only(top: 6.0),
child: Text(
author[index],
style: TextStyle(fontSize: 10.0, color: Colors.grey),
),
),
],
)),
)
]
);
}
)
)
);
}
}