mudit bansal
mudit bansal

Reputation: 31

flutter key event not working correctly(the state of the widget is updating twice)

i am making a desktop app with flutter, in which i am trying to make a date text field

This is the date widget code

import 'package:cashbook/core/utils/show_snackbar.dart';
import 'package:cashbook/fetures/accounts/presentation/widgets/stock_entry_datatable_columns.dart';
import 'package:cashbook/fetures/entries/domain/entities/sales_entry.dart';
import 'package:cashbook/fetures/entries/presentation/bloc/sales_entry_bloc.dart';
import 'package:cashbook/fetures/entries/presentation/widgets/entry_data_row.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class SalesEntryDataTable extends StatefulWidget {
  const SalesEntryDataTable(
      {super.key,
      required this.name,
      required this.selectedRowList,
      required this.onSelect});
  final String name;
  final List<SalesEntry> selectedRowList;
  final Function(bool selected, SalesEntry entry) onSelect;

  @override
  State<SalesEntryDataTable> createState() => _SalesEntryDataTableState();
}

class _SalesEntryDataTableState extends State<SalesEntryDataTable> {
  List<DataRow> getSalesDataRows(SalesEntryState state) {
    List<DataRow> rows = [];
    TextEditingController discriptionController = TextEditingController();
    TextEditingController perticularController = TextEditingController();
    TextEditingController nweidhtController = TextEditingController();
    TextEditingController rateController = TextEditingController();
    TextEditingController taxRateController = TextEditingController();
    TextEditingController taxAmountController = TextEditingController();
    TextEditingController amountCrController = TextEditingController();
    TextEditingController amountDrController = TextEditingController();
    DataRow addEntryDataRow = DataRow(cells: [
      const DataCell(
        TextField(
          textInputAction: TextInputAction.next,
          keyboardType: TextInputType.datetime,
          decoration: InputDecoration(hintText: "Date"),
        ),
      ),
      DataCell(TextField(
        controller: discriptionController,
        textInputAction: TextInputAction.next,
      )),
      DataCell(TextField(
        controller: perticularController,
        textInputAction: TextInputAction.next,
      )),
      DataCell(TextField(
        controller: nweidhtController,
        textInputAction: TextInputAction.next,
      )),
      DataCell(TextField(
        controller: rateController,
        textInputAction: TextInputAction.next,
      )),
      DataCell(TextField(
        controller: taxRateController,
        textInputAction: TextInputAction.next,
      )),
      DataCell(TextField(
        enabled: false,
        controller: taxAmountController,
        textInputAction: TextInputAction.next,
      )),
      DataCell(TextField(
        controller: amountCrController,
        textInputAction: TextInputAction.next,
      )),
      DataCell(TextField(
        controller: amountDrController,
        enabled: false,
        textInputAction: TextInputAction.next,
      )),
      DataCell(FilledButton(
          onPressed: () {
            context.read<SalesEntryBloc>().add(SalesEntryCreateEvent(
                date: DateTime.now(),
                perticularName: perticularController.text,
                accountName: widget.name,
                discription: discriptionController.text,
                taxRate: int.parse(taxRateController.text),
                amountCr: int.parse(amountCrController.text),
                nweight: int.parse(nweidhtController.text),
                sellingRate: int.parse(rateController.text)));
          },
          child: const Text("Create"))),
    ]);

    if (state is SalesEntryGetAccountSuccessState) {
      rows = state.list.map((e) {
        return getEntryDataRow(
            e, widget.selectedRowList, widget.onSelect, onUpdate);
      }).toList();
    }
    return [...rows, addEntryDataRow];
  }

  void onUpdate(SalesEntry entry) {
    context.read<SalesEntryBloc>().add(SalesEntryUpdateEvent(
        id: entry.id,
        date: entry.date,
        accountName: widget.name,
        perticularName: entry.perticularName,
        discription: entry.discription,
        taxRate: entry.taxRate,
        amountCr: entry.amountCr,
        nweight: entry.nweight,
        sellingRate: entry.sellingRate));
  }

  @override
  void initState() {
    context.read<SalesEntryBloc>().add(SalesEntryAccountGetEvent("aman"));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<SalesEntryBloc, SalesEntryState>(
      listener: (context, state) {
        if (state is SalesEntryGetAccountFailureState) {
          showSnackBar(context,
              const Text("error while getting this account sales entries"));
        }
        if (state is SalesEntryCreatedSuccessState) {
          context
              .read<SalesEntryBloc>()
              .add(SalesEntryAccountGetEvent(widget.name));
        }
        if (state is SalesEntryDeletedSuccessState) {
          context
              .read<SalesEntryBloc>()
              .add(SalesEntryAccountGetEvent(widget.name));
        }
        if (state is SalesEntryUpdatedSuccessState) {
          context
              .read<SalesEntryBloc>()
              .add(SalesEntryAccountGetEvent(widget.name));
        }
      },
      builder: (context, state) {
        if (state is SalesEntryLoading) {
          return const Center(
            child: CircularProgressIndicator(),
          );
        }
        if (state is SalesEntryGetAccountSuccessState) {
          return DataTable(
            columns: stockEntriesDatatableColumns,
            rows: getSalesDataRows(state),
          );
        }
        if (state is SalesEntryGetAccountFailureState) {
          return const Text("Couldn't get the sales entries of this account");
        }
        return const Text("Something went wrong");
      },
    );
  }
}

the problem is whenever i am pressing the ">" arrow key or the "<" arrow key on my keyboard the focus is moving twice to the next field idk why, i am usig block state management.

Please help me

i this the onkeyevent is beeing called twice idk please help me

Upvotes: 0

Views: 63

Answers (0)

Related Questions