rename_dialog.dart 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. _RenameDialogState 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: 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: TextStyle(
  32. color: Colors.white30,
  33. ),
  34. contentPadding: 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: 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: Colors.white,
  66. ),
  67. ),
  68. onPressed: () {
  69. if (_newName.trim().isEmpty) {
  70. showErrorDialog(
  71. context, "empty name", "${widget.type} name cannot be empty");
  72. return;
  73. }
  74. if (_newName.trim().length > widget.maxLength) {
  75. showErrorDialog(context, "name too large",
  76. "${widget.type} name should be less than ${widget.maxLength} characters");
  77. return;
  78. }
  79. Navigator.of(context).pop(_newName.trim());
  80. },
  81. ),
  82. ],
  83. );
  84. }
  85. }