gpt4 book ai didi

flutter - 在 Flutter 中绘制 ListView 边界之外

转载 作者:行者123 更新时间:2023-12-02 19:37:20 28 4
gpt4 key购买 nike

我想将我的项目转换为比 ListView 本身更大。 (重点导航的意图)

我的列表:

        Container(
height: 100,
child: ListView.builder(
itemBuilder: (context, index) => HomeItem(title: '$index'),
scrollDirection: Axis.horizontal,
),
),

我的元素:

class HomeItem extends StatelessWidget {
final String title;
final bool expand;

const HomeItem({
@required this.title,
this.expand = false,
});

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: ThemeDimens.padding8),
child: Transform.scale(
scale: expand ? 1.5 : 1,
child: AnimatedContainer(
width: 50,
height: 100,
color: expand ? ThemeColors.accent : ThemeColors.primary,
duration: ThemeDurations.shortAnimationDuration(),
child: Center(
child: Text(title),
),
),
),
);
}
}

当前行为 enter image description here

预期行为 enter image description here

最佳答案

如果您尝试使用 OverflowBox 或 Transform,项目的内容仍将被剪切,并且不会绘制到其边界框之外。但可以使用 Overlay在列表顶部绘制一个元素并将其放置在特定的列表项上,尽管这有点复杂。

demo

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
final elements = List.generate(12, (i) => i);

int selectedIndex;
OverlayEntry overlayEntry;
List<LayerLink> layerLinks;

@override
void initState() {
super.initState();

// Creating a layer link for each list cell
layerLinks = List.generate(elements.length, (i) => LayerLink());
}

void createOverlayEntry(int i, BuildContext context) {
// Removing an overlay entry, if there was one
overlayEntry?.remove();

final renderBox = context.findRenderObject() as RenderBox;
final size = renderBox.size;
final offset = renderBox.localToGlobal(Offset.zero);

// Creating a new overlay entry linked to specific list element
overlayEntry = OverlayEntry(
builder: (context) => Positioned(
left: 0,
top: 0,
child: CompositedTransformFollower(
link: layerLinks[i],
showWhenUnlinked: false,
offset: Offset(-20, 0),
child: Material(
color: Colors.yellow,
child: InkWell(
onTap: () {
setState(() {
selectedIndex = null;
});

overlayEntry?.remove();
overlayEntry = null;
},
child: Container(
alignment: Alignment.center,
width: 70,
height: elementHeight,
child: Text('$i')
),
)
),
)
)
);

// Inserting an entry
Overlay.of(context).insert(overlayEntry);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
height: elementHeight,
child: ListView.separated(
scrollDirection: Axis.horizontal,
itemCount: elements.length,
itemBuilder: (c, i) {
return CompositedTransformTarget(
link: layerLinks[i],
child: Material(
color: Colors.red,
child: InkWell(
onTap: () {
setState(() {
selectedIndex = i;
});

createOverlayEntry(i, context);
},
child: Container(
alignment: Alignment.center,
width: 30,
child: Text('${elements[i]}'),
),
),
),
);
},
separatorBuilder: (c, i) {
return Container(width: 10, height: 10);
},
),
),
);
}
}

关于flutter - 在 Flutter 中绘制 ListView 边界之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60884031/

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