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

时间:2020-05-09 10:05:06

标签: sorting flutter dart flutter-dependencies

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

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

这是输出图像 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(),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:2)

您可以使用sort的{​​{1}}方法按照以下特定格式对列表进行排序

list

我已经使用了我的演示自定义列表

@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}");
    }

  }

然后将输出 enter image description here

请检查完整的代码源

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