bosskmk / pluto_grid

PlutoGrid is a dataGrid for flutter that can be controlled by the keyboard on desktop and web. Of course, it works well on Android and IOS.
https://pluto.weblaze.dev
MIT License
637 stars 291 forks source link

[Help] Filterrow disappearing #957

Closed BilPat closed 7 months ago

BilPat commented 9 months ago

Hi! So I'm currently trying to switch over to flutter_bloc from classic state management, I've been working with flutter for over a year now, so there's a lot of refactoring that needs to be done in my project.

I started out with my included PlutoGrid, and for some reason the moment my event is called by selecting something from the dropdown and the state is updated, the filter row disappears, and I can't figure out why. The issue started when I used UniqueKey() as I wanted to force a rerender, as the respective row didn't change it's value.

Here's the code:

class OrderTable extends StatefulWidget {
  final update;
  OrderTable({Key? key, required this.update})
      : super(key: key);

  @override
  State<OrderTable> createState() => _OrderTableState();
}

class _OrderTableState extends State<OrderTable> {
  PlutoGridStateManager? stateManager;
  TextEditingController dateInput = TextEditingController();
  OrderBloc get _orderBloc => BlocProvider.of<OrderBloc>(context);
  List<PlutoRow> rows = [];

  List<PlutoRow> createRows(List<Order> orderList) {
    List<PlutoRow> rows = [];

    for (var i = 0; i < orderList.length; i++) {
      String getLastComment(Order order) {
        String? comment = order.statusHistory.isNotEmpty
            ? order.statusHistory.first.comments
            : null;
        if (comment == null || comment.toLowerCase() == 'null') {
          return 'kein Kommentar vorhanden';
        }
        return comment;
      }

      PlutoRow row = PlutoRow(
        cells: {
          'Status': PlutoCell(value: orderList[i].status),
          //other cells
        },
      );
      rows.add(row);
    }
    return rows;
  }

  @override
  Widget build(BuildContext context) {

    List<PlutoColumn> columns = <PlutoColumn>[
      PlutoColumn(
        title: 'Status',
        field: 'Status',
        type: PlutoColumnType.text(),
        renderer: (rendererContext) {
          Color? backgroundColor = Colors.white;

          if (rendererContext.cell.value.toString() == 'offen') {
          //colors for selected values
          }

          return material.SizedBox.expand(
            child: material.Container(
              child: material.Center(
                child: material.DropdownButtonHideUnderline(
                  child: material.DropdownButton<String>(
                      isExpanded: true,
                      iconEnabledColor: Colors.white,
                      icon: material.Icon(
                          material.Icons.arrow_drop_down_rounded,
                          color: backgroundColor),
                      style: TextStyle(fontWeight: FontWeight.bold),
                      dropdownColor: Colors.white,
                      value: rendererContext.cell.value.toString(),
                      items: [
                        material.DropdownMenuItem(
                          child: Text(
                            'offen',
                            style: TextStyle(color: Colors.red),
                            textAlign: TextAlign.center,
                          ),
                          value: 'offen',
                        ),
                        //other options
                      onChanged: (value) {
                        _orderBloc.add(UpDateOrdersEvent(id: rendererContext.row.cells.entries
                                .elementAt(3)
                                .value
                                .value,
                            newValue: value,
                            field: "Bearbeitet"));
                      }),
                ), 
              ),
            ),
          );
        },
      ),
      //other columns

    return material.Column(
      children: [
        SizedBox(
          height: 100,
          child: material.Material(
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Container(
                      color: Colors.green,
                      child: material.TextButton(
                        onPressed: () {
                          widget.update();
                        },
                        child: Row(
                          children: [
                            Text(
                              "Aktualisieren   ",
                              style: TextStyle(color: Colors.white),
                            ),
                            Icon(
                              FluentIcons.refresh,
                              size: 14.0,
                              color: Colors.white,
                            )
                          ],
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
        Expanded(
          child: BlocBuilder<OrderBloc, List<Order>>(builder: (context, orderList) {
            print(orderList[0].status);
            final rows = createRows(orderList);
            return PlutoGrid(
             key: UniqueKey()
              mode: PlutoGridMode.selectWithOneTap,
              columns: columns,
              onSelected: (PlutoGridOnSelectedEvent event) {
                var orderId = event.row!.cells['Bestellnr.']!.value;
                print(event.row!.cells['Bestellnr.']!.value);
                showDetailDialog(context, orderList, orderId, widget.update);
               },
              rows: rows,
              onLoaded: (PlutoGridOnLoadedEvent event) {

                if (stateManager == null) {

                  stateManager = event.stateManager;
                  stateManager!.setShowColumnFilter(true);
                }

              },
              onChanged: (PlutoGridOnChangedEvent event) {
                print(event);
              },
              configuration: PlutoGridConfiguration(
                columnFilter: PlutoGridColumnFilterConfig(
                  filters: [
                    ...FilterHelper.defaultFilters,
                  ],
                  resolveDefaultColumnFilter: (column, resolver) {
                    return resolver<PlutoFilterTypeContains>() as PlutoFilterType;
                  },
                ),
                localeText: PlutoGridLocaleText(
                  //locale Text
                ),
                style: PlutoGridStyleConfig(
                  evenRowColor: Color(0xcccccccc),
                ),
                scrollbar: const PlutoGridScrollbarConfig(
                  isAlwaysShown: true,
                  scrollbarThickness: 12,
                  scrollbarThicknessWhileDragging: 12,
                ),
              ),
            );

The BlocProvider is located in my main, and the state management is working with other widgets just fine. I've been trying to figure it out, but I really can't find the issue here. If i get rid of the UniqueKey() it doesnt rerender, but the print statement tells me, that bloc was updated with the correct value.

Thank you in advance!

github-actions[bot] commented 7 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 7 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.