import 'dart:async';

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

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

Future<List<Data>> fetchData() async {
var url = Uri.parse('http://bg-mnsao=5&tentruong=THCS%20V%C5%A9%20H%E1%BB%AFu&tenkhoi=6&tenlop=A&tenhuyen=B%C3%ACnh%20Giang');
final response = await http.get(url);
if (response.statusCode == 200) {
List jsonResponse = json.decode(response.body);
return jsonResponse.map((data) => Data.fromJson(data)).toList();
} else {
throw Exception('Unexpected error occured!');
}
}

class Data {
final int STT;
final String ma_the;
final String ho_ten;

Data({required this.STT, required this.ma_the, required this.ho_ten});

factory Data.fromJson(Map<String, dynamic> json) {
return Data(
STT: json['STT'],
ma_the: json['ma_the'],
ho_ten: json['ho_ten'],
);
}
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(useMaterial3: true),
home: const MyHomePage(),
);
}
}

class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter DataTable Example'),
),
body: const MyStatefulWidget());
}
}

class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({super.key});

@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
@override
Widget build(BuildContext context) {
return FutureBuilder<List<Data>>(
future: fetchData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return SingleChildScrollView(
child: DataTable(
border: TableBorder.all(width: 1),
columnSpacing: 30,
columns: const [
DataColumn(label: Text('STT'), numeric: true),
DataColumn(label: Text('ma_the'),),
DataColumn(label: Text('ho_ten')),
],
rows: List.generate(
snapshot.data!.length,
(index) {
var data = snapshot.data![index];
return DataRow(cells: [
DataCell(
Text(data.STT.toString()),
),
DataCell(
Text(data.ma_the.toString()),
),
DataCell(
Text(data.ho_ten),
),
]);
},
).toList(),
showBottomBorder: true,
),
);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
}
// By default show a loading spinner.
return const CircularProgressIndicator();
},
);
}
}

Listview tô dòng
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

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

class Data {
final int STT;
final String ma_the;
final String ho_ten;

Data({required this.STT, required this.ma_the, required this.ho_ten});

factory Data.fromJson(Map<String, dynamic> json) {
return Data(
STT: json['STT'],
ma_the: json['ma_the'],
ho_ten: json['ho_ten'],
);
}
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(useMaterial3: true),
home: const MyHomePage(),
);
}
}

class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});


@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter PaginatedDataTable Example'),
),
body: const MyStatefulWidget());
}
}

class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({super.key});

@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
List<Data> data = [];

fetchData() async {
var url = Uri.parse('http://bg-mnsaomaixanh.haiduong.edu.vn/api/DanhSach?caphoc=5&tentruong=THCS%20V%C5%A9%20H%E1%BB%AFu&tenkhoi=6&tenlop=A&tenhuyen=B%C3%ACnh%20Giang');
final response = await http.get(url);
if (response.statusCode == 200) {
List jsonResponse = json.decode(response.body);
setState(() {
data = jsonResponse.map((data) => Data.fromJson(data)).toList();
});
} else {
throw Exception('Unexpected error occured!');
}
}

@override
void initState() {
super.initState();
fetchData();
}

@override
Widget build(BuildContext context) {
return PaginatedDataTable(columns: const [
DataColumn(label: Text('STT'), numeric: true),
DataColumn(label: Text('ma_the'),),
DataColumn(label: Text('ho_ten')),
], source: MyData(data));
}
}

class MyData extends DataTableSource {
final List<Data> data;
MyData(this.data);

@override
int get rowCount => data.length;

@override
bool get isRowCountApproximate => false;

@override
int get selectedRowCount => 0;

@override
DataRow getRow(int index) {
final Data result = data[index];
return DataRow.byIndex(index: index,
color: index % 2 == 0
? MaterialStateProperty.resolveWith(getColor)
: null,
cells: <DataCell>[
DataCell(Text(result.STT.toString())),
DataCell(Text(result.ma_the.toString())),
DataCell(Text(result.ho_ten)),
]);
}
}
Color getColor(Set<MaterialState> states) {
return Colors.grey;
}