Created
May 12, 2020 06:10
-
-
Save erluxman/f1bcb6e2309aa8446363c6bf05928f1a to your computer and use it in GitHub Desktop.
Datatable fluttter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
StreamController<bool> isLightTheme = StreamController(); | |
main() { | |
runApp(MainApp()); | |
} | |
class MainApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return StreamBuilder<bool>( | |
initialData: true, | |
stream: isLightTheme.stream, | |
builder: (context, snapshot) { | |
return MaterialApp( | |
theme: snapshot.data ? ThemeData.light() : ThemeData.dark(), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
appBar: AppBar(title: Text("Player Goals")), | |
body: DataTableWidget())); | |
}); | |
} | |
} | |
class DataTableWidget extends StatefulWidget { | |
@override | |
_DataTableWidgetState createState() => _DataTableWidgetState(); | |
} | |
class _DataTableWidgetState extends State<DataTableWidget> { | |
var names = [ | |
Person("Ronado", 600, | |
"https://metro.co.uk/wp-content/uploads/2017/02/533494160.jpg"), | |
Person("Cr7", 900, | |
"https://upload.wikimedia.org/wikipedia/commons/8/8c/Cristiano_Ronaldo_2018.jpg"), | |
Person("Messi", 900, | |
"https://upload.wikimedia.org/wikipedia/commons/9/92/NIG-ARG_%286%29.jpg"), | |
Person("Zidane", 200, | |
"https://upload.wikimedia.org/wikipedia/commons/f/f3/Zinedine_Zidane_by_Tasnim_03.jpg"), | |
Person("Platini", 500, "https://im.rediff.com/sports/2006/may/15plat.jpg"), | |
Person("Pele", 1000, | |
"https://upload.wikimedia.org/wikipedia/commons/0/09/Pel%C3%A9_%C3%A0_la_Coupe_du_monde_de_football_1970%2C_%27Mexico_70_-_World_Cup_Story%27%2C_Panini_figurina_n%C2%B038.jpg"), | |
Person("Cruyff", 400, | |
"https://www.globalresearch.ca/wp-content/uploads/2016/03/Johan-Cruyff.jpg"), | |
Person("Kaka", 300, | |
"https://cdn.britannica.com/87/139487-050-98D3449D/Kaka-2009.jpg") | |
]; | |
@override | |
Widget build(BuildContext context) { | |
return DataTable( | |
sortColumnIndex: 2, | |
showCheckboxColumn: true, | |
columns: [ | |
DataColumn(label: Text("")), | |
DataColumn( | |
label: Text("Name"), | |
onSort: (index, ascending) { | |
setState(() { | |
names.sort((a, b) { | |
return ascending | |
? a.name.compareTo(b.name) | |
: b.name.compareTo(a.name); | |
}); | |
}); | |
}), | |
DataColumn( | |
label: Text("Goals"), | |
numeric: true, | |
onSort: (index, ascending) { | |
setState(() { | |
names.sort((a, b) { | |
return ascending | |
? a.goals.compareTo(b.goals) | |
: b.goals.compareTo(a.goals); | |
}); | |
}); | |
}) | |
], | |
rows: names.map((player) { | |
return DataRow( | |
cells: [ | |
DataCell( | |
ClipRRect( | |
child: Image.network( | |
player.imageUrl, | |
height: 40, | |
width: 40, | |
fit: BoxFit.cover, | |
), | |
borderRadius: BorderRadius.circular(200), | |
), | |
), | |
DataCell( | |
Text(player.name), | |
), | |
DataCell(Text(player.goals.toString())) | |
], | |
); | |
}).toList()); | |
} | |
} | |
// | |
//class _DatatableWidgetState extends State<DatatableWidget> { | |
// | |
// | |
// @override | |
// Widget build(BuildContext context) { | |
// print("Hello world"); | |
// Timeline.startSync('interesting function'); | |
// var widget = Column( | |
// children: <Widget>[ | |
// Padding( | |
// padding: const EdgeInsets.all(24.0), | |
// child: Text( | |
// "Order Players in terms of goals scored", | |
// style: Theme.of(context).textTheme.headline, | |
// textAlign: TextAlign.center, | |
// ), | |
// ), | |
// Expanded( | |
// flex: 1, | |
// child: ReorderableListView( | |
// padding: EdgeInsets.all(8), | |
// onReorder: (oldIndex, newIndex) { | |
// setState(() { | |
// if (oldIndex < newIndex) { | |
// newIndex -= 1; | |
// } | |
// var previous = names[oldIndex]; | |
// names.removeAt(oldIndex); | |
// names.insert(newIndex, previous); | |
// }); | |
// }, | |
// children: names.map((person) { | |
// return Card( | |
// key: ObjectKey(person), | |
// child: Padding( | |
// padding: const EdgeInsets.all(16.0), | |
// child: Row( | |
// mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
// children: [ | |
// ClipRRect( | |
// child: Image.network( | |
// person.imageUrl, | |
// height: 80, | |
// width: 80, | |
// fit: BoxFit.cover, | |
// ), | |
// borderRadius: BorderRadius.circular(200), | |
// ), | |
// Text( | |
// person.name, | |
// textAlign: TextAlign.start, | |
// style: TextStyle( | |
// fontSize: 20, fontWeight: FontWeight.w700), | |
// ), | |
// Icon(Icons.reorder), | |
// ]), | |
// ), | |
// ); | |
// }).toList()), | |
// ), | |
// ], | |
// ); | |
// Timeline.finishSync(); | |
// | |
// return widget; | |
// } | |
//} | |
class Person { | |
const Person(this.name, this.goals, this.imageUrl); | |
final int goals; | |
final String name; | |
final String imageUrl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment