4

I'm using a Flutter TextField inside a custom widget, but somehow the textAlignVertical property is not working. Below is the code for the custom widget. Is there anybody who knows where this behavior is coming from? I tried to solve it with setting the height property of the styleparameter for the TextField to 0 but that didn't help either.

Any help is greatly appreciated!

class CustomTextField extends StatefulWidget {
  final double width;
  final double height;
  final Color color;
  final TextEditingController controller;
  final String hintText;
  final TextStyle textStyle;
  final Icon icon;
  final String errorKey;
  final Map<String, String> errors;
  final BorderRadius borderRadius;

  CustomTextField({
    this.controller, 
    this.hintText, 
    this.textStyle,
    this.icon, 
    this.errorKey, 
    this.errors, 
    this.width,
    this.height,
    this.color,
    this.borderRadius});

  @override
  _CustomTextFieldState createState() => _CustomTextFieldState();
}

class _CustomTextFieldState extends State<CustomTextField> {
  bool showClearButton = false;
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          width: widget.width,
          height: 100,
          decoration: BoxDecoration(
            color: widget.color,
            borderRadius: widget.borderRadius
          ),
          child: Row(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(left: 10),
                child: widget.icon,
              ),
              Expanded(
                child: TextField(
                  textAlignVertical: TextAlignVertical.center,
                  controller: widget.controller,
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(left: 8),
                    hintText: widget.hintText,
                    border: InputBorder.none,
                  ),
                  onChanged: (value) {
                    if (widget.controller.text.isNotEmpty) {
                      setState(() { showClearButton = true; });
                    } 
                  },
                  onEditingComplete: () {
                    FocusScope.of(context).unfocus(); 
                  },
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(right: 8),
                child: (showClearButton || widget.controller.text.isNotEmpty) 
                  ? GestureDetector(
                      child: Icon(Icons.clear, color: BaseColors.orange,),
                      onTap: () { 
                        widget.controller.clear(); 
                        setState(() { showClearButton = false; });
                      }) 
                  : SizedBox(width: 0,),
              )
            ]
          )
        ),
        (widget.errors != null && widget.errors.containsKey(widget.errorKey)) 
          ? Container(
              alignment: Alignment.topLeft,
              child: Padding(padding: EdgeInsets.only(left: 10), child: Text(widget.errors[widget.errorKey], style: TextStyle(fontSize: 12)))
            )
          : SizedBox(height: 0),
        ]
    );
  }
}
JJuice
  • 1,015
  • 2
  • 12
  • 21

1 Answers1

2

You can config the border property in the TextField.decoration
so you can see a non-border center-textaligned `TextField``

TextField(
  decoration: InputDecoration(
    focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(width: 0, color: Colors.transparent)
    ),
    disabledBorder: OutlineInputBorder(
      borderSide: BorderSide(width: 0, color: Colors.transparent)
    ),
    enabledBorder: OutlineInputBorder(
      borderSide: BorderSide(width: 0, color: Colors.transparent)
    ),
    border: OutlineInputBorder(
      borderSide: BorderSide(width: 0, color: Colors.transparent)
    ),
    contentPadding: EdgeInsets.symmetric(vertical: 0),
  ),             
  //other config            
),
J. S.
  • 8,905
  • 2
  • 34
  • 44