Skip to content

Instantly share code, notes, and snippets.

@horgag1
Last active May 23, 2021 04:59
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save horgag1/c4ce662e62d3a0a7ace0648697651dbe to your computer and use it in GitHub Desktop.
Save horgag1/c4ce662e62d3a0a7ace0648697651dbe to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<List<Result>> fetchResults(http.Client client) async {
final response = await client.get('https://api.myjson.com/bins/j5xau');
// Use the compute function to run parseResults in a separate isolate
return compute(parseResults, response.body);
}
// A function that will convert a response body into a List<Result>
List<Result> parseResults(String responseBody) {
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<Result>((json) => Result.fromJson(json)).toList();
}
class Result {
final String sex;
final String region;
final int year;
final String statistic;
final String value;
Result({this.sex, this.region, this.year, this.statistic, this.value});
bool selected = false;
factory Result.fromJson(Map<String, dynamic> json) {
return Result(
sex: json['sex'] as String,
region: json['region'] as String,
year: json['year'] as int,
statistic: json['statistic'] as String,
value: json['value'] as String,
);
}
}
class ResultsDataSource extends DataTableSource {
final List<Result> _results;
ResultsDataSource(this._results);
void _sort<T>(Comparable<T> getField(Result d), bool ascending) {
_results.sort((Result a, Result b) {
if (!ascending) {
final Result c = a;
a = b;
b = c;
}
final Comparable<T> aValue = getField(a);
final Comparable<T> bValue = getField(b);
return Comparable.compare(aValue, bValue);
});
notifyListeners();
}
int _selectedCount = 0;
@override
DataRow getRow(int index) {
assert(index >= 0);
if (index >= _results.length) return null;
final Result result = _results[index];
return DataRow.byIndex(
index: index,
selected: result.selected,
onSelectChanged: (bool value) {
if (result.selected != value) {
_selectedCount += value ? 1 : -1;
assert(_selectedCount >= 0);
result.selected = value;
notifyListeners();
}
},
cells: <DataCell>[
DataCell(Text('${result.sex}')),
DataCell(Text('${result.region}')),
DataCell(Text('${result.year}')),
DataCell(Text('${result.statistic}')),
DataCell(Text('${result.value}')),
]);
}
@override
int get rowCount => _results.length;
@override
bool get isRowCountApproximate => false;
@override
int get selectedRowCount => _selectedCount;
void _selectAll(bool checked) {
for (Result result in _results) result.selected = checked;
_selectedCount = checked ? _results.length : 0;
notifyListeners();
}
}
class DataTableDemo extends StatefulWidget {
ResultsDataSource _resultsDataSource = ResultsDataSource([]);
bool isLoaded = false;
@override
_DataTableDemoState createState() => _DataTableDemoState();
}
class _DataTableDemoState extends State<DataTableDemo> {
ResultsDataSource _resultsDataSource = ResultsDataSource([]);
bool isLoaded = false;
int _rowsPerPage = PaginatedDataTable.defaultRowsPerPage;
int _sortColumnIndex;
bool _sortAscending = true;
void _sort<T>(
Comparable<T> getField(Result d), int columnIndex, bool ascending) {
_resultsDataSource._sort<T>(getField, ascending);
setState(() {
_sortColumnIndex = columnIndex;
_sortAscending = ascending;
});
}
Future<void> getData() async {
final results = await fetchResults(http.Client());
if (!isLoaded) {
setState(() {
_resultsDataSource = ResultsDataSource(results);
isLoaded = true;
});
}
}
@override
Widget build(BuildContext context) {
getData();
return Scaffold(
appBar: AppBar(
title: const Text('Data tables'),
),
body: ListView(padding: const EdgeInsets.all(20.0), children: <Widget>[
PaginatedDataTable(
header: const Text('Census Data'),
rowsPerPage: _rowsPerPage,
onRowsPerPageChanged: (int value) {
setState(() {
_rowsPerPage = value;
});
},
sortColumnIndex: _sortColumnIndex,
sortAscending: _sortAscending,
onSelectAll: _resultsDataSource._selectAll,
columns: <DataColumn>[
DataColumn(
label: const Text('Sex'),
onSort: (int columnIndex, bool ascending) => _sort<String>(
(Result d) => d.sex, columnIndex, ascending)),
DataColumn(
label: const Text('Region'),
numeric: true,
onSort: (int columnIndex, bool ascending) => _sort<String>(
(Result d) => d.region, columnIndex, ascending)),
DataColumn(
label: const Text('Year'),
numeric: true,
onSort: (int columnIndex, bool ascending) => _sort<num>(
(Result d) => d.year, columnIndex, ascending)),
DataColumn(
label: const Text('Data'),
numeric: true,
onSort: (int columnIndex, bool ascending) => _sort<String>(
(Result d) => d.statistic, columnIndex, ascending)),
DataColumn(
label: const Text('Value'),
numeric: true,
onSort: (int columnIndex, bool ascending) => _sort<String>(
(Result d) => d.value, columnIndex, ascending)),
],
source: _resultsDataSource)
]));
}
}
void main() {
runApp(MaterialApp(home: DataTableDemo()));
}
@thukuwanjiku
Copy link

Just wanted to say thank you for this. It helped me somewhere.

@neezystudio
Copy link

where is the url of the exported json coming from?

@hassanesmb
Copy link

Just wanted to say thank you for this. It helped me somewhere.

can you help me , im new to flutter, i know how to connect flutter to google sheets but my problem is to make this code works

@thukuwanjiku
Copy link

Just wanted to say thank you for this. It helped me somewhere.

can you help me , im new to flutter, i know how to connect flutter to google sheets but my problem is to make this code works

I haven't worked with Google Sheets, but as long as you're able to fetch the data into your app, the displaying it using a Paginated Table should work. What point are you having problems?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment