如何在Flutter中向ReorderableListView添加页脚

时间:2019-06-29 07:55:55

标签: flutter flutter-layout flutter-dependencies flutter-test

尝试制作一个包含页眉和页脚以及可重排内容项的ui。有一个名为 header 的属性,我们可以从中添加标题项。但是,如果我也想添加 footer 项目,该怎么办。

import 'package:flutter/material.dart';

class MyStickyHeader extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyStickyHeaderState();
  }
}

class _MyStickyHeaderState extends State<MyStickyHeader> {
  List<Widget> _list = [
    Text("Apple"),
    Text("Ball"),
    Text("Cat"),
    Text("Dog"),
    Text("Elephant")
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 30, left: 10),
      color: Colors.white,
      child: showData(),
    );
  }

  Widget showData() {
    return Container(
      child: ReorderableListView(
        header: Container(
          height: 100,
          color: Colors.red,
        ),
        children: _list
            .map((item) => Container(
                  padding: EdgeInsets.all(10),
                  key: Key("${(item as Text).data}"),
                  child: Row(
                    children: <Widget>[
                      Icon(Icons.ac_unit),
                      Expanded(
                        child: item,
                      )
                    ],
                  ),
                ))
            .toList(),
        onReorder: (int start, int current) {
          // dragging from top to bottom
          if (start < current) {
            int end = current - 1;
            Widget startItem = _list[start];
            int i = 0;
            int local = start;
            do {
              _list[local] = _list[++local];
              i++;
            } while (i < end - start);
            _list[end] = startItem;
          }

          // dragging from bottom to top
          if (start > current) {
            Widget startItem = _list[start];
            for (int i = start; i > current; i--) {
              _list[i] = _list[i - 1];
            }
            _list[current] = startItem;
          }
          setState(() {});
        },
      ),
    );
  }
}

3 个答案:

答案 0 :(得分:1)

列表的最后一个元素可以是页脚。它必须是具有onLongPress属性的小部件。例如:

ReorderableListView(
      onReorder: (int oldIndex, int newIndex) {},
      children: List.generate(someList.items.length + 1, (index) {
        if (index < someList.items.length)
          return ListTile(
            key: Key(someList.items[index].description),
          );
        else
          return RaisedButton(key: Key('footer'), onPressed: () {}, onLongPress: (){}, child: Text('Button'));
      })),

答案 1 :(得分:0)

如果用ReorderableListViewColumn小部件包装Expanded,则可以在底部添加一个Container作为页脚:

Column(
  children: <Widget>[
    Expanded(
      child: ReorderableListView(
        header: Container(
          height: 100,
          color: Colors.red,
        ),
        children: _list
          .map((item) => Container(
          padding: EdgeInsets.all(10),
          key: Key("${(item as Text).data}"),
          child: Row(
            children: <Widget>[
              Icon(Icons.ac_unit),
              Expanded(
                child: item,
              )
            ],
          ),
        )).toList(),
        onReorder: (int start, int current) {
          // dragging from top to bottom
          if (start < current) {
            int end = current - 1;
            Widget startItem = _list[start];
            int i = 0;
            int local = start;
            do {
              _list[local] = _list[++local];
              i++;
            } while (i < end - start);
            _list[end] = startItem;
          }

          // dragging from bottom to top
          if (start > current) {
            Widget startItem = _list[start];
            for (int i = start; i > current; i--) {
              _list[i] = _list[i - 1];
            }
            _list[current] = startItem;
          }
          setState(() {});
        },
      ),
    ),
    Container(
      height: 40,
      alignment: Alignment.center,
      child: Text('Footer'),
      color: Colors.orange,
    ),
  ],
),

答案 2 :(得分:0)

要实现这种视图,我建议使用 Slivers

好处

  1. 粘性页眉/页脚。
  2. 无限正文/内容滚动。

检查下面的代码

import 'package:flutter/material.dart';

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverList(
            delegate: SliverChildListDelegate(
              [
                Container(
                  width: double.infinity,
                  height: 50,
                  color: Colors.orangeAccent,
                  child: Center(
                    child: Text(
                      'Header',
                      style: TextStyle(color: Colors.white, letterSpacing:4),
                    ),
                  ),
                ),
                ListView.builder(
                  shrinkWrap: true,
                  itemCount: 100,
                  itemBuilder: (BuildContext context, int index) {
                    return ListTile(
                      title: Center(child: Text('$index')),
                    );
                  },
                ),
              ],
            ),
          ),
          SliverFillRemaining(
            hasScrollBody: false,
            child: Align(
              alignment: Alignment.bottomCenter,
              child: Container(
                width: double.infinity,
                height: 50,
                color: Colors.blueAccent,
                child: Center(
                  child: Text(
                    'Footer',
                    style: TextStyle(color: Colors.white, letterSpacing: 4),
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

enter image description here

enter image description here

enter image description here

enter image description here

更多细节请看这里:

https://itnext.io/create-a-header-footer-with-scrollable-body-view-in-flutter-5551087270de