Manipulate rows per page to appear when certain amount of data is met, Flutter paginated datatable

251 Views Asked by At

Good day, I have a basic paginated datatable with rows per page multiple of 10, like this:

int _rowsPerPage = PaginatedDataTable.defaultRowsPerPage;
 PaginatedDataTable(
    rowsPerPage: _rowsPerPage,
    onRowsPerPageChanged: (int? value) {
    setState(() {
       _rowsPerPage = value!;
    });
    },
    availableRowsPerPage: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],

Sorry that it's not a complete code.

The situation right now is only 10 rows per page visible when the data hasn't reached 20 yet.

17 data but no option to pick 20

I want to make it possible that once the data reaches more than 10, 20 will appear. when the data reaches 21+, 30 rows per page will appear, and so on. I hope I explained it clearly enough, I'll appreciate any suggestion.

Thank you in advance.

EDIT: Here's a minimal reproducible code of a basic paginated data table. It has 37 dummy data. With the current code, it'll show options for 10, 20, and 30 data.

What I want is for it to show options for 10, 20, 30, and 40 data.

rows per page initial condition

// main.dart
import 'package:flutter/material.dart';
import 'dart:math';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        // Hide the debug banner
        debugShowCheckedModeBanner: false,
        title: 'table',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const HomeScreen());
  }
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final DataTableSource _data = MyData();
  int _rowsPerPage = PaginatedDataTable.defaultRowsPerPage;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Kindacode.com'),
      ),
      body: SingleChildScrollView(child:Column(
        children: [
          const SizedBox(
            height: 10,
          ),
          PaginatedDataTable(
            source: _data,
            header: const Text('My Products'),
            rowsPerPage: _rowsPerPage,
            onRowsPerPageChanged: (int? value) {
            setState(() {
               _rowsPerPage = value!;
            });
            },
            availableRowsPerPage: const[10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
            columns: const [
              DataColumn(label: Text('ID')),
              DataColumn(label: Text('Name')),
              DataColumn(label: Text('Price'))
            ],
            columnSpacing: 100,
            horizontalMargin: 10,
            showCheckboxColumn: false,
          ),
        ],
      )),
    );
  }
}

class MyData extends DataTableSource {
  // dummy data
  final List<Map<String, dynamic>> _data = List.generate(
      37,
      (index) => {
            "id": index,
            "title": "Item $index",
            "price": Random().nextInt(10000)
          });

  @override
  bool get isRowCountApproximate => false;
  @override
  int get rowCount => _data.length;
  @override
  int get selectedRowCount => 0;
  @override
  DataRow getRow(int index) {
    return DataRow(cells: [
      DataCell(Text(_data[index]['id'].toString())),
      DataCell(Text(_data[index]["title"])),
      DataCell(Text(_data[index]["price"].toString())),
    ]);
  }
}

I hope it's clear enough, thank you for your visit.

0

There are 0 best solutions below