contents of SingleChildScrollView isn't becoming full height - flutter

I have a SingleChildScrollView > Column > [A bunch of widgets, Text (really long text)]
I'm trying to make the Text go to the bottom of the screen so I tried 2 things and they both gave me errors
I tried adding a Spacer before the Text, but I got a RenderFlex error. I then tried wrapping all the widgets in a second column and leaving the Text widget in the 1st/original column. I also got an error with that.
How can I make the Text Widget go all the way to the bottom of the screen?

In SingleChildScrollView you can't use Spacer or Expanded widget. You should give height to your widget. I offer you try Stack widget like code below:
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack(
children: [
SingleChildScrollView(
child: Column(
children: [
Container(
height: 100,
margin: EdgeInsets.all(10),
color: Colors.amber,
),
Container(
height: 100,
margin: EdgeInsets.all(10),
color: Colors.amber,
),
Container(
height: 100,
margin: EdgeInsets.all(10),
color: Colors.amber,
),
Container(
height: 100,
margin: EdgeInsets.all(10),
color: Colors.amber,
),
],
),
),
Align(
alignment: Alignment.bottomCenter,
child: Text('test'),
)
],
),
));
}

Related

How to center one element and place the second element next to it

In my layout I have two Widgets in a row, a text and a button.
How can I achieve something like below, where only the Text is centered, and the icon is simply next to it?
---------------------------
Text *
---------------------------
Using Row would center all the contents and would output something like
---------------------------
Text *
---------------------------
Tried: Row(children:[widget1, widget2], mainAxisAlignment: MainAxisAlignment.center);
But this centers both items, causing the text to look off-centered.
You can use CompositedTransformTarget and CompositedTransformFollower as mentioned on comment section by pskink.
class AppPT extends StatelessWidget {
const AppPT({super.key});
#override
Widget build(BuildContext context) {
final LayerLink _layerLink = LayerLink();
return Scaffold(
body: Column(
children: [
Stack(
children: [
Align(
child: CompositedTransformTarget(
link: _layerLink,
child: Container(
color: Colors.red,
width: 100,
height: 60,
alignment: Alignment.center,
child: Text("btn"),
),
),
),
CompositedTransformFollower(
link: _layerLink,
targetAnchor: Alignment.centerRight,
followerAnchor: Alignment.centerLeft,
child: Container(
color: Colors.cyanAccent,
width: 12,
height: 12,
alignment: Alignment.center,
child: Text("*"),
),
),
],
)
],
),
);
}
}
There are few tricks I can think of,
You can use Stack widget with Position.
including another widget on right by applying opacity(invisible) on current snippet.
using transform will be handle if you know the width of the widget.
Transform.translate(
offset: Offset(20 / 2, 0), //20 is the * box size
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
color: Colors.red,
width: 100,
height: 60,
alignment: Alignment.center,
child: Text("btn"),
),
Container(
color: Colors.green,
width: 20,
height: 20,
child: Center(child: Text("*")),
),
],
),
),
Place text and second widget inside row then put the row inside container with alignment center and use SizedBox for spacing between widget instead of Padding Widget.
Container(
color: Colors.green,
alignment: Alignment.center,
height: 100,
child: Row(
mainAxisSize: MainAxisSize.min,
children: const [
Text("Text"),
SizedBox(width: 10),
Text("*"),
],
),
);

Using Align widget in the Stack widget without it expanding

I'm trying to create a custom widget and it makes use of the Stack widget. When I wrap the Stack's children using the Positined widget, the stack hugs the children that are not positioned. When I try to use the Align widget for more precise placement the stack expands to fill all the available space.
I'm trying to avoid using any widget with a fixed size, i.e. Container or SizedBox, as the parent for the Stack widget.
Is there a way to prevent the stack from filling all the available space or work around this that archives the same result?
I'm trying to position a child to the top center of the Stack as an example and this is where I am right now:
Using the Positined widget (I have to try multiple time to get the center position and sometimes its still off):
Positioned(
top: 0,
left: 80,
child: child,
);
Using the Align widget:
Align(
alignment: Alignment.topCenter,
child: child,
);
This should be the result using the Align widget:
Align(
alignment: Alignment.topCenter,
child: child,
);
You can set Stack's alignment to topCenter, Try this:
Stack(
alignment: Alignment.topCenter, // <=== add this
children: [
Container(
color: Colors.grey,
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisSize: MainAxisSize.min, // <=== add this
children: [
Container(
color: Colors.amber,
height: 30,
width: 30,
),
Container(
color: Colors.green,
height: 30,
width: 30,
),
Container(
color: Colors.blue,
height: 30,
width: 30,
),
Container(
color: Colors.yellow,
height: 30,
width: 30,
),
],
),
),
Container(
color: Colors.red,
height: 30,
width: 30,
)
],
)
Note: make sure you set Row's mainAxisSize to min.

Stack Widget Not Scrolling in SCSV, Flutter

I'm using a Stack to Positioned one widget on top of an Image.asset widget, but the problem I have is that my whole Stack widget is not scrollable to see the whole content. I wrapped the Stack widget with a SingleChildScrollView but the problem still persists. If I wrap the Stack widget with a Containter and give the height: MediaQuery.of(context).size.height, it's scrollable but I can't still see the whole content of the page. Where is my mistake, that's what I'm wondering? With what widget should I wrap Stack or is there a better way for my problem? In short, I'm stacking a Column on top of an Image and I need the whole Stack widget to be scrollable so I can see the whole content of the Stack widget. Here is the code:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body: SafeArea(
child: SingleChildScrollView(
child: Stack(
overflow: Overflow.visible,
children: [
Image.asset(
'lib/modules/common/images/logo.png',
width: double.infinity,
height: 196.0,
),
Positioned(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
top: 136.0,
child: Column(
children: [
Container(), // some content inside the container
Container(), // // some content inside the container
],
),
),
],
),
),
),
);
}
Thanks in advance for the help!
if you mean to have a image on back and scrollable content at fron check this answer, if not let me know and share a prototype/image that you want.
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body: SafeArea(
child: SingleChildScrollView(
child: SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 3,
child: Stack(
fit: StackFit.loose,
children: [
Align(
alignment: Alignment.topCenter,
child: Image.asset(
'assets/image.jpg',
width: double.infinity,
height: 196.0,
),
),
Positioned(
top: 136.0,
child: SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Column(
mainAxisSize: MainAxisSize.min,
children: List.generate(
2,
(index) => Container(
height: 100,
width: double.infinity,
color: index % 2 == 0
? Colors.amberAccent
: Colors.cyanAccent,
), // some content inside the container
// some content inside the container
),
),
),
),
],
),
),
),
),
);
}

how make scrollable all mobile page in flutter?

Do you know how make scrollable all mobile page in flutter ? Because if we display some widgets it générâtes an error if it is more of the height of the mobile screen
This would work perfectly. Check it out.
ListView(
children: <Widget>[
// first widget here
Container(
height: 200,
width: MediaQuery.of(context).size.width,
color: Colors.blue,
),
// third widget here
Container(
height: 200,
width: MediaQuery.of(context).size.width,
color: Colors.black,
),
// second widget here
Container(
height: 200,
width: MediaQuery.of(context).size.width,
color: Colors.red,
),
],
),
You can use a ListView
Widget view(){
return ListView(
children: <Widget[
Text("your widgets here"),
SizedBox(height: 20), // you can use these for spacing
]
)
}
Or a SingleChildScrollView
Widget view(){
return SingleChildScrollView(
children: <Widget[
Text("your widgets here"),
SizedBox(height: 20), // you can use these for spacing
]
)
}

Flutter Align widget does not align

I'm building a ListItem and I want the text to be in a cardview that takes up the entire width and just enough height as the text size is. This is my code
#override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Align(
child: Container(
width: double.infinity,
height: 200.0,
color: Colors.pinkAccent,
),
),
Container(
width: double.infinity,
color: Colors.deepPurpleAccent,
child: Align(
alignment: Alignment.bottomCenter,
child: Card(
child: Text(
'some text',
textAlign: TextAlign.center,
),
)),
)
],
);
}
There are two widgets inside the stack, I want the second widget to lap above the first widget but just for a small part. Yet as you can see, the align of the second widget isn't at the bottom but at the top. I also tried switching Container and Align as child of each other but to no avail. Here's a screenshot of the resulting code
There is nothing like "BottomCenter" (with capital B). You should use Alignment.bottomCenter. But you probably need to set alignment for Stack.
Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Container(
width: double.infinity,
height: 200.0,
color: Colors.pinkAccent,
),
Container(
width: double.infinity,
color: Colors.deepPurpleAccent,
child: Card(
child: Text(
'some text',
textAlign: TextAlign.center,
),
),
)
],
)