Rol*_*iza 9 dart flutter flutter-sliver flutter-layout
我想用水平分页滚动呈现卡片,并且每次都能看到上一张和下一张卡片的边框。flutter PageView 小部件几乎产生了我想要的结果,但它没有按照我想要的方式显示页面,这是我的代码
import 'package:flutter/material.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: 'PageView Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'PageView Alignment'),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: PageView.builder(
itemCount: 5,
itemBuilder: (context, i) => Container(
color: Colors.blue,
margin: const EdgeInsets.only(right: 10),
child: Center(child: Text("Page $i")),
),
controller: PageController(viewportFraction: .7),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望 PageView 与屏幕左侧对齐,或者至少与第一页对齐,即删除Page 0. 我缺少任何 PageView 参数吗?或者是否存在产生我正在寻找的结果的其他组件?
Rol*_*iza 26
在对我自己的需求进行更深入的分析并检查了PageView小部件的源代码后,我意识到我需要一个逐项工作的滚动小部件,但同时我需要为每个项目分配空间与普通卷轴相同,所以我需要更改ScrollPhysics普通卷轴的 。在发现这篇文章中,在某种程度上描述了颤动中的滚动物理并且接近我的需求,不同之处在于我需要在当前可见小部件的两侧添加空间,而不仅仅是在右侧。
所以我把CustomScrollPhysics帖子里的内容拿过来用这种方式修改了(帖子代码中修改过的部分用h<--和-->评论围起来了:
class CustomScrollPhysics extends ScrollPhysics {
final double itemDimension;
const CustomScrollPhysics(
{required this.itemDimension, ScrollPhysics? parent})
: super(parent: parent);
@override
CustomScrollPhysics applyTo(ScrollPhysics? ancestor) {
return CustomScrollPhysics(
itemDimension: itemDimension, parent: buildParent(ancestor));
}
double _getPage(ScrollMetrics position, double portion) {
// <--
return (position.pixels + portion) / itemDimension;
// -->
}
double _getPixels(double page, double portion) {
// <--
return (page * itemDimension) - portion;
// -->
}
double _getTargetPixels(
ScrollMetrics position,
Tolerance tolerance,
double velocity,
double portion,
) {
// <--
double page = _getPage(position, portion);
// -->
if (velocity < -tolerance.velocity) {
page -= 0.5;
} else if (velocity > tolerance.velocity) {
page += 0.5;
}
// <--
return _getPixels(page.roundToDouble(), portion);
// -->
}
@override
Simulation? createBallisticSimulation(
ScrollMetrics position, double velocity) {
// If we're out of range and not headed back in range, defer to the parent
// ballistics, which should put us back in range at a page boundary.
if ((velocity <= 0.0 && position.pixels <= position.minScrollExtent) ||
(velocity >= 0.0 && position.pixels >= position.maxScrollExtent)) {
return super.createBallisticSimulation(position, velocity);
}
final Tolerance tolerance = this.tolerance;
// <--
final portion = (position.extentInside - itemDimension) / 2;
final double target =
_getTargetPixels(position, tolerance, velocity, portion);
// -->
if (target != position.pixels) {
return ScrollSpringSimulation(spring, position.pixels, target, velocity,
tolerance: tolerance);
}
return null;
}
@override
bool get allowImplicitScrolling => false;
}
Run Code Online (Sandbox Code Playgroud)
总之,我所做的是把当前可见的小部件(即离开了额外的空间的一半(position.extentInside - itemDimension) / 2),并把它添加到基于滚动位置的页面计算,使部件更小的可见滚动大小,但考虑到整个范围作为单个页面,并将其减去基于页面的滚动像素计算,防止“页面”放置在小部件侧面的半可见部分之前或之前。
另一个变化itemDimension不是滚动范围除以元素数量,我需要这个值是滚动方向上每个小部件的大小。
这就是我最终的结果:
当然,这个实现有一些限制:
我没有专注于解决这个限制和拥有一个更完整的小部件,因为在我需要这个小部件的情况下,这个限制是确保的。下面是上面例子的完整代码。
https://gist.github.com/rolurq/5db4c0cb7db66cf8f5a59396faeec7fa