jdevp2
jdevp2

Reputation: 663

In Flutter, how to place a ListView at the bottom of the screen

I'm new to Flutter. How to keep a ListView at the bottom of the screen in Flutter ? I try to put 2 widgets inside a column and put Expanded around the first widget(Text) so it should takes up the remaining screen and the ListView at bottom. But nothing shows up. If I replace the ListView with a Text widget, it works. Not sure what will take to keep the ListView at the bottom.

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: Column(children: <Widget>[
            Expanded(
              child: Text('Here I\'m taking all the space.'),
            ),
            Align(
              alignment: Alignment.bottomCenter,
              child: MyWidget(),
              //child: Text("if use Text widget, it works"),
            ),
          ]),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(scrollDirection: Axis.horizontal, children: _getListIcon());
  }

  _getListIcon() {
    List<Widget> widgets = [];
    for (int i = 0; i < 10; i++) {
      widgets.add(RaisedButton(
          onPressed: () => {},
          color: Colors.orange,
          padding: EdgeInsets.all(10.0),
          child: Column(
            children: <Widget>[Icon(Icons.add), Text("Add")],
          )));
    }
    return widgets;
  }
}

Upvotes: 0

Views: 6661

Answers (2)

farouk osama
farouk osama

Reputation: 2519

I don't see any problems.

I suggest some improvements to the code

You do not need to use Align widget in Column

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: Column(children: <Widget>[
             Spacer(),
             MyWidget(),
          ]),
        ),
      ),
    );
  }
}

Just like that, and if you wanted to make the list a fixed height if it was empty use SizedBox like this:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: Column(children: <Widget>[
             Spacer(),
             SizedBox(height:50,child:MyWidget()),
          ]),
        ),
      ),
    );
  }
}

Upvotes: 1

Kevin Scheeren
Kevin Scheeren

Reputation: 41

instead of using the Alignment widget try changing the alignment on the axis of the column like this:

Column(
   mainAxisAlignment: MainAxisAlignment.end
   children: <Widget>[
      Expanded(
         child: Text('Here I\'m taking all the space.'),
      ),
      YourListView(),
    ],
),

this works for the cross axis as well. For reference you might want to take a look at the flutter docs for the Column and the MainAxisAlignment

I hope this solves your problem.

Upvotes: 3

Related Questions