Centering layout in Flutter - flutter

If I use the following structure in the Scaffold:
body: Container(
alignment: Alignment.center,
child:
Text('Test')
)
Then the text is vertically and centrally structured, as I want. But if I do the following:
body: Container(
alignment: Alignment.center,
child:
ListView(
children:people.map(person).toList(),
),
)
Then the ListView is stuck to the top left of the body and isn't centered.
Why is that, and how can I center the ListView the same way that Text was centered?

Vertical
body: Container(
alignment: Alignment.center,
child:
ListView(
shrinkWrap: true,
children:people.map(person).toList(),
),
)
Horizontal
ListView(
shrinkWrap: true,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('text'),
],
),
Text('text'),
Text('text'),
],
),

It's because ListView take all the space available. To tell Listview to take only children sizes, add property "shrinkWrap" to true.
Center(
child: ListView(
shrinkWrap: true,
children: <Widget>[
Center(
child: Text("Hello world !"),
),
],
),
)

Related

flutter scrollview and SingleChildScrollView throws bottom overflow and is not scrolling

The widgets inside the child scroll view doesn't scroll and throws bottom overflow exception on the screen when there are more widgets in that list view. I am not sure where I am doing a mistake.
home: Scaffold(
body: Column(
children: [
APPbar(),
Container(
color: Colors.grey[200],
child: Expanded(
child: Column(
children: [
searchBar(),
Row(children: casewidgets),
],
)
),
)
SingleChildScrollView(
child:Column(
children: [
Text("a new column"),
Text("a new column"),
Text("a new column"),
Text("a new column"),
],
)
),
]
)),
You can use Expanded on SingleChildScollView to get aviable height, and remove column's Expanded.
home: Scaffold(
body: Column(children: [
// APPbar(),
Container(
color: Colors.grey[200],
child: Column(
children: [
// searchBar(),
Row(children: []),
],
),
),
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
for (int i = 0; i < 33; i++) Text("a new column"),
],
)),
),
])),
You cannot use expand when using a singlechildscrollview in appropriate way. First you need to understand that expand covers the maximum height and width on the screen or the specific section in which you use expand.
Instead of using SingleChildScrollView, It's easier to use CustomScrollView with a SliverFillRemaining. and it's works work's great.
CustomScrollView(
slivers: [
SliverFillRemaining(
hasScrollBody: false,
child: Column(
children: <Widget>[
const Text('TextData'),
Expanded(
child: Container(
color: Colors.green,
child: Text("Your Text Data",),
),
),
const Text('Add Other Widgets'),
],
),
),
],
),

How to put a shadow above containers inside ListView.builder Flutter?

I have a ListView.builder where I want to show containers with shadows. And I want a shadow of one container to cover another container. Example of how it should looks.
It easily to do with Stack widget, but I have no idea how to do it inside ListView.builder.
Solution that works fine for me
Instead of using ListView I've decided to use CustomScrollView that takes slivers as parameters. Inside slivers I'm maping through a list of items and return my container wrapped with SliverToBoxAdapter. I don't know exactly how it works, but it works fine for me.
CustomScrollView(
slivers: [
...names.map(
(e) => SliverToBoxAdapter(
child: Center(
child: CustomContainer(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Container #${e['name']}'),
Text('Age is ${e['age']} y.o.')
],
),
),
),
),
),
),
],
),
// if scroll view overflow on other widget then use ClipRRect With Expanded.
ClipRRect(
child: Expanded(
child: CustomScrollView(
clipBehavior: Clip.none, //add this line inside CustomScrollView
slivers: [
...names.map(
(e) => SliverToBoxAdapter(
child: Center(
child: CustomContainer(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Container #${e['name']}'),
Text('Age is ${e['age']} y.o.')
],
),
),
),
),
),
),
],
),),),
I face the same problem and i use this tricks and it's solved.
You can use index of listview and give different elevation of card according to index.
ListView.builder(
itemCount:10,
shrinkWrap: true,
itemBuilder: (ctx, i) {
return Card(
elevation: (i%2 == 1)?10 :0,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
crossAxisAlignment:CrossAxisAlignment.center,
mainAxisAlignment:MainAxisAlignment.center,
children: [
Text("Container $i"),
],
),
),
);
}),

Interact with widget under an image in flutter/dart

I have a question, how do I make a image on top of a listview and still be able to scroll on the listview? Like scroll though the image, so the image is not moving.
When I stack them with 'stack widget' I still can't scroll, only in the parts of the scrollview that dosn't have an image on top of it.
Please help me, this is like a big part of my app that i've been developing for 6 weeks now, and if I can't make it work, my app is kinda done for it!
Here's an example:
Stack(
children: <Widget>[
ListView(
controller: scrollController,
children: <Widget>[
Container(
child: Text('widget 1'),
),
Container(
child: Text('widget 2'),
),
Container(
child: Text('widget 3'),
),
],
),
Image.asset('assets/myImage.png'),
],
),
You can wrap the image with an IgnorePointer widget:
Stack(
children: <Widget>[
ListView(
controller: scrollController,
children: <Widget>[
Container(
child: Text('widget 1'),
),
Container(
child: Text('widget 2'),
),
Container(
child: Text('widget 3'),
),
],
),
IgnorePointer(child: Image.asset('assets/myImage.png')),
],
),

How to create scrollable row in flutter

i tried creating scrollable row in flutter
but after trying multiple methods in some i am facing issue of height being fixed or list is not scrolling
posting one of the code i tried.
looking forward for a way where i can scroll in a row and don't need to fix a height for the widget.
new Column(
children: [
new Container(
height: 100.0,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
new Text("text 1"),
new Text("text 2"),
new Text("text 3"),
],
),
),
],
),
I have already answered somewhat related question where you don't need to give a fix height to the widgets
and your widgets can scroll in horizontal direction
you can check it here
https://stackoverflow.com/a/51937651/9236994
TIP: Use SingleChildScrollView widget
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: <Widget>[
Text('hi'),
Text('hi'),
Text('hi'),
]
)
)
Replace the Column widget by ListView.
You can take a look at
https://api.flutter.dev/flutter/widgets/ListView-class.html
Regards
Something like this ?
new Column(
children: [
new Container(
height: 100.0,
child: Expanded(
child: ListView.builder(
shrinkWrap: true,
itemBuilder: (context,int){
return Container(
child: Row(
children: <Widget>[
new Text("text 1"),
new Text("text 2"),
new Text("text 3"),
],
);
},
),
),
],
),
),
],
),

How to center content on screen and scroll if it overflows

I have as screen that is centered using:
SafeArea(
child: Column(
children: <Widget>[
Expanded(
child: Center(
child: ...,
),
),
],
),
)
But if the content is larger than the screen size, it will overflow. How can I keep the content centered, but at the same time start scrolling if it overflows?
You could use ListView widget and then put your content into it's children
Like so:
SafeArea(
child: ListView(
children: <Widget>[
Expanded(
children: <Widget>[
Center(
child: ...,
),
]
),
],
),
)
Replace your column with a ListView.
Following #Tembero's logic, I added a shrinkWraped ListView, removed the Expanded, and wrapped the ListView in a Center widget, so now it behaves exactly as expected:
SafeArea(
child: Center(
child: ListView(
shrinkWrap: true,
children: <Widget>[
Center(
child: ...
),
],
),
),
)