gpt4 book ai didi

flutter - 如何实现位置: sticky and bottom 0 with Flutter?

转载 作者:行者123 更新时间:2023-12-05 03:01:23 24 4
gpt4 key购买 nike

我想构建一个带有粘性页脚的 ListView ,如 this article的“坚持到底?!”在 Flutter 中。

enter image description here

在 CSS 中,

.main-footer{     
position: sticky;
bottom: 0;
}

但是如何使用 Flutter 呢?

我想要什么

  1. 可滚动的大内容
  2. 页脚(粘性)
  3. 可滚动的大内容

1 和 2 首先可见(可滚动内容和固定页脚)。滚动到 1 的末尾后,页脚 (2) 变得不固定。其余内容 (3) 将显示在页脚 (2) 下方。

我试图用 CustomScrollView 实现上面的内容但页脚按钮未绘制在列表上方。

enter image description here

代码

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fixed footer',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: _FixedFooterDemo(),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
),
);
}
}

class _FixedFooterDemo extends StatelessWidget {
const _FixedFooterDemo({
Key key,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return CustomScrollView(
slivers: <Widget>[
SliverAppBar(
title: Text('Fixed footer'),
),
SliverList(
delegate: SliverChildListDelegate(List.generate(20, _buildListItem)),
),
SliverStickyFooter(
child: Center(
child: RaisedButton(
onPressed: () {},
child: Text('Fixed under list button'),
),
),
),
SliverFillRemaining(
child: Container(
color: Colors.yellow,
child: Center(
child: Text('below space'),
),
),
),
],
);
}

ListTile _buildListItem(int i) {
return ListTile(
title: Text(
'Item $i',
),
subtitle: Text(
'Sit est ipsum consequat sit ex. Minim magna laborum dolore aliqua sit dolore velit sint fugiat. Culpa officia tempor proident minim aliquip nisi reprehenderit ullamco duis mollit. Aute velit irure ut Lorem pariatur anim mollit cillum dolor irure quis. Eu officia dolore deserunt do est cupidatat duis elit. Pariatur magna reprehenderit aliquip ea irure Lorem sunt aute.',
maxLines: 2,
),
);
}
}

class SliverStickyFooter extends SingleChildRenderObjectWidget {
const SliverStickyFooter({
Key key,
Widget child,
}) : super(key: key, child: child);

@override
RenderSliverStickyFooter createRenderObject(BuildContext context) =>
RenderSliverStickyFooter();
}

class RenderSliverStickyFooter extends RenderSliverSingleBoxAdapter {
/// Creates a [RenderSliver] that wraps a [RenderBox] which is sized to fit
/// the remaining space in the viewport.
RenderSliverStickyFooter({
RenderBox child,
}) : super(child: child);

@override
void performLayout() {
child?.layout(
constraints.asBoxConstraints(),
parentUsesSize: true,
);

final paintedChildSize =
calculatePaintOffset(constraints, from: 0.0, to: child.size.height);
assert(paintedChildSize.isFinite);
assert(paintedChildSize >= 0.0);
geometry = SliverGeometry(
scrollExtent: child.size.height,
paintExtent: paintedChildSize,
maxPaintExtent: paintedChildSize,
hasVisualOverflow: true,
paintOrigin: -child.size.height + paintedChildSize,
visible: true,
);

if (child != null) {
setChildParentData(child, constraints, geometry);
}
}
}

最佳答案

尝试 Column 和三个 child ,Header 和 Footer 有特定的大小,ListView 占据剩余空间。

        Column(
children:[
Container(height: 100, child: HeaderWidget),
Expanded(child: ListView.builder(...)),
Container(height: 100, child: FooterWidget),
]
),

滚动将应用于 ListView。

关于flutter - 如何实现位置: sticky and bottom 0 with Flutter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55862884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com