gpt4 book ai didi

listview - 更改ListView中当前顶部可见项的颜色-Flutter

转载 作者:行者123 更新时间:2023-12-03 04:22:16 24 4
gpt4 key购买 nike

我想在用户滚动浏览列表时使列表中屏幕顶部的可见项目更改颜色。

我最接近的是插件ScrollablePositionedList:
https://pub.dev/packages/scrollable_positioned_list

现在,列表中的第一项会更改颜色,或者在我热重新加载时,位于顶部屏幕的任何一项都会更改。但是当我滚动时,他们并没有实时进行操作。

我将非常感谢您的帮助。请在下面找到相关代码。提前致谢!

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:scrollviewexperiment/models/place_data.dart';
import 'package:scrollviewexperiment/widgets/place_tile.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';

class PlacesList extends StatefulWidget {
@override
_PlacesListState createState() => _PlacesListState();
}

class _PlacesListState extends State<PlacesList> {
final ItemPositionsListener itemPositionsListener =
ItemPositionsListener.create();

ItemScrollController _itemScrollController = ItemScrollController();
int currentIndex = 0;



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

@override
Widget build(BuildContext context) {
return Consumer<PlaceData>(
builder: (context, placeData, child) {
return ScrollablePositionedList.builder(
itemCount: placeData.placeCount,
itemPositionsListener: itemPositionsListener,
itemBuilder: (context, index) {
final place = placeData.places[index];
return Card(
color: index == currentIndex ? Colors.blue : Colors.white,
elevation: 2.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
child: PlaceTile(
placeName: place.name,
distance: place.distance,
),
);
},
);
},
);
}

currentVisibleIndex() {
itemPositionsListener.itemPositions.addListener(() {
int min;
if (itemPositionsListener.itemPositions.value.isNotEmpty) {
min = itemPositionsListener.itemPositions.value
.where((ItemPosition position) => position.itemTrailingEdge > 0)
.reduce((ItemPosition min, ItemPosition position) =>
position.itemTrailingEdge < min.itemTrailingEdge
? position
: min)
.index;
print('Min Index $min');
currentIndex = min;
}
});
}
}

最佳答案

我想到了!

我在ScrollablePositionedList.builder的itemBuilder中返回了一个ValueListenableBuilder

这不是世界上最干净的解决方案,所以如果有人可以对此改进,我很想知道。

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:scrollviewexperiment/models/place_data.dart';
import 'package:scrollviewexperiment/widgets/place_tile.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';

class PlacesList extends StatefulWidget {
@override
_PlacesListState createState() => _PlacesListState();
}

class _PlacesListState extends State<PlacesList> {
final ItemPositionsListener itemPositionsListener =
ItemPositionsListener.create();

@override
Widget build(BuildContext context) {
return Consumer<PlaceData>(
builder: (context, placeData, child) {
return ScrollablePositionedList.builder(
itemCount: placeData.placeCount,
itemPositionsListener: itemPositionsListener,
itemBuilder: (context, index) {
final place = placeData.places[index];
return ValueListenableBuilder<Iterable<ItemPosition>>(
valueListenable: itemPositionsListener.itemPositions,
builder: (context, positions, child) {
int min;
if (positions.isNotEmpty) {
min = positions
.where((ItemPosition position) =>
position.itemTrailingEdge > 0)
.reduce((ItemPosition min, ItemPosition position) =>
position.itemTrailingEdge < min.itemTrailingEdge
? position
: min)
.index;
}
return Card(
color: index == min ? Colors.blue : Colors.white,
elevation: 2.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
child: PlaceTile(
placeName: place.name,
distance: place.distance,
),
);
},
);
},
);
},
);
}
}

关于listview - 更改ListView中当前顶部可见项的颜色-Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61966756/

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