[英]How to add and scroll two lists at the same time?
I have one scrollable list similar to cupertinoPicker.我有一个类似于 cupertinoPicker 的可滚动列表。 I need to make it so that I have 2 lists, one with the values I already have and the second list with the icons on the right.
我需要这样做,以便我有 2 个列表,一个包含我已经拥有的值,第二个列表包含右侧的图标。 Tell me, how can I add a second list and make it so that when scrolling, 2 lists scroll at once?
告诉我,如何添加第二个列表并使其在滚动时同时滚动 2 个列表? Like the example I have attached below.
就像我在下面附上的例子一样。 Now I have only one list with values, I need to add a second list with icons on the right and make it scroll along with the main one
现在我只有一个带有值的列表,我需要在右侧添加第二个带有图标的列表,并使其与主列表一起滚动
picker拣货员
class WheelPicker extends StatefulWidget {
final Function(dynamic) onValueChanged;
final int? startPosition;
final double itemSize;
final double? listHeight;
final int currentPosition;
final double? listWidth;
final FixedExtentScrollController? controller;
final List<String> list;
const WheelPicker({
Key? key,
required this.onValueChanged,
required this.currentPosition,
required this.itemSize,
this.startPosition,
this.listHeight,
this.controller,
this.listWidth, required this.list,
}) : super(key: key);
@override
State<WheelPicker> createState() => _WheelPickerState();
}
class _WheelPickerState extends State<WheelPicker> {
FixedExtentScrollController? fixedExtentScrollController;
int? currentPosition;
@override
void initState() {
super.initState();
currentPosition = widget.currentPosition;
fixedExtentScrollController = widget.controller ??
FixedExtentScrollController(initialItem: currentPosition ?? 0);
}
void _listener(int position) {
setState(() {
currentPosition = position;
});
widget.onValueChanged(currentPosition);
}
@override
Widget build(BuildContext context) {
return RotatedBox(
quarterTurns: 0,
child: SizedBox(
height: widget.listHeight ?? double.infinity,
width: widget.listWidth ?? double.infinity,
child: _getListWheelScrollView(),
),
);
}
Widget _getListWheelScrollView() {
return ListWheelScrollView(
diameterRatio: 20,
onSelectedItemChanged: _listener,
controller: fixedExtentScrollController,
physics: const FixedExtentScrollPhysics(
parent: BouncingScrollPhysics(),
),
useMagnifier: true,
magnification: 1,
itemExtent: widget.itemSize,
children: _convertListItems(),
);
}
List<Widget> _convertListItems() {
List<Widget> children = [
for (int i = 0; i < widget.list.length; i++) _item(widget.list[i], i),
];
return children;
}
Widget _item(String text, int pos) {
return Align(
alignment: Alignment.centerLeft,
child: Container(
height: 48,
width: double.infinity,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: currentPosition == pos
? constants.Colors.white
: Colors.transparent,
),
),
child: Text(
text,
style: currentPosition == pos
? constants.Styles.smallHeavyTimerTextStyleWhite
: constants.Styles.smallerBookTextStyleWhite.copyWith(
color: constants.Colors.white.withOpacity(0.5),
),
),
),
);
}
}
now I have我现在有
need to do需要做
you can do something like this:你可以这样做:
FixedExtentScrollController? fixedExtentScrollController1;
FixedExtentScrollController? fixedExtentScrollController2;
RotatedBox(
quarterTurns: 0,
child: SizedBox(
height: widget.listHeight ?? double.infinity,
width: widget.listWidth ?? double.infinity,
child: row(
children:[
expanded(
child: _getListWheelScrollView(fixedExtentScrollController1),
),
expanded(
child: _getListWheelScrollView(fixedExtentScrollController2),
),
]
),
),
)
Widget _getListWheelScrollView(FixedExtentScrollController controller) {
return ListWheelScrollView(
diameterRatio: 20,
onSelectedItemChanged: _listener,
controller: controller,
physics: const FixedExtentScrollPhysics(
parent: BouncingScrollPhysics(),
),
useMagnifier: true,
magnification: 1,
itemExtent: widget.itemSize,
children: _convertListItems(),
);
}
You can add a listener on main controller, then listen to it changes and set the position on second controller.您可以在主 controller 上添加一个监听器,然后监听它的变化并在第二个 controller 上设置 position。
final ScrollController mainCOntroller = ScrollController();
final ScrollController secondController = ScrollController();
@override
void initState() {
super.initState();
mainCOntroller.addListener(() {
if (mainCOntroller.hasClients && secondController.hasClients) {
secondController.jumpTo(mainCOntroller.offset);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Row(
children: [
Expanded(
child: ListView.builder(
controller:mainCOntroller ,
itemBuilder: (context, index) => Text("main $index"),
),
),
Expanded(
child: ListView.builder(
controller: secondController,
itemBuilder: (context, index) => Text("listener $index"),
),
),
],
));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.