workouttest_app/lib/view/settings.dart
2021-03-28 12:45:14 +02:00

135 lines
5.1 KiB
Dart

import 'package:aitrainer_app/bloc/menu/menu_bloc.dart';
import 'package:aitrainer_app/bloc/settings/settings_bloc.dart';
import 'package:aitrainer_app/library/custom_icon_icons.dart';
import 'package:aitrainer_app/util/app_language.dart';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/util/common.dart';
import 'package:aitrainer_app/util/enums.dart';
import 'package:aitrainer_app/util/track.dart';
import 'package:aitrainer_app/util/trans.dart';
import 'package:aitrainer_app/widgets/app_bar_min.dart';
import 'package:aitrainer_app/widgets/bottom_nav.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';
import 'package:toggle_switch/toggle_switch.dart';
// ignore: must_be_immutable
class SettingsPage extends StatelessWidget with Trans {
@override
Widget build(BuildContext context) {
// ignore: close_sinks
SettingsBloc settingsBloc = BlocProvider.of<SettingsBloc>(context);
settingsBloc.setLocale(AppLanguage().appLocal);
settingsBloc.context = context;
setContext(context);
MenuBloc menuBloc = BlocProvider.of<MenuBloc>(context);
return Scaffold(
appBar: AppBarMin(),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('asset/image/WT_light_background.jpg'),
fit: BoxFit.cover,
alignment: Alignment.center,
),
),
child: Form(
child: BlocConsumer<SettingsBloc, SettingsState>(listener: (context, state) {
if (state is SettingsError) {
Scaffold.of(context).showSnackBar(
SnackBar(backgroundColor: Colors.orange, content: Text(state.message, style: TextStyle(color: Colors.white))));
} else if (state is SettingsReady) {
menuBloc.add(MenuRecreateTree());
}
}, builder: (context, state) {
return ModalProgressHUD(
child: settingsUI(context, settingsBloc, menuBloc),
inAsyncCall: state is SettingsLoading,
opacity: 0.5,
color: Colors.black54,
progressIndicator: CircularProgressIndicator(),
);
}),
),
),
bottomNavigationBar: BottomNavigator(bottomNavIndex: 4));
}
ListView settingsUI(BuildContext context, SettingsBloc settingsBloc, MenuBloc menuBloc) {
return ListView(padding: EdgeInsets.only(top: 150), children: <Widget>[
ListTile(
leading: Icon(Icons.language),
subtitle: Text(t("Change App Language")),
title: DropdownButton(
value: settingsBloc.getLocale() == Locale('en') ? t("English") : t("Hungarian"),
items: [t("English"), t("Hungarian")].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String lang) => {
settingsBloc.add(SettingsChangeLanguage(language: lang)),
Track().track(TrackingEvent.settings_lang, eventValue: lang)
})),
getServer(settingsBloc),
//getDevice(settingsBloc),
]);
}
ListTile getServer(SettingsBloc settingsBloc) {
if (Cache().userLoggedIn == null || Cache().userLoggedIn.admin != 1) {
return ListTile(
title: Container(),
);
}
print("Live: ${Cache().liveServer}");
return ListTile(
leading: Icon(Icons.data_usage_sharp),
subtitle: Text("For Test purpuses select Test-Server. After that please restart the the App"),
title: ToggleSwitch(
minWidth: 120.0,
minHeight: 30.0,
fontSize: 14.0,
initialLabelIndex: Cache().liveServer
? Cache().testEnvironment == "1"
? 1
: 0
: 1,
activeBgColor: Colors.indigo,
activeFgColor: Colors.white,
inactiveBgColor: Colors.white60,
inactiveFgColor: Colors.grey[900],
labels: [t('Live-Server'), t('Test-Server')],
onToggle: (index) {
settingsBloc.add(SettingsSetServer(live: index == 0));
},
),
);
}
ListTile getDevice(SettingsBloc settingsBloc) {
return ListTile(
leading: Common.badgedIcon(Colors.grey, CustomIcon.cog, "hardware"),
subtitle: Text("Do you have Smart watch, or any device which collects the fit/health data?"),
title: ToggleSwitch(
minWidth: 120.0,
minHeight: 30.0,
fontSize: 14.0,
initialLabelIndex: Cache().hasHardware ? 0 : 1,
activeBgColor: Colors.indigo,
activeFgColor: Colors.white,
inactiveBgColor: Colors.white60,
inactiveFgColor: Colors.grey[900],
labels: [t('Yes'), t('No')],
onToggle: (index) {
settingsBloc.add(SettingsSetHardware(hasHardware: index == 0));
},
),
);
}
}