This repository has been archived on 2024-03-27. You can view files and clone it, but cannot push or open issues/pull-requests.
encrateia/lib/widgets/athlete_widgets/athlete_tag_group_widget.dart

142 lines
4.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_material_color_picker/flutter_material_color_picker.dart';
import '/models/athlete.dart';
import '/models/tag_group.dart';
import '/screens/add_tag_group_screen.dart';
import '/screens/show_tag_group_screen.dart';
import '/utils/icon_utils.dart';
import '/utils/my_button.dart';
class AthleteTagGroupWidget extends StatefulWidget {
const AthleteTagGroupWidget({this.athlete});
final Athlete athlete;
@override
_AthleteTagGroupWidgetState createState() => _AthleteTagGroupWidgetState();
}
class _AthleteTagGroupWidgetState extends State<AthleteTagGroupWidget> {
List<TagGroup> tagGroups;
int offset = 0;
int rows;
bool loading = true;
@override
void initState() {
getData();
super.initState();
}
@override
Widget build(BuildContext context) {
if (tagGroups != null) {
rows = (tagGroups.length < 8) ? tagGroups.length : 8;
return ListView(
children: <Widget>[
Center(
child: Text(
'\nTag Groups ${offset + 1} - ${offset + rows} '
'of ${tagGroups.length}',
style: Theme.of(context).textTheme.headline6,
),
),
DataTable(
headingRowHeight: kMinInteractiveDimension * 0.80,
dataRowHeight: kMinInteractiveDimension * 0.80,
columnSpacing: 9,
columns: const <DataColumn>[
DataColumn(label: Text('Name')),
DataColumn(label: Text('Color')),
DataColumn(label: Text('Edit')),
],
rows: tagGroups
.sublist(offset, offset + rows)
.map((TagGroup tagGroup) {
return DataRow(
key: ValueKey<int>(tagGroup.id),
cells: <DataCell>[
DataCell(Text(tagGroup.name)),
DataCell(CircleColor(
circleSize: 20,
elevation: 0,
color: Color(tagGroup.color),
)),
DataCell(
tagGroup.system ? MyIcon.show : MyIcon.edit,
onTap: () async {
await Navigator.push(
context,
MaterialPageRoute<BuildContext>(
builder: (BuildContext context) {
if (tagGroup.system) {
return ShowTagGroupScreen(tagGroup: tagGroup);
} else {
return AddTagGroupScreen(tagGroup: tagGroup);
}
},
),
);
getData();
},
)
],
);
}).toList(),
),
const SizedBox(height: 20),
Row(
children: <Widget>[
const Spacer(),
MyButton.add(
child: const Text('New tag group'),
onPressed: () async {
await Navigator.push(
context,
MaterialPageRoute<BuildContext>(
builder: (BuildContext context) => AddTagGroupScreen(
tagGroup: TagGroup(athlete: widget.athlete),
),
),
);
getData();
}),
const Spacer(),
MyButton.navigate(
child: const Text('<<'),
onPressed: (offset == 0)
? null
: () => setState(() {
offset > 8 ? offset = offset - rows : offset = 0;
}),
),
const Spacer(),
MyButton.navigate(
child: const Text('>>'),
onPressed: (offset + rows == tagGroups.length)
? null
: () => setState(() {
offset + rows < tagGroups.length - rows
? offset = offset + rows
: offset = tagGroups.length - rows;
}),
),
const Spacer(),
],
),
],
);
} else {
return Center(
child: Text(loading ? 'Loading' : 'No data found'),
);
}
}
Future<void> getData() async {
tagGroups = await widget.athlete.tagGroups;
setState(() => loading = false);
}
}