我有一个List<Address>
,在Address
类中,有几个键,介于:
id
,address
,city
和isPrimary
。
我想使用isPrimary
,假设当我将列表中的isPrimary
地址设置为true
时,该Address值将定位为up(主)。怎么做?
以下是我的意思是List<Address>
:
[
{ '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),
],
)));
}
}
答案 0 :(得分:1)
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(),
),
);
},
);
}
}
答案 1 :(得分:1)
为什么没有从数据源中检索元素并按需要使用这些元素?
例如。在显示元素之前对其进行排序。
class UserAddressItemList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<MainModel>(
builder: (context, child, model) {
var addressList = model.addressList; // <= retrieve elements
if (addressList != null) {
sortAsYouWish(addressList); // <= sort elements
}
return model.isLoadingUser
? LoadingProgress
: addressList == null // <= use elements
? NoProductFound()
: ListView.builder(
physics: ClampingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount:
addressList == null ? 0 : addressList.length, // <= use elements
itemBuilder: (context, i) {
var address = addressList[i]; // <= use elements
return _buildAddressItemList(address, context);
},
);
},
);
}
一种可能的排序方法(示例):
import 'package:queries/collections.dart';
void main() {
var q = Collection(addressList)
.orderByDescending((e) => e["isPrimary"] as bool)
.thenBy((e) => e["city"] as String);
var sortedData = q.toList();
print(sortedData);
}
var addressList = [
{
'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
},
];