workouttest_app/lib/view/account.dart
2020-11-20 13:31:26 +01:00

207 lines
7.3 KiB
Dart

import 'package:aitrainer_app/bloc/account/account_bloc.dart';
import 'package:aitrainer_app/localization/app_language.dart';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/customer.dart';
import 'package:aitrainer_app/util/trans.dart';
import 'package:aitrainer_app/widgets/app_bar_min.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:aitrainer_app/widgets/bottom_nav.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
// ignore: must_be_immutable
class AccountPage extends StatelessWidget with Trans {
// ignore: close_sinks
AccountBloc accountBloc;
@override
Widget build(BuildContext context) {
setContext(context);
accountBloc = BlocProvider.of<AccountBloc>(context);
return Scaffold(
appBar: AppBarMin(),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('asset/image/WT_light_background.png'),
fit: BoxFit.cover,
alignment: Alignment.center,
),
),
child: BlocConsumer<AccountBloc, AccountState>(listener: (context, state) {
if (state is AccountError) {
Scaffold.of(context).showSnackBar(
SnackBar(backgroundColor: Colors.orange, content: Text(state.message, style: TextStyle(color: Colors.white))));
} else if (state is AccountLoading) {}
}, builder: (context, state) {
if (state is AccountInitial) {
String customerName = accountBloc.customerRepository.firstName + " " + accountBloc.customerRepository.name;
return accountWidget(context, customerName, accountBloc);
} else if (state is AccountLoggedIn) {
String customerName = accountBloc.customerRepository.firstName + " " + accountBloc.customerRepository.name;
return accountWidget(context, customerName, accountBloc);
} else if (state is AccountLoggedOut) {
String customerName = "";
return accountWidget(context, customerName, accountBloc);
} else if (state is AccountReady) {
String customerName = accountBloc.customerRepository.firstName + " " + accountBloc.customerRepository.name;
return accountWidget(context, customerName, accountBloc);
} else {
return accountWidget(context, "", accountBloc);
}
}),
),
bottomNavigationBar: BottomNavigator(bottomNavIndex: 3));
}
ListView accountWidget(BuildContext context, String customerName, AccountBloc accountBloc) {
return ListView(padding: EdgeInsets.only(top: 35), children: <Widget>[
ListTile(
leading: Icon(Icons.perm_identity),
subtitle: Text(t("Profile")),
title: FlatButton(
child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
Text(customerName, style: TextStyle(color: Colors.blue)),
Icon(Icons.arrow_forward_ios),
]),
textColor: Colors.grey,
color: Colors.white,
onPressed: () => {
if (accountBloc.customerRepository.customer != null && Cache().userLoggedIn != null)
{
Navigator.of(context).pushNamed('customerModifyPage'),
}
},
),
),
loginOut(context, accountBloc),
getMyTrainees(context, accountBloc),
]);
}
ListTile loginOut(BuildContext context, AccountBloc accountBloc) {
ListTile element = ListTile();
String text = "Logout";
Color buttonColor = Colors.orange;
if (accountBloc.customerRepository.customer == null || accountBloc.customerRepository.customer.email == null) {
text = "Login";
buttonColor = Colors.blue;
}
element = ListTile(
enabled: true,
leading: Icon(Icons.input),
title: FlatButton(
child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
Text(t(text), style: TextStyle(color: buttonColor)),
Icon(Icons.arrow_forward_ios),
]),
textColor: buttonColor,
color: Colors.white,
onPressed: () => {
if (accountBloc.loggedIn)
{
confirmationDialog(accountBloc),
}
else
{
accountBloc.add(AccountLogin()),
Navigator.of(context).pushNamed('login'),
}
},
),
);
return element;
}
Widget getMyTrainees(BuildContext context, AccountBloc accountBloc) {
if (accountBloc.customerRepository.customer == null) {
return Container();
}
if (accountBloc.customerRepository.customer.trainer == 0) {
return ListTile(
title: Container(),
);
}
if (accountBloc.customerRepository.getTraineesList() == null) {
return ListTile(
leading: Icon(Icons.people),
title: RaisedButton(
color: Colors.white70,
onPressed: () => accountBloc.add(AccountGetTrainees()),
child: Text("See my trainees"),
),
);
}
List<Widget> elements = List<Widget>();
accountBloc.customerRepository.getTraineesList().forEach((element) {
Customer trainee = element;
String name = trainee.name;
String firstName = trainee.firstname;
String nodeName = AppLanguage().appLocal == Locale("en") ? firstName + " " + name : name + " " + firstName;
bool selected = accountBloc.traineeId == trainee.customerId;
Widget widget = FlatButton(
padding: EdgeInsets.all(10),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8)),
side: BorderSide(width: 2, color: selected ? Colors.blue : Colors.black26),
),
onPressed: () {
accountBloc.add(AccountSelectTrainee(traineeId: trainee.customerId));
//Navigator.of(context).pushNamed('login');
},
child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
Text(
nodeName,
style: TextStyle(color: selected ? Colors.blue : Colors.black54, fontWeight: selected ? FontWeight.bold : FontWeight.normal),
),
Icon(Icons.arrow_forward_ios),
]),
);
elements.add(widget);
});
return ListTile(
leading: Icon(Icons.people),
subtitle: Text("My Trainees"),
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: elements,
));
}
void confirmationDialog(AccountBloc accountBloc) {
showCupertinoDialog(
useRootNavigator: true,
context: context,
//barrierDismissible: false,
builder: (_) => CupertinoAlertDialog(
title: Text(t("Are you sure to logout?")),
content: Column(children: [
Divider(),
]),
actions: [
FlatButton(
child: Text(t("No")),
onPressed: () => Navigator.pop(context),
),
FlatButton(
child: Text(t("Yes")),
onPressed: () => {
accountBloc.add(AccountLogout()),
Navigator.pop(context),
},
)
],
));
}
}