如何解决在flutter中将滑块值更改为列表项值
我在此页面上加载了学生及其学位的列表。
首先我上了这个课:
class MyList {
final int id;
final String name;
final double degree;
MyList(this.id,this.name,this.degree);
}
然后我有此页面:
import 'package:flutter/material.dart';
import './class.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
List newList = [];
MyList newlist1 = MyList(1,'sar',25.0);
newList.add(newlist1);
MyList newlist2 = MyList(2,'Moh',10.0);
newList.add(newlist2);
MyList newlist3 = MyList(3,'John',12.0);
newList.add(newlist3);
return Scaffold(
body: ListView.builder(
itemBuilder: (ctx,index) {
double _value = newList[index].degree;
return Column(
children: [
Text(newList[index].id.toString()),Text(newList[index].name),Slider(
value: _value,min: 0,max: 100,divisions: 5,label: _value.round().toString(),onChanged: (double value) {
setState(() => _value = value);
}),],);
},itemCount: newList.length,));
}
}
我想做的是当我移动范围滑块两件事时:
1-将滑块的值更改为新值。
2-更新列表中的新学位值。
最好的问候
解决方法
在此代码中,您需要进行一些更改:
- 在MyList类中,应将度数变量更改为not 最后。 (最终变量在首次分配后不能更改。)
- newList的定义必须不包含在构建小部件中。 (在 在每个状态调用中重建statefull类)
- 将Slider的分度更改为100。
- 在setState调用中,应更改newList [index] .degree值
您的代码应如下所示:
class MyList {
final int id;
final String name;
double degree;
MyList(this.id,this.name,this.degree);
}
List _getNewList(){
List newList = [];
MyList newlist1 = MyList(1,'sar',25.0);
newList.add(newlist1);
MyList newlist2 = MyList(2,'Moh',10.0);
newList.add(newlist2);
MyList newlist3 = MyList(3,'John',12.0);
newList.add(newlist3);
return newList;
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List _newList = _getNewList();
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemBuilder: (ctx,index) {
double _value = _newList[index].degree;
return Column(
children: [
Text(_newList[index].id.toString()),Text(_newList[index].name),Slider(
value: _value,min: 0,max: 100,divisions: 100,label: _value.round().toString(),onChanged: (double value) {
setState(() {
_value = value;
_newList[index].degree = value;
});
}),],);
},itemCount: _newList.length,));
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。