geo-smart-app/lib/page/setting.dart

166 lines
5.3 KiB
Dart
Raw Normal View History

2019-12-16 00:18:09 +00:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2020-01-03 23:36:33 +00:00
import 'package:fluttertoast/fluttertoast.dart';
import 'package:geosmart/config.dart';
import 'package:geosmart/model/setting.dart';
import 'package:geosmart/page/map.dart';
import 'package:geosmart/service/setting_service.dart';
import 'package:geosmart/service/unique_id_service.dart';
2019-12-16 00:18:09 +00:00
class Setting extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new _SettingState();
}
}
class _SettingState extends State<Setting> {
final _hostController = TextEditingController();
SettingService _settingBloc;
UniqueIDService _uniqueIDBloc;
2019-12-16 00:18:09 +00:00
String id;
String host;
@override
void initState() {
_settingBloc = new SettingService();
2019-12-16 00:18:09 +00:00
if (!Config.dynamicHostSetting) {
_settingBloc.setSetting(new SettingModel(Config.api, null));
}
_settingBloc.getSetting().then((settingModel) {
if (!settingModel.isNullId()) {
this.id = settingModel.id;
}
if (!settingModel.isNullHost()) {
this.host = settingModel.host;
}
2019-12-16 00:18:09 +00:00
_hostController.text = this.host;
2020-01-03 23:36:33 +00:00
if (!settingModel.isNullHost()) {
_uniqueIDBloc = new UniqueIDService(settingModel);
2019-12-16 00:18:09 +00:00
2020-01-03 23:36:33 +00:00
if (_uniqueIDBloc != null) {
this._uniqueIDBloc.getUniqueID().then((uniqueId) {
2020-01-03 23:36:33 +00:00
print("Your Unique ID " + uniqueId.id.toString());
if (uniqueId.id != null && uniqueId.id != "") {
if (!settingModel.isNullId()) {
mapPage();
} else {
this._settingBloc.setSetting(
new SettingModel(this._hostController.text, uniqueId.id),
);
mapPage();
}
} else {
FlutterToast.showToast(msg: "Invalid host address");
}
2020-01-03 23:36:33 +00:00
});
}
2019-12-16 00:18:09 +00:00
}
});
super.initState();
}
2020-01-03 23:36:33 +00:00
mapPage() {
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (BuildContext context) => Map(),
));
}
2019-12-16 00:18:09 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: Container(
child: Column(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/server.png"),
),
),
height: 200,
),
SizedBox(
height: 30,
),
TextField(
controller: this._hostController,
decoration: InputDecoration(
labelText: "Host",
border: OutlineInputBorder(),
),
),
2020-01-03 23:36:33 +00:00
SizedBox(
height: 10.0,
),
Container(
width: double.infinity,
child: FlatButton(
color: Colors.blueAccent,
onPressed: () {
this
._settingBloc
.setSetting(
2020-01-03 23:36:33 +00:00
new SettingModel(this._hostController.text, null),
)
.then((settingModel) {
if (!settingModel.isNullId()) {
this.id = settingModel.id;
}
if (!settingModel.isNullHost()) {
this.host = settingModel.host;
}
_hostController.text = this.host;
if (!settingModel.isNullHost()) {
_uniqueIDBloc = new UniqueIDService(settingModel);
if (_uniqueIDBloc != null) {
this._uniqueIDBloc.getUniqueID().then((uniqueId) {
print("Your Unique ID " + uniqueId.id.toString());
if (uniqueId.id != null && uniqueId.id != "") {
if (!settingModel.isNullId()) {
mapPage();
} else {
this._settingBloc.setSetting(
new SettingModel(
this._hostController.text,
uniqueId.id,
),
);
mapPage();
}
} else {
FlutterToast.showToast(
msg: "Invalid host address",
);
}
});
}
}
});
2020-01-03 23:36:33 +00:00
},
child: Text(
"Save",
style: TextStyle(color: Colors.white),
),
),
2019-12-16 00:18:09 +00:00
)
],
),
width: 200,
height: 400,
),
),
),
);
}
}