List 颤振-如何将值从列表中向上定位?
我有一个List 颤振-如何将值从列表中向上定位?,list,dart,flutter,List,Dart,Flutter,我有一个列表,其中地址类中有几个键,它们之间: id,地址,城市和isPrimary 我想使用isPrimary,假设当我将列表中的isPrimary地址设置为true时,该地址值的位置为up(主)。怎么做 以下是我所指的列表: [ { 'id': 1, 'address': '40 Talbot Ambler, PA 19002', 'city': 'New York', 'isPrimary': false }, { 'id': 2, 'addr
列表
,其中地址
类中有几个键,它们之间:
id
,地址
,城市
和isPrimary
我想使用isPrimary
,假设当我将列表中的isPrimary
地址设置为true
时,该地址值的位置为up(主)。怎么做
以下是我所指的列表
:
[
{ 'id': 1,
'address': '40 Talbot Ambler, PA 19002',
'city': 'New York',
'isPrimary': false
},
{ 'id': 2,
'address': '618 Oak Valley West Deptford, NJ 08096',
'city': 'Sydney',
'isPrimary': true
},
{ 'id': 3,
'address': '8207 Gulf Ringgold, GA 30736',
'city': 'London',
'isPrimary': false
},
{ 'id': 4,
'address': '873 Ridgewood St.Romeoville, IL 60446',
'city': 'Manchester',
'isPrimary': false
},
]
我希望地址isPrimary=true
位于列表的顶部,然后是一个带有isPrimary=false
看起来是这样的:
[
{ 'id': 2,
'address': '618 Oak Valley West Deptford, NJ 08096',
'city': 'Sydney',
'isPrimary': true
},
{ 'id': 1,
'address': '40 Talbot Ambler, PA 19002',
'city': 'New York',
'isPrimary': false
},
{ 'id': 3,
'address': '8207 Gulf Ringgold, GA 30736',
'city': 'London',
'isPrimary': false
},
{ 'id': 4,
'address': '873 Ridgewood St.Romeoville, IL 60446',
'city': 'Manchester',
'isPrimary': false
},
]
请参见下面的我的代码:
class UserAddressItemList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<MainModel>(
builder: (context, child, model) {
return model.isLoadingUser
? LoadingProgress
: model.addressList == null
? NoProductFound()
: ListView.builder(
physics: ClampingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount:
model.addressList == null ? 0 : model.getAddressCount(),
itemBuilder: (context, i) {
var address = model.addressList[i];
return _buildAddressItemList(address, context);
},
);
},
);
}
Widget _buildAddressItemList(Address address, BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 3),
height: 150,
width: 280,
child: Card(
// color: Colors.blueGrey,
elevation: 2.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10))),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
SizedBox(height: 15),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 20),
// color: Colors.greenAccent,
width: 120,
child: Text(
'${address.address}\n'
'${address.city}',
style: Theme.of(context).textTheme.title.copyWith(
fontSize: 16,
fontWeight: FontWeight.w400,
height: 1.1),
),
),
Container(
padding: EdgeInsets.only(right: 15),
child: Icon(
FontAwesomeIcons.minus,
size: 16,
color: Colors.red,
))
],
),
SizedBox(height: 5),
Container(
height: 20,
// color: Colors.green,
margin: EdgeInsets.symmetric(horizontal: 10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
padding: EdgeInsets.only(bottom: 2),
child: Text(
"Delivery Address",
style: Theme.of(context)
.textTheme
.caption
.copyWith(
fontSize: 12, color: Colors.grey[400]),
)),
SizedBox(width: 10),
Container(
child: Icon(FontAwesomeIcons.solidCheckCircle,
size: 20,
color: address.isPrimary
? Colors.blue[600]
: Colors.grey),
)
],
)),
SizedBox(height: 5),
],
)));
}
}
类UserAddressItemList扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回范围modeldescendant(
生成器:(上下文、子对象、模型){
return model.isLoadingUser
?装载进度
:model.addressList==null
?找不到产品()
:ListView.builder(
物理:ClampingScrollPhysics(),
滚动方向:轴水平,
项目计数:
model.addressList==null?0:model.getAddressCount(),
itemBuilder:(上下文,i){
var address=model.addressList[i];
返回_buildAddressItemList(地址、上下文);
},
);
},
);
}
小部件_buildAddressItemList(地址,BuildContext上下文){
返回容器(
填充:边缘组。对称(水平:3),
身高:150,
宽度:280,
孩子:卡片(
//颜色:颜色。蓝灰色,
标高:2.0,
形状:圆形矩形边框(
borderRadius:borderRadius.all(半径.圆形(10)),
子:列(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
尺寸箱(高度:15),
划船(
crossAxisAlignment:crossAxisAlignment.start,
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
容器(
页边空白:仅限边缘组(左:20),
//颜色:Colors.greenAccent,
宽度:120,
子:文本(
“${address.address}\n”
“${address.city}”,
样式:Theme.of(context).textTheme.title.copyWith(
尺寸:16,
fontWeight:fontWeight.w400,
高度:1.1),,
),
),
容器(
填充:仅限边缘设置(右:15),
子:图标(
FontAwesomeIcons.减号,
尺码:16,
颜色:颜色,红色,
))
],
),
尺寸箱(高度:5),
容器(
身高:20,
//颜色:颜色。绿色,
边缘:边缘组。对称(水平:10),
孩子:排(
crossAxisAlignment:crossAxisAlignment.end,
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
容器(
填充:仅限边缘设置(底部:2),
子:文本(
“送货地址”,
风格:主题(上下文)
.文本主题
说明文字
.抄袭(
fontSize:12,颜色:Colors.grey[400],
)),
尺寸箱(宽度:10),
容器(
子:图标(FontAwesomeIcons.solidCheckCircle,
尺码:20,
颜色:address.isPrimary
?颜色。蓝色[600]
:颜色。灰色),
)
],
)),
尺寸箱(高度:5),
],
)));
}
}
isPrimary
不用于设置位置。您应该通过ScrollController手动设置位置
示例代码:
class ListViewPage extends StatefulWidget {
@override
ListViewPageState createState() {
return new ListViewPageState();
}
}
class ListViewPageState extends State<ListViewPage> {
ScrollController _scrollController;
@override
initState() {
_scrollController = ScrollController();
_scrollController.animateTo(
40.0, // insert your ListItem's height.
duration: Duration(
milliseconds: 500,
),
curve: Curves.linear,
);
super.initState();
}
Widget build(context) {
return ListView.builder(
controller: _scrollController,
itemCount: 100,
itemBuilder: (context, int index) {
return Container(
height: 40.0,
width: double.infinity,
child: Text(
index.toString(),
),
);
},
);
}
}
类ListViewPage扩展StatefulWidget{
@凌驾
ListViewPageState createState(){
返回新的ListViewPageState();
}
}
类ListViewPageState扩展了状态{
ScrollController\u ScrollController;
@凌驾
initState(){
_scrollController=scrollController();
_scrollController.animateTo(
40.0,//插入ListItem的高度。
持续时间:持续时间(
毫秒:500,
),
曲线:曲线。线性,
);
super.initState();
}
小部件构建(上下文){
返回ListView.builder(
控制器:\ u滚动控制器,
物品计数:100,
itemBuilder:(上下文,int索引){
返回容器(
身高:40.0,
宽度:double.infinity,
子:文本(
index.toString(),
),
);
},
);
}
}
要理解你的要求有点困难。你可以展示一些代码来解释你预期会发生什么,以及会发生什么吗?@Edman好的,添加了我的示例代码是地址,你说的是ListView的当前位置吗?@HeavenOSK Yeah,然后我希望地址isPrimary=true位于列表的顶部,后面是isPrimary=false的列表。所以当我显示时,列表中第一个地址的位置