1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-29 16:26:27 +00:00
comunicmobile/lib/ui/dialogs/new_survey_dialog.dart

153 lines
4.4 KiB
Dart
Raw Normal View History

2020-04-25 09:58:45 +00:00
import 'package:comunic/models/new_post.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
/// Create a new survey dialog
///
/// @author Pierre Hubert
/// Show a dialog to prompt information about a new survey to create
Future<NewSurvey?> showNewSurveyDialog(
{required BuildContext context, NewSurvey? initialSurvey}) async {
2020-04-25 09:58:45 +00:00
return await showDialog<NewSurvey>(
context: context,
builder: (c) => _SurveyDialog(
initialSurvey: initialSurvey,
));
}
class _SurveyDialog extends StatefulWidget {
final NewSurvey? initialSurvey;
2020-04-25 09:58:45 +00:00
const _SurveyDialog({Key? key, this.initialSurvey}) : super(key: key);
2020-04-25 09:58:45 +00:00
@override
__SurveyDialogState createState() => __SurveyDialogState();
}
class __SurveyDialogState extends State<_SurveyDialog> {
TextEditingController? _questionController;
2020-04-25 09:58:45 +00:00
final _choiceController = TextEditingController();
var _choices = Set<String>();
var _allowNewChoices = false;
2020-04-25 09:58:45 +00:00
bool get canConfirm =>
_questionController!.text.length > 2 && _choices.length > 1;
2020-04-25 09:58:45 +00:00
NewSurvey get newSurvey => NewSurvey(
question: _questionController!.text,
answers: _choices,
allowNewChoicesCreation: _allowNewChoices);
2020-04-25 09:58:45 +00:00
@override
void initState() {
super.initState();
_questionController = TextEditingController(
text: widget.initialSurvey == null ? "" : widget.initialSurvey!.question,
2020-04-25 09:58:45 +00:00
);
if (widget.initialSurvey != null) {
_choices = widget.initialSurvey!.answers;
_allowNewChoices = widget.initialSurvey!.allowNewChoicesCreation;
}
2020-04-25 09:58:45 +00:00
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(tr("New survey")!),
2020-04-25 09:58:45 +00:00
content: _buildBody(),
actions: <Widget>[
// Cancel
MaterialButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(tr("Cancel")!.toUpperCase()),
2020-04-25 09:58:45 +00:00
),
// Confirm
MaterialButton(
onPressed:
canConfirm ? () => Navigator.of(context).pop(newSurvey) : null,
child: Text(tr("Confirm")!.toUpperCase()),
2020-04-25 09:58:45 +00:00
),
],
);
}
Widget _buildBody() {
return ConstrainedBox(
constraints:
BoxConstraints(maxHeight: MediaQuery.of(context).size.height - 50),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
// Survey question
TextField(
controller: _questionController,
onChanged: (s) => setState(() {}),
decoration: InputDecoration(
alignLabelWithHint: true, labelText: tr("Question")),
),
Container(height: 30),
Text(tr("Current choices:")!),
2020-04-25 09:58:45 +00:00
_buildCurrentChoices(),
Container(
height: 10,
),
// Add choice form
TextField(
controller: _choiceController,
onChanged: (s) => setState(() {}),
onSubmitted: (s) => _addChoice(),
decoration: InputDecoration(
hintText: tr("New choice..."),
suffixIcon: IconButton(
icon: Icon(Icons.add),
onPressed: () => _choiceController.text.length > 0
? _addChoice()
: null,
)),
),
// Allow users to create new choices
ListTile(
leading: Switch.adaptive(
value: _allowNewChoices,
onChanged: (v) => setState(() => _allowNewChoices = v),
),
title: Text(tr("Allow users to create new choices")!),
)
2020-04-25 09:58:45 +00:00
]),
),
);
}
Widget _buildCurrentChoices() {
if (_choices.length == 0) return Text(tr("No choice yet.")!);
2020-04-25 09:58:45 +00:00
return Column(
children: _choices
.map((f) => ListTile(
title: Text(f),
dense: true,
trailing: IconButton(
icon: Icon(Icons.delete),
iconSize: 15,
onPressed: () => setState(() => _choices.remove(f)),
)))
.toList(),
);
}
void _addChoice() {
if (_choiceController.text.length == 0) return;
setState(() {
_choices.add(_choiceController.text);
2020-04-25 10:02:37 +00:00
_choiceController.clear();
2020-04-25 09:58:45 +00:00
});
}
}