Base user workflow (create/update)

This commit is contained in:
Andrew 2023-04-12 03:03:48 +07:00
parent eb5d3e2b70
commit 45be2c80ff
8 changed files with 528 additions and 17 deletions

View file

@ -0,0 +1,217 @@
import 'package:flutter/material.dart';
import 'package:flutter_fast_forms/flutter_fast_forms.dart';
import 'package:get/get.dart';
import 'package:tuuli_app/api/api_client.dart';
import 'package:tuuli_app/api/model/table_field_model.dart';
import 'package:tuuli_app/api/model/tables_list_model.dart';
import 'package:tuuli_app/api/model/user_model.dart';
import 'package:tuuli_app/utils.dart';
import 'package:uuid/uuid.dart';
import 'package:uuid/uuid_util.dart';
class CreateUserResult {
final String username;
final String password;
CreateUserResult(this.username, this.password);
}
class UpdateUserResult {
final String username;
final String password;
final String accessToken;
UpdateUserResult(this.username, this.password, this.accessToken);
}
// TODO: Add a way to change user's group
class CreateUserBottomSheet extends StatefulWidget {
final UserModel? existingUser;
const CreateUserBottomSheet({
super.key,
this.existingUser,
});
@override
State<StatefulWidget> createState() => _CreateUserBottomSheetState();
}
class _CreateUserBottomSheetState extends State<CreateUserBottomSheet> {
final _formKey = GlobalKey<FormState>();
String? newUsername;
String? newPassword;
String? newAccessToken;
bool obscurePassword = true;
bool obscureToken = true;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16),
child: FastForm(
formKey: _formKey,
children: [
Text(
widget.existingUser == null ? "Create new user" : "Update user",
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 16),
Card(
margin: const EdgeInsets.all(8),
child: Container(
padding: const EdgeInsets.all(8),
child: FastTextField(
name: "Username",
labelText: "Username",
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter a value";
}
return null;
},
readOnly: widget.existingUser != null,
initialValue: widget.existingUser?.username,
onChanged: (value) {
newUsername = value;
},
),
),
),
Card(
margin: const EdgeInsets.all(8),
child: Container(
padding: const EdgeInsets.all(8),
child: Row(
children: [
Flexible(
child: FastTextField(
name: "Password",
labelText: "Password",
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter a value";
}
return null;
},
obscureText: obscurePassword,
onChanged: (value) {
newPassword = value;
},
),
),
IconButton(
icon: Icon(
obscurePassword
? Icons.visibility_off
: Icons.visibility,
),
onPressed: () {
setState(() {
obscurePassword = !obscurePassword;
});
},
),
],
),
),
),
if (widget.existingUser != null)
Card(
margin: const EdgeInsets.all(8),
child: Container(
padding: const EdgeInsets.all(8),
child: Row(
children: [
Flexible(
child: FastTextField(
name: "Access token",
labelText: "Access token",
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter a value";
}
return null;
},
obscureText: obscureToken,
initialValue: newAccessToken ??
widget.existingUser?.accessToken,
readOnly: true,
onChanged: (value) {
newAccessToken = value;
},
),
),
IconButton(
icon: const Icon(Icons.shuffle),
onPressed: () {
setState(() {
newAccessToken = randomHexString(64);
});
},
),
IconButton(
icon: Icon(
obscurePassword
? Icons.visibility_off
: Icons.visibility,
),
onPressed: () {
setState(() {
obscureToken = !obscureToken;
});
},
),
],
),
),
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text("Cancel"),
),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
Navigator.of(context).pop(widget.existingUser == null
? CreateUserResult(
newUsername!,
newPassword!,
)
: UpdateUserResult(
newUsername!,
newPassword!,
newAccessToken!,
));
return;
}
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Please fill in all fields"),
),
);
},
child:
Text(widget.existingUser == null ? "Create" : "Update"),
),
],
),
],
),
),
);
}
}