gpt4 book ai didi

sorting - Flutter:-如何对自定义列表进行排序

转载 作者:行者123 更新时间:2023-12-03 04:22:44 25 4
gpt4 key购买 nike

在这里,我想列出一些项目,并希望按照ID或等级对它们进行排序。当我单击侧面抽屉按钮时,将需要使用已排序的项目重新排列列表 View ,希望您理解我的问题。

我已经尝试过使用smooth_sort软件包进行排序,但是它对我不起作用,因为它将根据索引进行排序。我想根据自己的值(value)进行排序。

这是输出图像
enter image description here

这是我的代码

class _MapAddressListingScreenState extends State<MapAddressListingScreen>
with AfterLayoutMixin<MapAddressListingScreen> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
List addressData;

@override
void afterFirstLayout(BuildContext context) {
_getAddress();
}

_getAddress() {
List requestedLatlong = ModalRoute.of(context).settings.arguments;

print("============================Address Data");
print(requestedLatlong);
setState(() {
addressData = requestedLatlong;
});
}

List<Widget> _addressData() {
List<Widget> _address = [];
for (int i = 0; i < addressData.length; i++) {
_address.add(
GestureDetector(
onTap: () {
print('address list taped');
AddWalkinModel model = widget.addWalkinModel;
print("After model");
model.fromMap = false;
model.fromMapListing = true;
model.autoAssign = addressData[i]['autoAssign'];
model.branchId = addressData[i]['branchId'];
model.branchServiceId = addressData[i]['branchServiceId'];
print("After Model Model");
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DynamicScreen(
addWalkinModel: model,
),
),
);
},
child: AddressListingTile(listingTileData: addressData[i]),
),
);
}
return _address;
}

Widget itemCard() {
return Container(
child: Column(
children: _addressData(),
),
);
}

@override
Widget build(BuildContext context) {
var colorStyles = Theming.colorstyle(context);
return Scaffold(
key: _scaffoldKey,
backgroundColor: colorStyles['primary'],
appBar: AppBar(
title: Text("BSP Listview "),
centerTitle: true,
elevation: 0,
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () {
Navigator.pop(context);
},
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.sort),
onPressed: () {
_scaffoldKey.currentState.openEndDrawer();
},
),
],
),
endDrawer: Drawer(
child: Column(
children: <Widget>[
SizedBox(
height: 100,
),
FlatButton(
onPressed: () {
ratingSort.shuffle();
},
child: Text("sort")),
],
),
),
body: Stack(
children: <Widget>[
Container(
child: ClipRRect(
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(30.0),
topRight: const Radius.circular(30.0)),
child: Stack(
children: <Widget>[
SingleChildScrollView(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
itemCard(),
],
),
),
)
],
),
),
),
],
),
);
}
}

最佳答案

您可以使用sortlist方法以特定格式对列表进行排序,如下所示

@override
void initState() {
super.initState();

for(int i=0;i<fields.length;i++) {
print("BEFORE==>>> ${fields[i].rating}");
}

fields.sort((a, b) => a.rating.compareTo(b.rating)); ///FROM THIS LINE YOU CAN PERFORM SHORTING ACCORDING TO THE RATING BASES

for(int i=0;i<fields.length;i++) {
print("AFTER==>>> ${fields[i].rating}");
}

}

我已经使用了我的演示自定义列表
List<Fields> fields = [
new Fields(
'DEFAULT CATEGORY',
2,
),
new Fields(
'DEFAULT CATEGORY',
1,
),
new Fields(
'DEFAULT CATEGORY',
5,
),
new Fields(
'DEFAULT CATEGORY',
3,
),
new Fields(
'DEFAULT CATEGORY',
4,
),
];

和输出将跟随
enter image description here

请检查完整的代码源
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutterlearningapp/colors.dart';

class HomeScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _HomeScreen();
}
}

class _HomeScreen extends State<HomeScreen> {
@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return Material(
child: Scaffold(
appBar: AppBar(
title: Text("Demo Scroll"),
),
body: Container(
height: double.infinity,
width: double.infinity,
color: Colors.white,
child: Column(
children: <Widget>[
RaisedButton(
child: Text("Shoring"),
onPressed: (){
setState(() {
fields.sort((a, b) => a.rating.compareTo(b.rating));

});

},


),
Expanded(
child: ListView.builder
(
itemCount: fields.length,
itemBuilder: (BuildContext ctxt, int index) {
return ListTile(
title: new Text("Rating #${fields[index].rating}"),
subtitle: new Text(fields[index].title),
);
}
) ,
)


],

),
),
));
}
}

class Fields {
final String title;
final int rating;

Fields(this.title, this.rating);
}

List<Fields> fields = [
new Fields(
'Two',
2,
),
new Fields(
'One',
1,
),
new Fields(
'DEFAULT CATEGORY',
5,
),
new Fields(
'Three',
3,
),
new Fields(
'Four',
4,
),
];

上面的程序输出如下

enter image description here

关于sorting - Flutter:-如何对自定义列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61694954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com