DiscoverModule/lib/ui_screen/ranking.dart

437 lines
14 KiB
Dart
Raw Normal View History

2024-05-20 10:29:02 +00:00
import 'dart:convert';
import 'dart:io';
import 'package:discover_module/custom_widget/floating_btn.dart';
import 'package:discover_module/custom_widget/show_alert.dart';
import 'package:discover_module/ui_screen/profile.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:syncfusion_flutter_xlsio/xlsio.dart';
import 'package:universal_html/html.dart' show AnchorElement;
class Ranking extends StatefulWidget {
@override
State<Ranking> createState() => _RankingState();
}
class _RankingState extends State<Ranking> {
late _DataSource _dataSource; // Instance variable for _DataSource
@override
void initState() {
super.initState();
print("hii_initState");
_dataSource = _DataSource(context); // Initialize _DataSource
}
@override
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
padding: const EdgeInsets.all(3),
children: [
PaginatedDataTable(
header: const Text(
'HCP RANKING',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18.0,
fontStyle: FontStyle.normal),
),
showFirstLastButtons: true,
showEmptyRows: false,
showCheckboxColumn: true,
actions: [
IconButton(
onPressed: () {
_createExcel();
},
icon: const Icon(Icons.download,
color: Color.fromARGB(255, 0, 71, 132)))
],
rowsPerPage: 5,
columns: const [
DataColumn(
2024-05-21 08:40:44 +00:00
label: FittedBox(
fit: BoxFit.scaleDown,
child: Text('Name',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14.0,
fontStyle: FontStyle.normal)),
)),
2024-05-20 10:29:02 +00:00
DataColumn(
2024-05-21 08:40:44 +00:00
label: FittedBox(
fit: BoxFit.scaleDown,
child: Text('Tier',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14.0,
fontStyle: FontStyle.normal)),
)),
2024-05-20 10:29:02 +00:00
DataColumn(
2024-05-21 08:40:44 +00:00
label: FittedBox(
fit: BoxFit.scaleDown,
child: Text('Rank',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14.0,
fontStyle: FontStyle.normal)),
)),
2024-05-20 10:29:02 +00:00
DataColumn(
2024-05-21 08:40:44 +00:00
label: FittedBox(
fit: BoxFit.scaleDown,
child: Text('Score',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14.0,
fontStyle: FontStyle.normal)),
)),
2024-05-20 10:29:02 +00:00
DataColumn(
2024-05-21 08:40:44 +00:00
label: FittedBox(
fit: BoxFit.scaleDown,
child: Text('Event',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14.0,
fontStyle: FontStyle.normal)),
)),
2024-05-20 10:29:02 +00:00
DataColumn(
2024-05-21 08:40:44 +00:00
label: FittedBox(
fit: BoxFit.scaleDown,
child: Text('Affliations',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14.0,
fontStyle: FontStyle.normal)),
)),
2024-05-20 10:29:02 +00:00
DataColumn(
2024-05-21 08:40:44 +00:00
label: FittedBox(
fit: BoxFit.scaleDown,
child: Text('Publications',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14.0,
fontStyle: FontStyle.normal)),
)),
2024-05-20 10:29:02 +00:00
DataColumn(
2024-05-21 08:40:44 +00:00
label: FittedBox(
fit: BoxFit.scaleDown,
child: Text('Trails',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14.0,
fontStyle: FontStyle.normal)),
)),
2024-05-20 10:29:02 +00:00
],
source: _dataSource,
),
],
),
floatingActionButton: Visibility(
visible: true,
child: FloatingBtn(
icon: Icons.add,
title: "add",
onTap: () {
List<String> selectedRowIds = _dataSource.getSelectedRowIds();
// Do something with selectedRowIds
print('Selected Row IDstrndsss: $selectedRowIds');
for (int i = 0; i < selectedRowIds.length; i++) {
print("checking_value_istrends: ${selectedRowIds[i]}");
// _contactbox.put(i, selectedRowIds[i]);
HiveFunctions.createUser({
"name": selectedRowIds[i],
"org": "Azienda Ospedaliera di Padova",
"adrr": "Via Giustiniani 2, Padova, Veneto 35128, Italy",
"phone": "+390498212410X12",
"Pphone": "+390498212410X12",
"email": "Gerosa,Gino@gmail.com",
"affno": "75",
"eveno": "8",
"pubno": "251",
"trailno": "1"
});
}
showDialog(
context: context,
builder: (_) {
2024-05-21 08:40:44 +00:00
return Alert(data: "User Added Successfully");
2024-05-20 10:29:02 +00:00
});
},
)),
);
}
Future<void> _createExcel() async {
//Create an Excel document.
//Creating a workbook.
final Workbook workbook = Workbook();
//Accessing via index
final Worksheet sheet = workbook.worksheets[0];
// sheet.getRangeByName('A1').setText('pooja');
/////////////////////////////////////////////////////////////////////////////
2024-05-21 08:40:44 +00:00
sheet.getRangeByName('A1').setText('Name');
sheet.getRangeByName('A2').setText('Gerosa, Gino');
sheet.getRangeByName('A3').setText('Gerosa, Gino');
sheet.getRangeByName('A4').setText('Gerosa, Gino');
2024-05-20 10:29:02 +00:00
final Style headingStyle = workbook.styles.add('HeadingStyle');
headingStyle.bold = true;
headingStyle.hAlign = HAlignType.center;
headingStyle.wrapText = true;
2024-05-21 08:40:44 +00:00
sheet.getRangeByName('B1').setText('Tier');
sheet.getRangeByName('B2').setText("Tier1");
sheet.getRangeByName('B3').setText("Tier1");
sheet.getRangeByName('B4').setText("Tier1");
sheet.getRangeByName('C1').setText('Rank');
sheet.getRangeByName('C2').setNumber(1);
sheet.getRangeByName('C3').setNumber(1);
sheet.getRangeByName('C4').setNumber(1);
sheet.getRangeByName('D1').setText('Score');
sheet.getRangeByName('D2').setNumber(0);
sheet.getRangeByName('D3').setNumber(0);
sheet.getRangeByName('D4').setNumber(0);
sheet.getRangeByName('E1').setText('Event');
sheet.getRangeByName('E2').setNumber(0);
sheet.getRangeByName('E3').setNumber(0);
sheet.getRangeByName('E4').setNumber(0);
sheet.getRangeByName('F1').setText('Affiliations');
sheet.getRangeByName('F2').setNumber(0);
sheet.getRangeByName('F3').setNumber(0);
sheet.getRangeByName('F4').setNumber(0);
sheet.getRangeByName('G1').setText('Publications');
sheet.getRangeByName('G2').setNumber(0);
sheet.getRangeByName('G3').setNumber(0);
sheet.getRangeByName('G4').setNumber(0);
sheet.getRangeByName('H1').setText('Trails');
sheet.getRangeByName('H2').setNumber(0);
sheet.getRangeByName('H3').setNumber(0);
sheet.getRangeByName('H4').setNumber(0);
2024-05-20 10:29:02 +00:00
//Defining a global style with properties.
final Style globalStyle = workbook.styles.add('globalStyle');
//globalStyle.backColor = '#37D8E9';
globalStyle.fontName = 'Times New Roman';
globalStyle.fontSize = 12;
// globalStyle.fontColor = '#C67878';
globalStyle.fontColor = '#C67878';
globalStyle.italic = true;
globalStyle.bold = true;
globalStyle.underline = true;
globalStyle.wrapText = true;
globalStyle.hAlign = HAlignType.center;
globalStyle.vAlign = VAlignType.center;
globalStyle.borders.all.lineStyle = LineStyle.thick;
// globalStyle.borders.all.color = '#9954CC';
final Style globalStyle1 = workbook.styles.add('globalStyle1');
globalStyle1.fontSize = 14;
// globalStyle1.fontColor = '#362191';
globalStyle1.hAlign = HAlignType.center;
globalStyle1.vAlign = VAlignType.center;
globalStyle1.borders.bottom.lineStyle = LineStyle.thin;
//globalStyle1.borders.bottom.color = '#829193';
globalStyle1.numberFormat = '0.00';
//Apply GlobalStyle
2024-05-21 08:40:44 +00:00
// sheet.getRangeByName('A1:D1').cellStyle = globalStyle;
2024-05-20 10:29:02 +00:00
2024-05-21 08:40:44 +00:00
// //Apply GlobalStyle1
// sheet.getRangeByName('B2:D4').cellStyle = globalStyle1;
2024-05-20 10:29:02 +00:00
//////////////////////////////////////////////////////////////////////////////
final List<int> bytes = workbook.saveSync();
print("hii");
if (kIsWeb) {
AnchorElement(
href:
'data:application/octet-stream;charset=utf-16le;base64,${base64.encode(bytes)}')
..setAttribute('download', 'Output.xlsx')
..click();
} else {
// final String path = (await getApplicationDocumentsDirectory()).path;
2024-05-20 11:46:56 +00:00
//final directory = await getExternalStorageDirectory();
Directory? directory = Platform.isAndroid
? await getExternalStorageDirectory()
: await getApplicationDocumentsDirectory();
2024-05-20 10:29:02 +00:00
final path = directory!.path;
final String filename =
Platform.isWindows ? '$path\\Output.xlsx' : '$path/Output.xlsx';
// final String filename = '$path/Output.xlsx';
final File file = File(filename);
await file.writeAsBytes(bytes, flush: true);
OpenFile.open(filename);
}
}
}
class HiveFunctions {
// String userHiveBox="User Box";
// Box which will use to store the things
static final _contactbox = Hive.box("mycontact");
static createUser(Map data) {
_contactbox.add(data);
}
static List getAllUsers() {
final data = _contactbox.keys.map((key) {
final value = _contactbox.get(key);
print("Checking_Name : $value");
return {
"name": value["name"],
"org": value["org"],
"adrr": value["adrr"],
"phone": value["phone"],
"Pphone": value["Pphone"],
"email": value["email"],
"affno": value["affno"],
"eveno": value["eveno"],
"pubno": value["pubno"],
"trailno": value["trailno"]
};
}).toList();
return data.reversed.toList();
}
static List getindexUsers() {
final data = _contactbox.keys.map((key) {
final value = _contactbox.get(key);
print("Checking_Name : $value");
return {
"name": value["name"],
"org": value["org"],
"adrr": value["adrr"],
"phone": value["phone"],
"Pphone": value["Pphone"],
"email": value["email"],
"affno": value["affno"],
"eveno": value["eveno"],
"pubno": value["pubno"],
"trailno": value["trailno"]
};
}).toList();
return data.reversed.toList();
}
}
class _Row {
_Row(this.identifier, this.valueA, this.valueC, this.valueD, this.valueE,
this.valueF, this.valueG, this.valueH, this.valueI);
final int identifier;
final String valueA;
final String valueC;
final String valueD;
final String valueE;
final String valueF;
final String valueG;
final String valueH;
final String valueI;
bool selected = false;
}
class _DataSource extends DataTableSource {
final BuildContext context;
late List<_Row> _rows;
// late List<int> _selectedRowsIndexes = []; // List to track selected rows
final List<String> _selectedRowIds = [];
_DataSource(this.context) {
_rows = <_Row>[
for (int i = 0; i < 20; i++)
_Row(i, 'Gerosa, Gino', 'Tier1', '1', '0', '0', '0', '0', '0'),
];
//}
}
List<String> getSelectedRowIds() {
return _selectedRowIds; // Return a copy to prevent direct modification
}
int _selectedCount = 0;
@override
DataRow? getRow(int index) {
assert(index >= 0);
if (index >= _rows.length) return null;
final row = _rows[index];
return DataRow.byIndex(
index: index,
selected: row.selected,
onSelectChanged: (value) {
if (row.selected != value) {
// _selectedCount += value! ? 1 : -1;
// assert(_selectedCount >= 0);
row.selected = value!;
if (value) {
print("Selected");
_selectedRowIds.add(row.valueA); // Add the row ID to the list
print("Selected_selectedRowIds :$_selectedRowIds");
} else {
_selectedRowIds
.remove(row.valueA); // Remove the row ID from the list
}
notifyListeners();
print("Selectedddd_is $value");
print("Selectedddd_value ${row.valueA}");
}
},
cells: [
DataCell(
Text(row.valueA),
onTap: () {
print("hiii Data cell tap ${row.valueA}");
Navigator.push(
context, MaterialPageRoute(builder: (context) => Profile()));
},
),
DataCell(Text(row.valueC)),
DataCell(Text(row.valueD.toString())),
DataCell(Text(row.valueE)),
DataCell(Text(row.valueF)),
DataCell(Text(row.valueG)),
DataCell(Text(row.valueH)),
DataCell(Text(row.valueI)),
],
);
}
@override
int get rowCount => _rows.length;
@override
bool get isRowCountApproximate => false;
@override
int get selectedRowCount => _selectedCount;
}