Reputation: 1620
Here I want I've some listed item and I want to sort item them on its id or rating. when I click on the side drawer button it will need to rearrange the list view with sorted items hope you understand my question.
I've tried this smooth_sort package for sorting but it won't work for me because it will sort based on the index. and I want to sort based on my own value.
Here is my code
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(),
],
),
),
)
],
),
),
),
],
),
);
}
}
Upvotes: 1
Views: 3549
Reputation: 8032
You can use the sort
method of the list
for sorting the list in the specific format as follows
@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}");
}
}
And i have use the my demo custom list for it
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,
),
];
Please check the full source of code for it
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,
),
];
And above output of the program as follow
Upvotes: 3