-1
I am new to Flutter programming. I have assembled a page that when calling via menu I would like to present the data in a Datatable.
I even managed to do this, but only presents the data when I click to change the order, I should present the data right when opening the screen.
I will post here as mounted, if you can give a strength. I thank from now!
import 'dart:convert';
import 'package:cibernetica/screens/modelDiscip.dart';
import 'package:flutter/material.dart';
import 'package:cibernetica/screens/loginuser.dart';
import 'package:http/http.dart' as http;
class pagdisciplina extends StatefulWidget {
var oTbAluno;
pagdisciplina({this.oTbAluno});
@override
_pagdisciplinaState createState() => _pagdisciplinaState();
}
class _pagdisciplinaState extends State<pagdisciplina> {
var id = ModelDiscip.id;
List<ModelDiscip> tbDiscip;
bool sort;
@override
void initState() {
sort = false;
setState(() {
var tbtmp = getDisciplina(id);
tbDiscip = getConvert(tbtmp);
});
super.initState();
}
onSortColum(int columnIndex, bool ascending) {
if (columnIndex == 0) {
if (ascending) {
setState(() {
tbDiscip.sort((a, b) => a.disciplina.compareTo(b.disciplina));
});
} else {
setState(() {
tbDiscip.sort((a, b) => b.disciplina.compareTo(a.disciplina));
});
}
}
}
DataTable dataBody() {
return DataTable(
sortAscending: sort,
sortColumnIndex: 0,
columns: [
DataColumn(
label: Text("Disciplina"),
numeric: false,
tooltip: "Nome da disciplina",
onSort: (columnIndex, ascending) {
setState(() {
sort = !sort;
});
onSortColum(columnIndex, ascending);
}),
DataColumn(
label: Text("Professor"),
numeric: false,
tooltip: "Professor da disciplina")
],
rows: tbDiscip
.map(
// ignore: non_constant_identifier_names
(ModelDiscip) => DataRow(cells: [
DataCell(Text(ModelDiscip.disciplina), onTap: () {
print('Selecionado ${ModelDiscip.disciplina}');
}),
DataCell(
Text(ModelDiscip.professor),
),
]),
)
.toList(),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Disciplinas'),
),
body: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
verticalDirection: VerticalDirection.down,
children: <Widget>[
Center(
child: dataBody(),
)
],
),
);
}
listaTrabalhos() {
return ListView.builder(
itemBuilder: (context, index) {
return ListTile(
title: Text(''),
);
},
);
}
static Future<List<dynamic>> getDisciplina(int id) async {
String url =
'http://facbrasil.com/mobile/educacional/trabalho/' + id.toString();
final response = await http.get(
url,
headers: {'Content-Type': 'application/json; charset=UTF-8'},
);
var convDataToJson = jsonDecode(response.body);
List<dynamic> listaDiscip = new List<dynamic>();
for (Map<String, dynamic> json in convDataToJson) {
ModelDiscip lista = ModelDiscip.fromJson(json);
listaDiscip.add(lista);
}
return listaDiscip;
}
List<ModelDiscip> getConvert(Future<List<dynamic>> oLista) {
List<ModelDiscip> listConvert = [];
oLista.then((value) =>
{if (value != null) value.forEach((item) => listConvert.add(item))});
return listConvert == null ? [] : listConvert;
}
Josenaldo, good evening. Thanks for the feedback and the tips. I will make these adjustments that scored and answer as soon as finalize the adjustments.
– Valdemir Rabelo