Home > Mobile >  How to sort list items by condition and not show all?
How to sort list items by condition and not show all?

Time:09-27

I have a horizontal list in which I display the elements, but I encountered an error, I do not need to display all the elements, but display only those that match the condition. But if the element does not match the condition, I try to increase the index, but I encounter an error that nothing is displayed at all, an empty list. Tell me how can I sort the elements that match the condition and display them?

PageView.builder(
                            physics: const BouncingScrollPhysics(),
                            controller: PageController(viewportFraction: 1),
                            itemCount: setItemCount(),
                            onPageChanged: (int index) => setState(() {
                              _index = index;
                            }),
                            itemBuilder: (_, index) {
                              Widget? futureOrders;
                              var startedAtParse = DateTime.parse(
                                  widget.bookingsList[index].startedAt);
                              String startedAtFormatted =
                                  DateFormat("dd.MM.yyyy")
                                      .format(startedAtParse);
                              if (widget.isOnlyPending) {
                                if (!widget.bookingsList[index].confirmed &&
                                    startedAtFormatted == selectedDayOrders) {
                                  futureOrders = PoyntsBookingsCard(
                                    order: approvedList[index],
                                  );
                                }
                              } else {
                                if (startedAtFormatted == selectedDayOrders) {
                                  approvedList.add(widget.bookingsList[index]);
                                  futureOrders = PoyntsBookingsCard(
                                    order: approvedList[index],
                                  );
                                } else {
                                  setState(() {
                                    index  ;
                                  });
                                }
                              }
                              return Transform.scale(
                                scale: index == _index ? 1 : 0.95,
                                child: futureOrders,
                              );
                            },
                          ),

CodePudding user response:

if I have this list as example

List<int> list = [1, 2, 3, 10, 50];

and I want to filter it with numbers bigger than 5 then :

List<int> filteredList = list.where((number) => number > 5);

it will result a list that have element with that number > 5 condition which is [10, 50]

CodePudding user response:

I think you are running around in circles here. You first initiate PageView.builder on the entire bookingList, and then try to figure out what to show.

Here's my try on fixing this - I'm using a simplified example, showing only even numbers, but you can apply any criteria you want.

import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  
  final l=<int>[9,10,11,12,13,14,15];
  @override
  Widget build(BuildContext context) {
    var iter=l.where( (e) => e.isEven);
    var cnt=iter.length;
    
    
    return PageView.builder(
                            physics: const BouncingScrollPhysics(),
                            controller: PageController(viewportFraction: 1),
                            itemCount: cnt,
                            itemBuilder: (_, index) {
                              return Center(
          child:Text(iter.elementAt(index).toString()));
                               
                            }
  );
  }
}
  • Related