Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/widgets.dart';
  4.  
  5. import '../styles.dart';
  6.  
  7. class CustomTextField extends StatefulWidget {
  8. final TextEditingController controller;
  9.  
  10. final String placeholder;
  11.  
  12. final bool obscureText;
  13.  
  14. CustomTextField({
  15. @required this.controller,
  16. this.obscureText = false,
  17. this.placeholder = '',
  18. }) : assert(controller != null),
  19. assert(placeholder != null);
  20.  
  21. @override
  22. _CustomTextFieldState createState() => _CustomTextFieldState();
  23. }
  24.  
  25. class _CustomTextFieldState extends State<CustomTextField> {
  26.  
  27. FocusNode focusNode;
  28. String _text = "";
  29.  
  30. @override
  31. void initState() {
  32. super.initState();
  33. focusNode = FocusNode();
  34. focusNode.addListener(() => setState(() {}));
  35. }
  36.  
  37. @override
  38. void dispose() {
  39. super.dispose();
  40. focusNode.dispose();
  41. }
  42.  
  43. @override
  44. Widget build(BuildContext context) {
  45. return DecoratedBox(
  46. decoration: BoxDecoration(
  47. color: Styles.textFieldBackground,
  48. borderRadius: BorderRadius.circular(10),
  49. ),
  50. child: Padding(
  51. padding: const EdgeInsets.symmetric(
  52. horizontal: 4,
  53. vertical: 8,
  54. ),
  55. child: Row(
  56. children: [
  57. Expanded(
  58. child: CupertinoTextField(
  59. decoration: null,
  60. focusNode: focusNode,
  61. controller: widget.controller,
  62. placeholder: widget.placeholder,
  63. style: Styles.searchText,
  64. cursorColor: CupertinoTheme.of(context).primaryColor,
  65. autocorrect: false,
  66. obscureText: widget.obscureText,
  67. onChanged: _textFieldChange,
  68. ),
  69. ),
  70. GestureDetector(onTap: () {
  71. widget.controller.clear();
  72. }, child: Builder(
  73. builder: (BuildContext context) {
  74. if (focusNode != null && focusNode.hasFocus && _text != "") {
  75. return Icon(
  76. CupertinoIcons.clear_thick_circled,
  77. semanticLabel: 'Clear search terms',
  78. color: Styles.searchIconColor,
  79. );
  80. }
  81. return Container(
  82. width: 0.0,
  83. height: 0.0,
  84. );
  85. },
  86. )),
  87. ],
  88. ),
  89. ),
  90. );
  91. }
  92.  
  93. _textFieldChange(String text){
  94. setState(() {
  95. _text = text;
  96. });
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement