textfield

来源:17-3 Flutter 全屏幕适配指南

等她下班

2019-08-28

老师,您好,widget.defaultText的值不为null时,textfield不会显示出来,这个如何解决?

import 'package:flutter/material.dart';

class Search extends StatefulWidget {
  final bool enabled;
  final bool hideLeft;
  final bool hideRight;
  final bool hideSpeak;
  final bool autoFocus;
  final String hint;
  final String defaultText;
  final void Function() leftButtonClick;
  final void Function() rightButtonClick;
  final void Function() speakClick;
  final void Function() inputBoxClick;
  final ValueChanged<String> onChanged;
  final ValueChanged<String> onSubmitted;

  const Search({
    Key key,
    this.enabled = true,
    this.hideLeft,
    this.hideRight,
    this.hideSpeak,
    this.autoFocus = true,
    this.hint,
    this.defaultText,
    this.leftButtonClick,
    this.rightButtonClick,
    this.speakClick,
    this.inputBoxClick,
    this.onChanged,
    this.onSubmitted,
  }) : super(key: key);

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

class _SearchState extends State<Search> {
  bool showClear = false;
  final TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    if(widget.defaultText != null) {
      setState(() {
        _controller.text = widget.defaultText;
      });
    }
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return _genNormalSearch();
  }

  _genNormalSearch() {
    return Container(
      child: Row(
        children: <Widget>[
          _warpTap(
            Container(
              padding: widget.hideLeft ?? false
                  ? EdgeInsets.all(5)
                  : EdgeInsets.fromLTRB(6, 5, 10, 5),
              child: widget.hideLeft ?? false
                  ? null
                  : Icon(
                      Icons.arrow_back_ios,
                      color: Colors.grey,
                      size: 26,
                    ),
            ),
            widget.leftButtonClick,
          ),
          Expanded(flex: 1, child: _inputBox()),
          _warpTap(
            Container(
              padding: widget.hideRight ?? false
                  ? EdgeInsets.all(5)
                  : EdgeInsets.fromLTRB(10, 5, 10, 5),
              child: widget.hideRight ?? false
                  ? null
                  : Text(
                      '搜索',
                      style: TextStyle(color: Colors.blue, fontSize: 17),
                    ),
            ),
            widget.rightButtonClick,
          ),
        ],
      ),
    );
  }

  _warpTap(Widget child, void Function() callback) {
    return GestureDetector(
      onTap: () {
        if (callback != null) callback();
      },
      child: child,
    );
  }

  _inputBox() {
    return Container(
      height: 30,
      padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
      decoration: BoxDecoration(
          color: Color(int.parse('0xffEDEDED')),
          borderRadius: BorderRadius.circular(5)),
      child: Row(
        children: <Widget>[
          Icon(
            Icons.search,
            size: 20,
            color: Color(int.parse('0xffA9A9A9')),
          ),
          Expanded(
            flex: 1,
            child: TextField(
              controller: _controller,
              onChanged: _onChanged,
              onSubmitted: _onSubmitted,
              autofocus: widget.autoFocus,
              style: TextStyle(
                fontSize: 18,
                color: Colors.black,
                fontWeight: FontWeight.w300,
              ),
              decoration: InputDecoration(
                contentPadding: EdgeInsets.fromLTRB(5, 0, 5, 0),
                border: InputBorder.none,
                hintText: widget.hint ?? '',
                hintStyle: TextStyle(fontSize: 15, fontWeight: FontWeight.w400),
              ),
            ),
          ),
          inputClear
        ],
      ),
    );
  }

  get inputClear {
    return !showClear
        ? widget.hideSpeak
            ? Container()
            : _warpTap(
                Icon(
                  Icons.mic,
                  size: 22,
                  color: Colors.blue,
                ),
                widget.speakClick,
              )
        : _warpTap(
            Icon(
              Icons.clear,
              size: 22,
              color: Colors.grey,
            ),
            () {
              setState(() {
                _controller.clear();
              });
              _onChanged('');
              _onSubmitted('');
            },
          );
  }

  void _onChanged(String text) {
    if (text.length > 0) {
      setState(() {
        showClear = true;
      });
    } else {
      setState(() {
        showClear = false;
      });
    }

    if (widget.onChanged != null) {
      widget.onChanged(text);
    }
  }

  void _onSubmitted(String value) {
    if (widget.onSubmitted != null) {
      widget.onSubmitted(value);
    }
  }
}

写回答

2回答

CrazyCodeBoy

2019-09-02

用你代码试了下,通过:

setState(() {
  _controller.text = '4e23';
});

是可以设置默认值的,你短点调试一下,看widget.defaultText到底有没有值?

//img.mukewang.com/szimg/5d6d189409b94f7f14160478.jpg

0
1
等她下班
@override void initState() { _setTextField(); super.initState(); } @override void didUpdateWidget(Search oldWidget) { _setTextField(); super.didUpdateWidget(oldWidget); } @override Widget build(BuildContext context) { return _genNormalSearch(); } void _setTextField() { String keyword = widget.defaultText??''; setState(() { _controller.text = keyword; showClear = keyword.isNotEmpty; }); }
2019-09-02
共1条回复

CrazyCodeBoy

2019-08-29

从上述代码中没发现问题,将完整代码贴出来我帮你看下
0
2
CrazyCodeBoy
回复
等她下班
看上面回复哈
2019-09-02
共2条回复

Flutter从入门到进阶 实战携程网App 一网打尽核心技术

解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。

4788 学习 · 3270 问题

查看课程