1
0
mirror of https://github.com/go-vikunja/app synced 2024-06-02 18:49:47 +00:00
app-mirror-github/lib/pages/list/list_edit.dart

148 lines
5.5 KiB
Dart
Raw Normal View History

import 'dart:ffi';
import 'dart:developer';
2019-03-11 20:29:15 +00:00
import 'package:flutter/material.dart';
import 'package:vikunja_app/global.dart';
import 'package:vikunja_app/models/list.dart';
2019-03-14 21:25:57 +00:00
import 'package:vikunja_app/theme/button.dart';
import 'package:vikunja_app/theme/buttonText.dart';
2019-03-11 20:29:15 +00:00
class ListEditPage extends StatefulWidget {
final TaskList list;
2022-08-27 21:04:43 +00:00
ListEditPage({required this.list}) : super(key: Key(list.toString()));
2019-03-11 20:29:15 +00:00
@override
State<StatefulWidget> createState() => _ListEditPageState();
}
class _ListEditPageState extends State<ListEditPage> {
final _formKey = GlobalKey<FormState>();
bool _loading = false;
2022-09-03 15:43:16 +00:00
String _title = '', _description = '';
2022-08-27 21:04:43 +00:00
bool? displayDoneTasks;
2022-09-03 15:43:16 +00:00
late int listId;
@override
void initState(){
listId = widget.list.id;
super.initState();
}
2019-03-11 20:29:15 +00:00
@override
Widget build(BuildContext ctx) {
if(displayDoneTasks == null)
VikunjaGlobal.of(context).listService.getDisplayDoneTasks(listId).then(
(value) => setState(() => displayDoneTasks = value == "1"));
else
2022-08-27 21:04:43 +00:00
log("Display done tasks: " + displayDoneTasks.toString());
2019-03-11 20:29:15 +00:00
return Scaffold(
2019-03-14 21:25:57 +00:00
appBar: AppBar(
title: Text('Edit List'),
),
body: Builder(
builder: (BuildContext context) => SafeArea(
child: Form(
key: _formKey,
child: ListView(
padding: const EdgeInsets.all(16.0),
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
maxLines: null,
keyboardType: TextInputType.multiline,
initialValue: widget.list.title,
2022-09-03 15:43:16 +00:00
onSaved: (title) => _title = title ?? '',
validator: (title) {
2022-08-27 21:04:43 +00:00
//if (title?.length < 3 || title.length > 250) {
// return 'The title needs to have between 3 and 250 characters.';
//}
return null;
},
decoration: new InputDecoration(
labelText: 'Title',
border: OutlineInputBorder(),
2019-03-14 21:25:57 +00:00
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
maxLines: null,
keyboardType: TextInputType.multiline,
initialValue: widget.list.description,
2022-09-03 15:43:16 +00:00
onSaved: (description) => _description = description ?? '',
validator: (description) {
2022-08-27 21:04:43 +00:00
if(description == null)
return null;
if (description.length > 1000) {
return 'The description can have a maximum of 1000 characters.';
}
return null;
},
decoration: new InputDecoration(
labelText: 'Description',
border: OutlineInputBorder(),
2019-03-15 22:14:37 +00:00
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
2022-09-03 15:43:16 +00:00
child: CheckboxListTile(
value: displayDoneTasks ?? false,
title: Text("Show done tasks"),
onChanged: (value) {
value ??= false;
VikunjaGlobal.of(context).listService.setDisplayDoneTasks(listId, value ? "1" : "0");
setState(() => displayDoneTasks = value);
},
),
),
Builder(
builder: (context) => Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: FancyButton(
onPressed: !_loading
? () {
2022-08-27 21:04:43 +00:00
if (_formKey.currentState!.validate()) {
Form.of(context)?.save();
_saveList(context);
}
}
2022-08-27 21:04:43 +00:00
: () {},
child: _loading
? CircularProgressIndicator()
: VikunjaButtonText('Save'),
))),
]),
),
),
2019-03-14 21:25:57 +00:00
),
);
2019-03-11 20:29:15 +00:00
}
_saveList(BuildContext context) async {
setState(() => _loading = true);
// FIXME: is there a way we can update the list without creating a new list object?
// aka updating the existing list we got from context (setters?)
2022-08-26 15:38:32 +00:00
widget.list.title = _title;
widget.list.description = _description;
VikunjaGlobal.of(context).listService.update(widget.list).then((_) {
2019-03-15 22:14:37 +00:00
setState(() => _loading = false);
2022-04-10 13:31:56 +00:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
2019-03-15 22:14:37 +00:00
content: Text('The list was updated successfully!'),
));
}).catchError((err) {
setState(() => _loading = false);
2022-04-10 13:31:56 +00:00
ScaffoldMessenger.of(context).showSnackBar(
2019-03-15 22:14:37 +00:00
SnackBar(
content: Text('Something went wrong: ' + err.toString()),
action: SnackBarAction(
label: 'CLOSE',
2022-04-10 13:31:56 +00:00
onPressed: ScaffoldMessenger.of(context).hideCurrentSnackBar),
2019-03-15 22:14:37 +00:00
),
);
});
2019-03-11 20:29:15 +00:00
}
}