rename_dialog.dart 2.6 KB

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