Skip to content

How can I align a TextFormField on AppBar in vertical center? #23910

@jaggerwang

Description

@jaggerwang

Problem

The TextFormField on AppBar is a little higher than the back button on AppBar, and having space on left and right. As the following screenshot shows.

image

I want to align vertical center and removing the left and right space, to simulate a search bar.

Code

class TumblrSearchInputScreen extends StatefulWidget {
  final String initialTag;
  final void Function(String) search;

  TumblrSearchInputScreen({
    Key key,
    this.initialTag,
    @required this.search,
  }) : super(key: key);

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

class _TumblrSearchInputScreenState extends State<TumblrSearchInputScreen> {
  static final _bodyKey = GlobalKey<_BodyState>();

  final _formKey = GlobalKey<FormState>();
  TextEditingController _textController;
  String tag = '';

  @override
  void initState() {
    super.initState();

    _textController = TextEditingController(text: widget.initialTag);
  }

  void _submit() {
    if (_formKey.currentState.validate()) {
      _formKey.currentState.save();
      widget.search(tag);
      Navigator.of(context).pop();
    }
  }

  String _validateTag(String tag) {
    if (tag.isEmpty) {
      return '请输入关键字';
    }
    return '';
  }

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, _ViewModel>(
      converter: (store) => _ViewModel(
            tags: store.state.tumblr.tags,
          ),
      builder: (context, vm) => Scaffold(
            appBar: AppBar(
              // automaticallyImplyLeading: false,
              title: Form(
                key: _formKey,
                child: TextFormField(
                  decoration: InputDecoration(
                    border: InputBorder.none,
                    // prefixIcon: IconButton(
                    //   onPressed: () => Navigator.of(context).pop(),
                    //   icon: Icon(Icons.arrow_back, color: TblTheme.whiteLight),
                    // ),
                    hintText: '请输入关键字',
                    hintStyle: TextStyle(color: TblTheme.whiteNormal),
                    suffixIcon: GestureDetector(
                      onTap: Feedback.wrapForTap(
                        _textController.clear,
                        context,
                      ),
                      child: Icon(Icons.clear, color: TblTheme.whiteLight),
                    ),
                  ),
                  validator: _validateTag,
                  onSaved: (value) => tag = value.trim(),
                  onFieldSubmitted: (value) => _submit(),
                  autofocus: true,
                  maxLength: 20,
                  controller: _textController,
                  style: TextStyle(
                    color: TblTheme.whiteNormal,
                    fontSize: TblTheme.fontSizeLarge,
                  ),
                ),
              ),
            ),
            body: _Body(
              key: _bodyKey,
              vm: vm,
              search: widget.search,
            ),
          ),
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions