Refactor the code
This commit is contained in:
parent
d34a0d4b32
commit
73ce25ca52
3 changed files with 111 additions and 160 deletions
|
@ -37,18 +37,14 @@ class SettingsScreen extends StatelessWidget {
|
|||
SizedBox(
|
||||
height: 50,
|
||||
),
|
||||
Visibility(
|
||||
child: CircularProgressIndicator(),
|
||||
visible: github.isSyncInProgress),
|
||||
Visibility(
|
||||
child: buildSyncErrors(context),
|
||||
visible: github.syncErrors.length > 0,
|
||||
)
|
||||
if (github.isSyncInProgress)
|
||||
CircularProgressIndicator(),
|
||||
if (github.syncErrors.length > 0)
|
||||
buildSyncErrors(context),
|
||||
]),
|
||||
actions: <Widget>[
|
||||
Visibility(
|
||||
visible: !github.isSyncInProgress,
|
||||
child: FlatButton(
|
||||
if (!github.isSyncInProgress)
|
||||
FlatButton(
|
||||
child: Text('Close',
|
||||
style: TextStyle(color: Colors.blue)),
|
||||
onPressed: () {
|
||||
|
@ -58,7 +54,6 @@ class SettingsScreen extends StatelessWidget {
|
|||
Navigator.popAndPushNamed(context, '/');
|
||||
}
|
||||
},
|
||||
),
|
||||
)
|
||||
]);
|
||||
});
|
||||
|
@ -72,14 +67,11 @@ class SettingsScreen extends StatelessWidget {
|
|||
}
|
||||
|
||||
static Widget buildSyncErrors(BuildContext context) {
|
||||
List<Widget> syncErrors = new List();
|
||||
|
||||
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
|
||||
// TODO: Make it pretty
|
||||
github.syncErrors.forEach((error) {
|
||||
syncErrors.add(Text(error, style: TextStyle(color: Colors.red)));
|
||||
});
|
||||
|
||||
return Column(children: syncErrors);
|
||||
return Column(children: [
|
||||
for (var error in github.syncErrors)
|
||||
Text(error, style: TextStyle(color: Colors.red))
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,10 +37,16 @@ class WarbandRosterScreen extends StatelessWidget {
|
|||
child: SingleChildScrollView(child: WarbandDrawerWidget())));
|
||||
}
|
||||
|
||||
List<Widget> tiles = new List();
|
||||
|
||||
github.activeRoster.heros.forEach((hero) {
|
||||
tiles.add(new ListTile(
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(github.activeRoster.name),
|
||||
),
|
||||
drawer:
|
||||
Drawer(child: SingleChildScrollView(child: WarbandDrawerWidget())),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(children: [
|
||||
for (var hero in github.activeRoster.heros)
|
||||
ListTile(
|
||||
title: Text(hero.name),
|
||||
leading: CircleAvatar(
|
||||
child: Text(hero.experience.toString()),
|
||||
|
@ -48,13 +54,10 @@ class WarbandRosterScreen extends StatelessWidget {
|
|||
foregroundColor: Colors.greenAccent,
|
||||
),
|
||||
subtitle: Text(hero.type),
|
||||
));
|
||||
});
|
||||
|
||||
tiles.add(Divider());
|
||||
|
||||
github.activeRoster.henchmenGroups.forEach((henchmenGroup) {
|
||||
tiles.add(new ListTile(
|
||||
),
|
||||
Divider(),
|
||||
for (var henchmenGroup in github.activeRoster.henchmenGroups)
|
||||
ListTile(
|
||||
title: Text(henchmenGroup.name),
|
||||
trailing: Chip(label: Text(henchmenGroup.number.toString() + 'x')),
|
||||
leading: CircleAvatar(
|
||||
|
@ -63,16 +66,8 @@ class WarbandRosterScreen extends StatelessWidget {
|
|||
foregroundColor: Colors.white,
|
||||
),
|
||||
subtitle: Text(henchmenGroup.type),
|
||||
));
|
||||
});
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(github.activeRoster.name),
|
||||
),
|
||||
drawer:
|
||||
Drawer(child: SingleChildScrollView(child: WarbandDrawerWidget())),
|
||||
body: SingleChildScrollView(child: Column(children: tiles)),
|
||||
)
|
||||
])),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
|
|||
import 'package:provider/provider.dart';
|
||||
import 'package:badges/badges.dart';
|
||||
import 'package:toolheim/data/github_adapter.dart';
|
||||
import 'package:toolheim/data/warband_roster.dart';
|
||||
import 'package:toolheim/screens/settings_screen.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
|
@ -11,21 +10,16 @@ class WarbandDrawerWidget extends StatelessWidget {
|
|||
Widget build(BuildContext context) {
|
||||
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
|
||||
|
||||
// No settings at all
|
||||
String settingsText =
|
||||
'There is no repository set up. Please open the settings and provide a valid GitHub repository.';
|
||||
|
||||
// Never fetched any data
|
||||
if (github.activeRoster == null) {
|
||||
settingsText =
|
||||
'The repository is set, but no warbands are found. Try to search for warbands on the settings screen.';
|
||||
}
|
||||
|
||||
if (github.repository == null || github.activeRoster == null) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 100, left: 30, right: 30),
|
||||
child: Column(children: <Widget>[
|
||||
Text(settingsText),
|
||||
if (github.activeRoster == null)
|
||||
Text(
|
||||
'The repository is set, but no warbands are found. Try to search for warbands on the settings screen.')
|
||||
else
|
||||
Text(
|
||||
'There is no repository set up. Please open the settings and provide a valid GitHub repository.'),
|
||||
FlatButton(
|
||||
onPressed: () {
|
||||
Navigator.popAndPushNamed(context, '/settings');
|
||||
|
@ -38,12 +32,7 @@ class WarbandDrawerWidget extends StatelessWidget {
|
|||
Text(
|
||||
'If you have no clue what this app is all about, open the help screen and read the introduction.'),
|
||||
FlatButton(
|
||||
onPressed: () async {
|
||||
const url = '';
|
||||
if (await canLaunch(url)) {
|
||||
await launch(url);
|
||||
}
|
||||
},
|
||||
onPressed: openHelpWebsite,
|
||||
child: Text(
|
||||
'Help',
|
||||
style: TextStyle(color: Colors.blue),
|
||||
|
@ -53,18 +42,8 @@ class WarbandDrawerWidget extends StatelessWidget {
|
|||
);
|
||||
}
|
||||
|
||||
return buildRosterList(context);
|
||||
}
|
||||
|
||||
Widget buildRosterList(BuildContext context) {
|
||||
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
|
||||
WarbandRoster activeroster = github.activeRoster;
|
||||
List<WarbandRoster> rosters = github.rosters;
|
||||
|
||||
List<Widget> tiles = [];
|
||||
|
||||
// Show some stats for the own warband
|
||||
tiles.add(UserAccountsDrawerHeader(
|
||||
return Column(children: <Widget>[
|
||||
UserAccountsDrawerHeader(
|
||||
margin: const EdgeInsets.all(0),
|
||||
otherAccountsPictures: <Widget>[
|
||||
IconButton(
|
||||
|
@ -74,12 +53,7 @@ class WarbandDrawerWidget extends StatelessWidget {
|
|||
tooltip: 'Refresh warbands',
|
||||
onPressed: () async {
|
||||
await github.update();
|
||||
|
||||
if (github.syncErrors.length > 0) {
|
||||
List<Widget> errors = github.syncErrors.map((error) {
|
||||
return Text(error);
|
||||
}).toList();
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
|
@ -104,38 +78,24 @@ class WarbandDrawerWidget extends StatelessWidget {
|
|||
icon: Icon(Icons.help),
|
||||
color: Colors.white,
|
||||
highlightColor: Colors.brown,
|
||||
onPressed: () {
|
||||
Navigator.popAndPushNamed(context, '/help');
|
||||
},
|
||||
),
|
||||
onPressed: openHelpWebsite),
|
||||
],
|
||||
accountName: Text(activeroster.name),
|
||||
accountEmail: Text(activeroster.race),
|
||||
));
|
||||
|
||||
tiles.add(Visibility(
|
||||
visible: github.isSyncInProgress, child: LinearProgressIndicator()));
|
||||
|
||||
rosters.forEach((roster) {
|
||||
// We mark inactive warbands with a gray acent
|
||||
var textColor = Colors.black;
|
||||
if (!roster.active) {
|
||||
textColor = Colors.black45;
|
||||
}
|
||||
|
||||
var fontWeight = FontWeight.normal;
|
||||
if (roster.unseen) {
|
||||
fontWeight = FontWeight.bold;
|
||||
}
|
||||
|
||||
tiles.add(ListTile(
|
||||
accountName: Text(github.activeRoster.name),
|
||||
accountEmail: Text(github.activeRoster.race),
|
||||
),
|
||||
if (github.isSyncInProgress) LinearProgressIndicator(),
|
||||
for (var roster in github.rosters)
|
||||
ListTile(
|
||||
onTap: () {
|
||||
roster.unseen = false;
|
||||
github.activeRoster = roster;
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
title: Text(roster.name + ' (' + roster.playerName + ')',
|
||||
style: TextStyle(color: textColor, fontWeight: fontWeight)),
|
||||
style: TextStyle(
|
||||
color: !roster.active ? Colors.black45 : Colors.black,
|
||||
fontWeight: roster.unseen ? FontWeight.bold : FontWeight.normal,
|
||||
)),
|
||||
subtitle: Text(roster.version.message),
|
||||
isThreeLine: true,
|
||||
trailing: Badge(
|
||||
|
@ -145,11 +105,15 @@ class WarbandDrawerWidget extends StatelessWidget {
|
|||
badgeContent: Text(roster.campaignPoints.toString() + ' CP',
|
||||
style: TextStyle(color: Colors.white)),
|
||||
),
|
||||
));
|
||||
});
|
||||
),
|
||||
Divider()
|
||||
]);
|
||||
}
|
||||
|
||||
tiles.add(Divider());
|
||||
|
||||
return Column(children: tiles);
|
||||
void openHelpWebsite() async {
|
||||
const url = 'https://google.com/';
|
||||
if (await canLaunch(url)) {
|
||||
await launch(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue