暂无描述

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'package:flutter/material.dart';
  2. import 'dart:convert';
  3. import 'package:json_table/json_table.dart';
  4. class SimpleTable extends StatefulWidget {
  5. @override
  6. _SimpleTableState createState() => _SimpleTableState();
  7. }
  8. class _SimpleTableState extends State<SimpleTable> {
  9. final String jsonSample =
  10. '[{"id":"0","name":"Dr.Collazo","Oficina":"A","Phone":"787-522-0123","Espe":"Dentist"},{"id":"1","name":"Dr.Lelolelo","Oficina":"B","Phone":"787-533-4567","Espe":"Dentist"},{"id":"2","name":"Dr.Pepo","Oficina":"C","Phone":"787-544-8910","Espe":"otorrinolaringologo"}]';
  11. bool toggle = true;
  12. @override
  13. Widget build(BuildContext context) {
  14. var json = jsonDecode(jsonSample);
  15. return Scaffold(
  16. body: Container(
  17. padding: EdgeInsets.all(16.0),
  18. child: toggle
  19. ? Column(
  20. children: [
  21. JsonTable(
  22. json,
  23. showColumnToggle: true,
  24. tableHeaderBuilder: (String header) {
  25. return Container(
  26. padding: EdgeInsets.symmetric(
  27. horizontal: 8.0, vertical: 4.0),
  28. decoration: BoxDecoration(
  29. border: Border.all(width: 0.5),
  30. color: Colors.grey[300]),
  31. child: Text(
  32. header,
  33. textAlign: TextAlign.center,
  34. style: Theme.of(context).textTheme.display1.copyWith(
  35. fontWeight: FontWeight.w700,
  36. fontSize: 14.0,
  37. color: Colors.black87),
  38. ),
  39. );
  40. },
  41. tableCellBuilder: (value) {
  42. return Container(
  43. padding: EdgeInsets.symmetric(
  44. horizontal: 4.0, vertical: 2.0),
  45. decoration: BoxDecoration(
  46. border: Border.all(
  47. width: 0.5,
  48. color: Colors.grey.withOpacity(0.5))),
  49. child: Text(
  50. value,
  51. textAlign: TextAlign.center,
  52. style: Theme.of(context).textTheme.display1.copyWith(
  53. fontSize: 14.0, color: Colors.grey[900]),
  54. ),
  55. );
  56. },
  57. allowRowHighlight: true,
  58. rowHighlightColor: Colors.yellow[500].withOpacity(0.7),
  59. paginationRowCount: 20,
  60. ),
  61. SizedBox(
  62. height: 20.0,
  63. ),
  64. Text("Simple table which creates table directly from json")
  65. ],
  66. )
  67. : Center(
  68. child: Text(getPrettyJSONString(jsonSample)),
  69. ),
  70. ),
  71. floatingActionButton: FloatingActionButton(
  72. child: Icon(Icons.grid_on),
  73. onPressed: () {
  74. setState(
  75. () {
  76. toggle = !toggle;
  77. },
  78. );
  79. }),
  80. );
  81. }
  82. String getPrettyJSONString(jsonObject) {
  83. JsonEncoder encoder = new JsonEncoder.withIndent(' ');
  84. String jsonString = encoder.convert(json.decode(jsonObject));
  85. return jsonString;
  86. }
  87. }
  88. void main() => runApp(MyApp());
  89. class MyApp extends StatelessWidget {
  90. @override
  91. Widget build(BuildContext context) {
  92. return MaterialApp(
  93. title: 'Flutter Demo',
  94. theme: ThemeData(
  95. primarySwatch: Colors.blue,
  96. ),
  97. home: SimpleTable(),
  98. );
  99. }
  100. }