rename_dialog.dart 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import 'package:flutter/material.dart';
  2. import 'package:photos/utils/dialog_util.dart';
  3. class RenameDialog extends StatefulWidget {
  4. final String name;
  5. final String type;
  6. final int maxLength;
  7. const RenameDialog(this.name, this.type, {Key key, this.maxLength = 100})
  8. : super(key: key);
  9. @override
  10. State<RenameDialog> createState() => _RenameDialogState();
  11. }
  12. class _RenameDialogState extends State<RenameDialog> {
  13. String _newName;
  14. @override
  15. void initState() {
  16. super.initState();
  17. _newName = widget.name;
  18. }
  19. @override
  20. Widget build(BuildContext context) {
  21. return AlertDialog(
  22. title: const Text("Enter a new name"),
  23. content: SingleChildScrollView(
  24. child: Column(
  25. mainAxisAlignment: MainAxisAlignment.start,
  26. crossAxisAlignment: CrossAxisAlignment.start,
  27. children: [
  28. TextFormField(
  29. decoration: InputDecoration(
  30. hintText: '${widget.type} name',
  31. hintStyle: const TextStyle(
  32. color: Colors.white30,
  33. ),
  34. contentPadding: const EdgeInsets.all(12),
  35. ),
  36. onChanged: (value) {
  37. setState(() {
  38. _newName = value;
  39. });
  40. },
  41. autocorrect: false,
  42. keyboardType: TextInputType.text,
  43. initialValue: _newName,
  44. autofocus: true,
  45. ),
  46. ],
  47. ),
  48. ),
  49. actions: [
  50. TextButton(
  51. child: const Text(
  52. "Cancel",
  53. style: TextStyle(
  54. color: Colors.redAccent,
  55. ),
  56. ),
  57. onPressed: () {
  58. Navigator.of(context).pop(null);
  59. },
  60. ),
  61. TextButton(
  62. child: Text(
  63. "Rename",
  64. style: TextStyle(
  65. color: Theme.of(context).colorScheme.onSurface,
  66. ),
  67. ),
  68. onPressed: () {
  69. if (_newName.trim().isEmpty) {
  70. showErrorDialog(
  71. context,
  72. "Empty name",
  73. "${widget.type} name cannot be empty",
  74. );
  75. return;
  76. }
  77. if (_newName.trim().length > widget.maxLength) {
  78. showErrorDialog(
  79. context,
  80. "Name too large",
  81. "${widget.type} name should be less than ${widget.maxLength} characters",
  82. );
  83. return;
  84. }
  85. Navigator.of(context).pop(_newName.trim());
  86. },
  87. ),
  88. ],
  89. );
  90. }
  91. }