mirror of
https://github.com/DefinedNet/mobile_nebula.git
synced 2025-03-06 16:46:35 +00:00
* Enable `flutter_lints` linting * Fix unmarked deps, we aren't on web so we don't need a URL strategy * ` dart fix --apply --code=use_super_parameters` * `dart fix --apply --code=use_key_in_widget_constructors` * `dart fix --apply --code=use_function_type_syntax_for_parameters` * Ignore code-generated `lib/services/theme.dart` file * `dart fix --apply --code=unnecessary_this` * `dart fix --apply --code=unnecessary_null_in_if_null_operators` * `dart fix --apply --code=unnecessary_new` * `dart fix --apply --code=sort_child_properties_last` * `dart fix --apply --code=sized_box_for_whitespace` * `dart fix --apply --code=prefer_typing_uninitialized_variables` * `dart fix --apply --code=prefer_is_empty` * `dart fix --apply --code=prefer_interpolation_to_compose_strings` * `dart fix --apply --code=prefer_final_fields` * `dart fix --apply --code=prefer_const_constructors_in_immutables` * `dart fix --apply --code=prefer_collection_literals` * `dart fix --apply --code=no_leading_underscores_for_local_identifiers` * `dart fix --apply --code=curly_braces_in_flow_control_structures` * `dart fix --apply --code=avoid_function_literals_in_foreach_calls` * `dart fix --apply --code=annotate_overrides` * Add CI for dart linting * `dart format lib/` * Re-enable the `usePathUrlStrategy` call, with proper deps https://docs.flutter.dev/ui/navigation/url-strategies#configuring-the-url-strategy
119 lines
2.9 KiB
Dart
119 lines
2.9 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:mobile_nebula/components/SimplePage.dart';
|
|
import 'package:mobile_nebula/services/utils.dart';
|
|
|
|
/// SimplePage with a form and built in validation and confirmation to discard changes if any are made
|
|
class FormPage extends StatefulWidget {
|
|
const FormPage({
|
|
super.key,
|
|
required this.title,
|
|
required this.child,
|
|
required this.onSave,
|
|
required this.changed,
|
|
this.hideSave = false,
|
|
this.scrollController,
|
|
});
|
|
|
|
final String title;
|
|
final Function onSave;
|
|
final Widget child;
|
|
final ScrollController? scrollController;
|
|
|
|
/// If you need the page to progress to a certain point before saving, control it here
|
|
final bool hideSave;
|
|
|
|
/// Useful if you have a non form field that can change, overrides the internal changed state if true
|
|
final bool changed;
|
|
|
|
@override
|
|
_FormPageState createState() => _FormPageState();
|
|
}
|
|
|
|
class _FormPageState extends State<FormPage> {
|
|
var changed = false;
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
changed = widget.changed || changed;
|
|
|
|
return PopScope<Object?>(
|
|
canPop: !changed,
|
|
onPopInvokedWithResult: (bool didPop, Object? result) async {
|
|
if (didPop) {
|
|
return;
|
|
}
|
|
final NavigatorState navigator = Navigator.of(context);
|
|
|
|
Utils.confirmDelete(
|
|
context,
|
|
'Discard changes?',
|
|
() {
|
|
navigator.pop();
|
|
},
|
|
deleteLabel: 'Yes',
|
|
cancelLabel: 'No',
|
|
);
|
|
},
|
|
child: SimplePage(
|
|
leadingAction: _buildLeader(context),
|
|
trailingActions: _buildTrailer(context),
|
|
scrollController: widget.scrollController,
|
|
title: Text(widget.title),
|
|
child: Form(
|
|
key: _formKey,
|
|
onChanged:
|
|
() => setState(() {
|
|
changed = true;
|
|
}),
|
|
child: widget.child,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildLeader(BuildContext context) {
|
|
return Utils.leadingBackWidget(
|
|
context,
|
|
label: changed ? 'Cancel' : 'Back',
|
|
onPressed: () {
|
|
if (changed) {
|
|
Utils.confirmDelete(
|
|
context,
|
|
'Discard changes?',
|
|
() {
|
|
changed = false;
|
|
Navigator.pop(context);
|
|
},
|
|
deleteLabel: 'Yes',
|
|
cancelLabel: 'No',
|
|
);
|
|
} else {
|
|
Navigator.pop(context);
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
List<Widget> _buildTrailer(BuildContext context) {
|
|
if (!changed || widget.hideSave) {
|
|
return [];
|
|
}
|
|
|
|
return [
|
|
Utils.trailingSaveWidget(context, () {
|
|
if (_formKey.currentState == null) {
|
|
return;
|
|
}
|
|
|
|
if (!_formKey.currentState!.validate()) {
|
|
return;
|
|
}
|
|
|
|
_formKey.currentState!.save();
|
|
widget.onSave();
|
|
}),
|
|
];
|
|
}
|
|
}
|