3
0
Fork 0
trifid_mobile/lib/screens/siteConfig/UnsafeRoutesScreen.dart

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
3.0 KiB
Dart
Raw Permalink Normal View History

2020-07-27 20:43:58 +00:00
import 'package:flutter/cupertino.dart';
import 'package:mobile_nebula/components/FormPage.dart';
import 'package:mobile_nebula/components/config/ConfigButtonItem.dart';
import 'package:mobile_nebula/components/config/ConfigPageItem.dart';
import 'package:mobile_nebula/components/config/ConfigSection.dart';
import 'package:mobile_nebula/models/UnsafeRoute.dart';
import 'package:mobile_nebula/screens/siteConfig/UnsafeRouteScreen.dart';
import 'package:mobile_nebula/services/utils.dart';
class UnsafeRoutesScreen extends StatefulWidget {
const UnsafeRoutesScreen({
Key? key,
required this.unsafeRoutes,
required this.onSave,
}) : super(key: key);
2020-07-27 20:43:58 +00:00
final List<UnsafeRoute> unsafeRoutes;
final ValueChanged<List<UnsafeRoute>>? onSave;
2020-07-27 20:43:58 +00:00
@override
_UnsafeRoutesScreenState createState() => _UnsafeRoutesScreenState();
}
class _UnsafeRoutesScreenState extends State<UnsafeRoutesScreen> {
late Map<Key, UnsafeRoute> unsafeRoutes;
2020-07-27 20:43:58 +00:00
bool changed = false;
@override
void initState() {
unsafeRoutes = {};
2020-07-27 20:43:58 +00:00
widget.unsafeRoutes.forEach((route) {
unsafeRoutes[UniqueKey()] = route;
});
super.initState();
}
@override
Widget build(BuildContext context) {
return FormPage(
title: 'Unsafe Routes',
changed: changed,
onSave: _onSave,
child: ConfigSection(
children: _buildRoutes(),
));
}
_onSave() {
Navigator.pop(context);
if (widget.onSave != null) {
widget.onSave!(unsafeRoutes.values.toList());
}
2020-07-27 20:43:58 +00:00
}
List<Widget> _buildRoutes() {
final double ipWidth = Utils.textSize("000.000.000.000/00", CupertinoTheme.of(context).textTheme.textStyle).width;
List<Widget> items = [];
unsafeRoutes.forEach((key, route) {
items.add(ConfigPageItem(
disabled: widget.onSave == null,
label: Text(route.route ?? ''),
2020-07-27 20:43:58 +00:00
labelWidth: ipWidth,
content: Text('via ${route.via}', textAlign: TextAlign.end),
onPressed: () {
Utils.openPage(context, (context) {
return UnsafeRouteScreen(
route: route,
onSave: (route) {
setState(() {
changed = true;
unsafeRoutes[key] = route;
});
},
onDelete: () {
setState(() {
changed = true;
unsafeRoutes.remove(key);
});
});
});
},
));
});
if (widget.onSave != null) {
items.add(ConfigButtonItem(
content: Text('Add a new route'),
onPressed: () {
Utils.openPage(context, (context) {
return UnsafeRouteScreen(
route: UnsafeRoute(),
onSave: (route) {
setState(() {
changed = true;
unsafeRoutes[UniqueKey()] = route;
});
2020-07-27 20:43:58 +00:00
});
});
},
));
}
2020-07-27 20:43:58 +00:00
return items;
}
}