1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00
comunicmobile/lib/ui/dialogs/new_survey_dialog.dart
2020-04-25 12:02:37 +02:00

138 lines
3.9 KiB
Dart

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 {
return await showDialog<NewSurvey>(
context: context,
builder: (c) => _SurveyDialog(
initialSurvey: initialSurvey,
));
}
class _SurveyDialog extends StatefulWidget {
final NewSurvey initialSurvey;
const _SurveyDialog({Key key, this.initialSurvey}) : super(key: key);
@override
__SurveyDialogState createState() => __SurveyDialogState();
}
class __SurveyDialogState extends State<_SurveyDialog> {
TextEditingController _questionController;
final _choiceController = TextEditingController();
var _choices = Set<String>();
bool get canConfirm =>
_questionController.text.length > 2 && _choices.length > 1;
NewSurvey get newSurvey =>
NewSurvey(question: _questionController.text, answers: _choices);
@override
void initState() {
super.initState();
_questionController = TextEditingController(
text: widget.initialSurvey == null ? "" : widget.initialSurvey.question,
);
if (widget.initialSurvey != null) _choices = widget.initialSurvey.answers;
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(tr("New survey")),
content: _buildBody(),
actions: <Widget>[
// Cancel
MaterialButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(tr("Cancel").toUpperCase()),
),
// Confirm
MaterialButton(
onPressed:
canConfirm ? () => Navigator.of(context).pop(newSurvey) : null,
child: Text(tr("Confirm").toUpperCase()),
),
],
);
}
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:")),
_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,
)),
),
]),
),
);
}
Widget _buildCurrentChoices() {
if (_choices.length == 0) return Text(tr("No choice yet."));
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);
_choiceController.clear();
});
}
}