Home > Mobile >  guys please help me, how to make pull up refresh in datatable
guys please help me, how to make pull up refresh in datatable

Time:02-11

[I want to make datatable with pull up refresh data. and i want dataTable column is fixed! Please help me!] Here my aplication view [I want to make datatable with pull up refresh data. and i want dataTable column is fixed! Please help me!] Here my aplication view [I want to make datatable with pull up refresh data. and i want dataTable column is fixed! Please help me!] Here my aplication view

    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    import 'package:infinity_pagination/model/passenger_data.dart';
    import 'package:pull_to_refresh/pull_to_refresh.dart';
    import 'package:data_table_2/data_table_2.dart';
    import 'package:data_table_2/paginated_data_table_2.dart';

    void main() {
      HttpOverrides.global = new MyHttpOverrides();

      runApp(MyApp());
    }
    class MyHttpOverrides extends HttpOverrides {
      @override
      HttpClient createHttpClient(SecurityContext? context) {
        return super.createHttpClient(context)
          ..badCertificateCallback =
              (X509Certificate cert, String host, int port) => true;
      }
    }

    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            debugShowCheckedModeBanner: false,
            home: HomePage());
      }
    }

    class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }

    class _HomePageState extends State<HomePage> {
      int currentPage = 1;

      late int totalPages;

      List<Passenger> passengers = [];

      final RefreshController refreshController =
          RefreshController(initialRefresh: true);

      Future<bool> getPassengerData({bool isRefresh = false}) async {
        if (isRefresh) {
          currentPage = 1;
        } else {
          if (currentPage >= totalPages) {
            refreshController.loadNoData();
            return false;
          }
        }

        final Uri uri = Uri.parse(
            "https://api.instantwebtools.net/v1/passenger?page=$currentPage&size=10");

        final response = await http.get(uri);

        if (response.statusCode == 200) {
          final result = passengersDataFromJson(response.body);

          if (isRefresh) {
            passengers = result.data;
          }else{
            passengers.addAll(result.data);
          }

          currentPage  ;

          totalPages = result.totalPages;

          print(response.body);
          setState(() {});
          return true;
        } else {
          return false;
        }
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("Infinite List Pagination"),
          ),
          body: 
          SmartRefresher(
            controller: refreshController,
            enablePullUp: true,
            onRefresh: () async {
              final result = await getPassengerData(isRefresh: true);
              if (result) {
                refreshController.refreshCompleted();
              } else {
                refreshController.refreshFailed();
              }
            },
            onl oading: () async {
              final result = await getPassengerData();
              if (result) {
                refreshController.loadComplete();
              } else {
                refreshController.loadFailed();
              }
            },
            child: 
               ListView.separated(
                itemBuilder: (context, index){
                final passenger = passengers[index];
               return DataTable2(
               columnSpacing: 12,
                horizontalMargin: 12,
                 minWidth: 600,
                 columns: <DataColumn>[
                   DataColumn(label: Text('Name', overflow: TextOverflow.ellipsis)),
                   DataColumn(label: Text('Country', overflow: TextOverflow.ellipsis)),
                   DataColumn(label: Text('Flight', overflow: TextOverflow.ellipsis)),
                 ],
                rows: <DataRow>[
                DataRow(
                cells: <DataCell>[
                      DataCell( Text('${passenger.name}')),
                      DataCell( Text('${passenger.airline.country}')),
                      DataCell( Text('${passenger.airline.name}')),
                     ]
                    )
                 ]
                
                    
                  );
                },
                separatorBuilder: (context, index) => Divider(),
                itemCount: passengers.length,
              
            ),
          ),
        );
      }
    }

CodePudding user response:

For the DataTable Column is fixed, you should make the code before the pull up, and after that show the pull up data, so the dataTable will shown just once.

CodePudding user response:

to pull to refresh you can use widget

RefreshIndicator
then onRefresh :() async {
function_call_data
}

need some minimal code for column

but for the column headers that you create outside the list to display your data.

  • Related