Using Align inside of ListView to align Widgets left/right - android

My application shows profile of users. A feature I wish to add is to allow "like"ing of profiles by users. For this, I am using a Listview to show all profile content.
The problem is when adding a LikeButton, it gets centered, which is expected. I want to move it to the (top) right.
Align appears like a more viable and elegant solution that doing something like this (see below) but I cannot get Align it to work (it remains centered).
ListView(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [LikeButton()],
),
// rest of profile information
Using a Container with width: double.infinity doesn't work with Align either - it remains centered.
Container(
width: double.infinity,
child: Align(
alignment: Alignment.topRight,
child: LikeButton(),
),
)
Is using a Row my only option or is there a better way?

Since LikeButton is a Row or Column it self, You can't Align it like that,
You can use its properties to align it
ListView(
children: [
LikeButton(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
)
],
)
or just wrap it inside a SizedBox and align that box
Container(
width: double.infinity,
height: 500,
alignment: Alignment.topRight,
child: SizedBox(
width: 100,
height: 100,
child: LikeButton()),
)
Row takes the size of its children so your first code won't align it vertically
You didn't give height in you're Container code so Aligning it by topRight would be meaningless

You can Stack the LikeButton() onto Container(child:Row()) and position it to top right by giving values to topRight parameters.

Related

How to make a background gradient fill the screen in a SingleChildScrollView? (but not ConstrainedBox)

What is the best way to have a background gradient fill the entire screen, all while being in a SingleChildScrollView? I would like the screen to scroll only when necessary (when the items and bottom text overflow the screen). Note the Back button is a FAB.
Note: I have tried ConstrainedBox (but the scroll is weird when there's no overflow in the listview)
What's the best way to 1) fill the background completely, and 2) have scrolling activity only activate when the listview overflows?
My code:
Widget body() {
return SingleChildScrollView(
child: Container(
decoration: background(),
child: Column(children: [
listViewReminderBuilder(),
SizedBox(height: 10),
Container(
padding: EdgeInsets.all(5),
child: bottomText()),
SizedBox(height: 10),
])));
}
BoxDecoration Background() {
return BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
topColor,
bottomColor,
],
));
}
This is the result of the above code is shown below. The scroll works as I'd like (only scrolls when there's an overflow of listview items), but obviously the background doesn't fill the screen.
Many solutions online led me to the ConstrainedBox. My code and result is as follows:
Widget body() {
_screenHeight = MediaQuery.of(context).size.height;
return SingleChildScrollView(
child: Container(
decoration: background(),
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: _screenHeight),
child: Column(children: [
listViewReminderBuilder(),
SizedBox(height: 10),
Container(
padding: EdgeInsets.all(5),
child: bottomText()),
SizedBox(height: 10),
]))));
}
// background() was the same
The result. In this case, the background nicely fills the screen, but the screen is always scrollable and looks weird if the listview items aren't overflowing.
What's the best way to 1) fill the background completely, and 2) have scrolling activity only activate when the listview overflows?
first of all: wraping ListView with SingleChildScrollView is not good practice.
workaround:
use container only for background. by setting width infinity to fill entire screen.
no need to wrap with SingleChildScrollView if you have a ListView. just use Expanded widget.
Widget body() {
return Container(
width: double.infinity,
// here you can set height if you want.
// but since we use Column,by default it will expand the maximum height.
decoration: background(),
child: Column(children: [
// using Listview will make you widget not overflowing.
// wrap with expanded to make it fill available screen.
Expanded(child: listViewReminderBuilder(),),
SizedBox(height: 10),
Container(padding: EdgeInsets.all(5), child: Text('Back')),
SizedBox(height: 10),
]));
}
i just tried on dartpad. and it works fine.

How do I prevent the background image from shrinking when a value is entered from the phone keyboard in Flutter?

View before the keyboard is seen
The view after the keyboard is seen
I want the background image to always stay at the size before the keyboard is seen.
return Scaffold(
body: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/back.png'),
fit: BoxFit.cover,
),
),
alignment: Alignment.center,
padding: const EdgeInsets.fromLTRB(50, 100, 70, 0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Center(
child: TextFormWidget(form: name, hint: 'Name',),
),
SizedBox(
height: 15,
),
TextFormWidget(form: surname, hint: 'Surname'),
],
),
),
);
resizeToAvoidBottomInset on scaffold
If true the [body] and the scaffold's floating widgets should size themselves to avoid the onscreen keyboard whose height is defined by the ambient [MediaQuery]'s [MediaQueryData.viewInsets] bottom property.
For example, if there is an onscreen keyboard displayed above the scaffold, the body can be resized to avoid overlapping the keyboard, which prevents widgets inside the body from being obscured by the keyboard.
Defaults to true.
Also you might also like to wrap Scaffold with SafeArea to get view after statusBar.

Flutter: How to prevent an container overflowing inside a padding

The Problem
I have a card that contains a row of two elements, the second one being a column of two texts. One of the texts can be long, so the column-element itself can overflow.
Coming from CSS, a "width: 100%" would usually be enough here.
What I already tried
I learned you could wrap the column with something like Expanded or Flexible, but it leads the text to become invisible (and the overflow still existing).
I am not sure where exactly I have to put the Flexible/Expanded.
I looked into similar questions and there was always an "Expanded", but I could not apply any of said solutions to my layout so far.
Question
What is the cleanest way to get the outlined box to be only as wide as the padding should allow?
Code
#override
Widget build(BuildContext context) {
return (Card(
child: InkWell(
onTap: () {
print("tab" + name);
},
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(25),
child: SizedBox(
width: 50,
height: 50,
child: Image(
image: NetworkImage(imageUrl),
),
),
),
Padding(
padding: const EdgeInsets.all(16),
child: Container(
decoration: BoxDecoration(
border: Border.all(width: 1, color: Colors.purpleAccent),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontWeight: FontWeight.bold),
textAlign: TextAlign.left,
),
Text(
description,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.left,
),
],
),
),
),
],
),
),
),
));
}
You can surround the Padding widget inside your Row with an Expanded widget - The child of Expanded will then size itself to the space available.
For some additional context, Rows and Columns are both Flex widgets. Flex widgets layout their children along an axis, and the size of this axis is unbounded by default. Even though the widget "knows" how much space it has available on the display, it doesn't pass that information along to its children, so the children are free to take whatever size they want, which means they can potentially overflow the container.
An Expanded widget can only be placed as a direct child of a Flex widget, and it automatically takes up a given proportion (given by the flex property) of the space available to it (by default that will simply be all of the space which is not taken up by other widgets in the children list).
So essentially, Expanded will take up as much space as the parent widget has available, and then will constrain it's child to be no larger than that (along whichever axis pertains).
The link above to the Flex documentation has more info.

Gesture Detector and SingleChildScrollView Flutter

I was trying to create a scroll view and add a gesture detector to find the exact location of the screen.
The gesture Detection shows location of the blue Container which is correct because its the child, but it shows the pixel position relative to the phone. I want to see the pixel position relative to the full screen(body). That means the blue Container starts after (10,000 pixels). Any help would be appreciated.
My code:
body: SingleChildScrollView(
child: Column(
children: [
Container(
color: Colors.redAccent[200],
height: 1000.0,
width: 500.0,
alignment: Alignment.center,
),
GestureDetector(
onVerticalDragStart: (DragStartDetails details){
print("Start");
print(details);
},
),
Container(
color: Colors.blueAccent[200],
height: 1000.0,
width: 500.0,
alignment: Alignment.center,
),
],
),
),
Also Why is the SingleChildScrollViewNot working on the blue side?
Image of Screen
Image of output of Gesture Detector

how to achieve LinearLayout like layout in flutter?

I am new to flutter how to achieve Linear Layout like layout in flutter ? layout view's horizontally and vertically.
Row is the horizontal Linear Layout
new Row(
children: <Widget>[
///display children in a horizontal manner
],
And Column is the vertical Linear Layout
new Column (
children: <Widget>[
///display children in a vertical manner
],
First you should try to understand Linear layout in flutter link is given below.
https://flutter.io/flutter-for-android/#what-is-the-equivalent-of-a-linearlayout
After that you will be able to achieve it.
To achieve pure linear layout in flutter , use row widget while managing the main axis and cross axis alignment
Example
Lets say you want to show two icons horizontally with equal weight ie 1-1 each
Use row like this
Container(
color: Colors.yellowAccent,
child: new Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
new Icon(
Icons.access_time,
size: 50.0,
),
new Icon(
Icons.pie_chart,
size: 100.0,
),
],
),
)

Categories

Resources