Layout Rows and Columns to Expand as a percentage of overall screen width - flutter-layout

I am new to Flutter and am trying to create a row/column layout that has container areas that I can then populate with specialized widgets but I am struggling to get the appropriate layout.
I have manually hard-coded widths into the columns and containers but I would like to divide the layout into 1/3 2/3 with some padding around the columns
body: Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(bottom:8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(height: 20.0, width:100, color: Colors.yellow),
Container(height: 220.0, width:100,color: Colors.green),
],
),
),
Container(height: 200.0,width:100, color: Colors.cyan),
]),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(width:600,color: Colors.amber),
),
]),
This produces a layout but it's hard coded, I would like to use a percentage of the current device's screen width.

Row and column take the size of their parent if there ain't one then they take the size of the content.
For this you can put a Container in front of row and specify the size of it.
Somewhat Like this
Container(width: MediaQuery.of(context).size.width * 0.75,
child:Row(
0.75 is for 75%

Related

Row match parent width

I'm trying to make the Row containing "Short label" wide as the Row below it.
Putting mainAxisSize: MainAxisSize.max isn't enought, since it should match parent width, based on this answer:
The equivalent of wrap_content and match_parent in flutter?
Tried using Expanded outside row, but it causes an error, same using SizedBox.expand.
Can't use constraints like width: double.infinity because the row on the left (// Left part in code) must take only the space it needs, the rest must be taken from the yellow container.
Screenshot and code below:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Column(
children: [
Row(
children: [
// Left part
Row(
children: [
Column(children: [
// Error using Expanded ...
//Expanded(
//child:
Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: [
Text('Short label:'),
Container(width: 20, height: 20, color: Colors.red),
],
),
//),
Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: [
Text('Long text label:'),
Container(width: 20, height: 20, color: Colors.red),
],
),
]),
],
),
Expanded(
child: Container(
color: Colors.yellow,
height: 100,
),
),
],
),
],
),
),
),
);
}
EDIT (old, check final edit below)
IntrinsicWidth was the correct way to give the rows the same width, as #mohandes suggests.
But it does not work if i add another row below (the one containing the checkbox in the image below), wrapping the rows inside two IntrinsicWidth widgets.
The checkbox row should be wide as the upper one in order to align the checkbox on the left.
In the first row i added an orange container on the right as a placeholder for a similar block.
Screenshot and code below:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Column(
children: [
Row(children: [
Column(
children: [
IntrinsicWidth(
child: Row(children: [
IntrinsicWidth(
child: Column(children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text('Short label 1:'),
Container(width: 20, height: 20, color: Colors.red),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text('Short label 123456:'),
Container(width: 20, height: 20, color: Colors.red),
],
),
]),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 40,
height: 40,
color: Colors.orange,
),
)
]),
),
IntrinsicWidth(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
Checkbox(value: true, onChanged: null),
Text('Checkbox'),
],
),
),
],
),
Expanded(
child: Container(
color: Colors.yellow,
height: 100,
),
),
]),
],
),
),
),
);
}
EDIT 2
IntrinsicWidth works fine, the problem with the code above in the first Edit is that i used IntrinsicWidth as wrapper for child widgets (the rows) instead of parent widget (the outer column).
Try adding the column that contains rows (long and short) in intrinsic width widget .
Intrinsic width make all children width as longest child width.
By changing the layout to Row [ Column Column Container ] we get
Code:
Widget alternateLayout() {
return Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
color: Colors.blue[100],
child: Text('Short label:'),
),
Container(
color: Colors.green[100],
child: Text('Long text label:'),
),
],
),
Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 20,
width: 20,
color: Colors.red,
),
Container(
height: 20,
width: 20,
color: Colors.red,
),
],
),
Expanded(
child: Container(
color: Colors.yellow,
height: 100,
),
),
],
);
}

Flutter: How can I put "header image" and "ListView" in same scroll?

I want to make header image and ListView in same scroll. But the code like below doesn't work:
SingleChildScrollView(
child: Column(
children: <Widget>[
HeaderImage(),
ListView(...)
]
)
)
I know ListView can't be below Column() directly. I know the way to use Expand() or Container with height or SizedBox, but these way can't make header image and ListView in same scroll. How to do these in same scroll? (HeaderImage widget is example.)(I must use ListView.)
I used to see this style in the world of web frequently.
You can use:
CustomScrollView(
slivers: <Widget>[
SliverAppBar(
flexibleSpace: FlexibleSpaceBar(
background: Container(
color: Colors.transparent,
child: Image(
image: NetworkImage('your image url here'),
fit: BoxFit.cover,
),
)
),
expandedHeight: 300,
backgroundColor: Colors.transparent,
actionsIconTheme: IconThemeData.fallback(),
),
SliverList(
delegate: SliverChildListDelegate(
[
Column(
children: <Widget>[
for(var index = 0;index<your_data.length;index++) // Dont put comma
your List Design here
]
)
]
)
)
]
)

Flutter: Text clipping not working when in a Row

I have a situation where there are two text items in a row. The first text can have a variable length, but I don't want to wrap the texts in two lines. So I need the first text to be clipped when the parent does not have enough width for it and the second text. Now, if I do not place the texts in a row, it works as expected. But if they are in a Row, the clipping does not occur. I can not wrap the first text in an Expanded widget (which fixes the clipping problem) because it adds a blank space between the two texts.
Here is a code snippet
Container(
child: Row (
children: <Widget>[
Text("Long text that needs to be clipped",
overflow: TextOverflow.clip,
textAlign: TextAlign.left),
//there should be no blank space between the elements
Text( "+1",
textAlign: TextAlign.left,
overflow: TextOverflow.fade),
],
)
)
Been scratching my head for a while...
We can use Expanded as per below:
Material(
child: Container(
color: appColor,
child: Row(
children: <Widget>[
Expanded(
child: Align(
alignment: AlignmentDirectional.centerStart,
child: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Text(
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
softWrap: false,
overflow: TextOverflow.fade,
style: TextStyle(color: Colors.white),
),
),
),
),
IconButton(
icon: Image.asset('assets/images/filter.png'),
onPressed: () {},
),
IconButton(
icon: Image.asset('assets/images/notification.png'),
onPressed: () {},
),
],
)),
);

Put fixed widget above TabBarView

I want to build a UI where I have some fixed widgets (visible on all tabs) on top of my screen and then below them I want a TabBarView (with the tab bar at the bottom), is this possible without making your own tab widget or does the TabBarView have to take the whole screen?
Think I found the answer here:
https://camposha.info/course/flutter-widgets/lesson/flutter-nested-tabbar/
This is how I did it:
return Scaffold(
body: Stack(
children: [
Container(
alignment: Alignment.topCenter,
child: Image.asset('assets/images/logo.png',
fit: BoxFit.fitWidth, width: Get.width / 2.5)),
Align(
alignment: Alignment.bottomCenter,
child: DraggableScrollableSheet(
initialChildSize: 0.7,
minChildSize: 0.7,
builder:
(BuildContext context, ScrollController scrollController) =>
SingleChildScrollView(
controller: scrollController,
child: DefaultTabController(
initialIndex: 0,
length: 2,
child: Column(
children: [
TabBar(tabs: <Widget>[
Tab(child: Text('Text1')),
Tab(child: Text('Text2'))
]),
Container(
height: MediaQuery.of(context).size.height,
child: TabBarView(
children: <Widget>[Widget1(), Widget2()],
),
)
],
)),
),
),
),
],
));

How to remove the gap above and below the text on flutter

I am trying to put the text "Hello" right below the "123", but the bigger the text is, the bigger the gap. How do I remove the gap??? Flutter images are added below.
The only way I could find so far is to reduce height property, the problem though is that it reduces the gap above only. So in your case, you could try to set it for hello text to the minimum:
Text(
'123',
style: TextStyle(fontSize: 60.0),
),
Text(
'hello',
style: TextStyle(fontSize: 10.0, height: 0.1),
),
Use Stack widget to align your text widgets
Stack(
children: <Widget>[
Text(
'123',
style: TextStyle(fontSize: 60.0),
),
Positioned(
child: Text('Hello'),
bottom: 0.0,
left: 35.0,
)
],
),
Hope it helps!

Resources