为什么所有 Slider 小部件在 ListView.builder 中一起移动?
Why are all Slider widgets moving together in ListView.builder?
我有一个 ListView.builder() 方法可以构建多张卡片,每张卡片包含一张图片、一些文本和一个滑块。问题是,每当我移动滑块时,它们都会一起移动,值也是如此。
double _sliderValue = 0.0;
final List<String> cardList = [
'assets/food.jpg',
'assets/food.jpg',
'assets/food.jpg',
];
void _setValue(double value) {
setState(() {
_sliderValue = value;
});
}
Widget _buildFoodCard(BuildContext context, int index) {
return Container(
height: 350,
child: Card(
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Column(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: Image.asset(
cardList[index],
fit: BoxFit.cover,
),
),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Text(
'${_sliderValue.round()}' + ' ITEMS',
style: TextStyle(color: Colors.white, fontSize: 15.0),
),
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
thumbColor: Colors.white,
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 10.0),
activeTrackColor: Color(0xff3ADEA7),
inactiveTrackColor: Colors.grey,
overlayColor: Colors.transparent,
trackHeight: 1.0),
child: Slider(
value: _sliderValue,
onChanged: _setValue,
min: 0.0,
max: 150.0,
divisions: 30,
),
),
],
),
color: Colors.transparent,
elevation: 0.0,
margin: EdgeInsets.all(10.0),
),
);
}
Widget _buildCardList() {
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: _builFoodCard,
itemCount: cardList.length,
);
}
SCREENSHOT
我该怎么做才能使每个滑块和值 move/increment 单独?
您为每张带有滑块的卡片实现一个状态,并具有自己的滑块状态。
Widget _buildCardList() {
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) =>
MyWidgetSlider(cardList[index]),
itemCount: cardList.length,
);
}
class MyWidgetSlider extends StatefulWidget {
final String data;
MyWidgetSlider(this.data) : super();
_MyWidgetSliderState createState() => _MyWidgetSliderState();
}
class _MyWidgetSliderState extends State<MyWidgetSlider> {
double _sliderValue;
@override
void initState() {
super.initState();
_sliderValue = 0.0;
}
void _setValue(double value) {
setState(() {
_sliderValue = value;
});
}
@override
Widget build(BuildContext context) {
return Container(
height: 350,
child: Card(
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Column(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: Image.asset(
this.widget.data,
fit: BoxFit.cover,
),
),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Text(
'${_sliderValue.round()}' + ' ITEMS',
style: TextStyle(color: Colors.white, fontSize: 15.0),
),
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
thumbColor: Colors.white,
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 10.0),
activeTrackColor: Color(0xff3ADEA7),
inactiveTrackColor: Colors.grey,
overlayColor: Colors.transparent,
trackHeight: 1.0),
child: Slider(
value: _sliderValue,
onChanged: _setValue,
min: 0.0,
max: 150.0,
divisions: 30,
),
),
],
),
color: Colors.transparent,
elevation: 0.0,
margin: EdgeInsets.all(10.0),
),
);
}
}
我有一个 ListView.builder() 方法可以构建多张卡片,每张卡片包含一张图片、一些文本和一个滑块。问题是,每当我移动滑块时,它们都会一起移动,值也是如此。
double _sliderValue = 0.0;
final List<String> cardList = [
'assets/food.jpg',
'assets/food.jpg',
'assets/food.jpg',
];
void _setValue(double value) {
setState(() {
_sliderValue = value;
});
}
Widget _buildFoodCard(BuildContext context, int index) {
return Container(
height: 350,
child: Card(
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Column(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: Image.asset(
cardList[index],
fit: BoxFit.cover,
),
),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Text(
'${_sliderValue.round()}' + ' ITEMS',
style: TextStyle(color: Colors.white, fontSize: 15.0),
),
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
thumbColor: Colors.white,
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 10.0),
activeTrackColor: Color(0xff3ADEA7),
inactiveTrackColor: Colors.grey,
overlayColor: Colors.transparent,
trackHeight: 1.0),
child: Slider(
value: _sliderValue,
onChanged: _setValue,
min: 0.0,
max: 150.0,
divisions: 30,
),
),
],
),
color: Colors.transparent,
elevation: 0.0,
margin: EdgeInsets.all(10.0),
),
);
}
Widget _buildCardList() {
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: _builFoodCard,
itemCount: cardList.length,
);
}
SCREENSHOT
我该怎么做才能使每个滑块和值 move/increment 单独?
您为每张带有滑块的卡片实现一个状态,并具有自己的滑块状态。
Widget _buildCardList() {
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) =>
MyWidgetSlider(cardList[index]),
itemCount: cardList.length,
);
}
class MyWidgetSlider extends StatefulWidget {
final String data;
MyWidgetSlider(this.data) : super();
_MyWidgetSliderState createState() => _MyWidgetSliderState();
}
class _MyWidgetSliderState extends State<MyWidgetSlider> {
double _sliderValue;
@override
void initState() {
super.initState();
_sliderValue = 0.0;
}
void _setValue(double value) {
setState(() {
_sliderValue = value;
});
}
@override
Widget build(BuildContext context) {
return Container(
height: 350,
child: Card(
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Column(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: Image.asset(
this.widget.data,
fit: BoxFit.cover,
),
),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Text(
'${_sliderValue.round()}' + ' ITEMS',
style: TextStyle(color: Colors.white, fontSize: 15.0),
),
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
thumbColor: Colors.white,
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 10.0),
activeTrackColor: Color(0xff3ADEA7),
inactiveTrackColor: Colors.grey,
overlayColor: Colors.transparent,
trackHeight: 1.0),
child: Slider(
value: _sliderValue,
onChanged: _setValue,
min: 0.0,
max: 150.0,
divisions: 30,
),
),
],
),
color: Colors.transparent,
elevation: 0.0,
margin: EdgeInsets.all(10.0),
),
);
}
}