Flutter TextField遇到didUpdateWidget

一、情况说明

列表内为TextField数据填写,当进行数据删除的时候,界面UI不更新时需要在TextField小部件内用didUpdateWidget进行相应的数据刷新,(注意:当小部件用didUpdateWidget时TextField数据回调之后不需要进行setState)


image.png
二、代码示例
// 输入框
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xiaofendian/pages/activity/widgets/part_item.dart';
import 'package:xiaofendian/pages/shop/shop_dialog.dart';
import 'package:xiaofendian/utils/double_input_formatter.dart';
import 'package:xiaofendian/utils/utils_index.dart';
import 'package:xiaofendian/widgets/fd_prompt_images.dart';

class PartItemInputText extends StatefulWidget {
  String title;
  String hint;
  String detail;
  String unit;
  int maxLength;
  num maxNumber;
  var initValue;

  // 是否只能输入数字
  bool isNum;

  // 是否只能是小数
  bool isDouble;
  bool isEnabled;
  bool showMust;
  // OnPartItemInputChange change;
  Function change;

  // 提示
  bool isShowPrompt;
  bool isDelete;

  PartItemInputText(this.title, this.hint,
      {this.isEnabled = true,
      this.maxNumber,
      this.isDouble = false,
      this.isNum = false,
      this.initValue,
      this.change,
      this.unit,
      this.maxLength,
      this.detail,
      this.showMust = false,
      this.isShowPrompt = false,
      this.isDelete = false,
      Key key})
      : super(key: key);

  _PartItemInputTextState createState() => _PartItemInputTextState();
}

class _PartItemInputTextState extends State<PartItemInputText> {
  TextEditingController _controller = new TextEditingController();
  List<TextInputFormatter> textInputFormatterList;

  @override
  void initState() {
    super.initState();
    setController();
    textInputFormatterList = [
      LengthLimitingTextInputFormatter(widget.maxLength)
    ];
    if (widget.isNum) {
       textInputFormatterList.add(WhitelistingTextInputFormatter.digitsOnly);
    }
    if (widget.isDouble) {
      textInputFormatterList.add(DoubleInputFormatter());
    }
  }

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

  @override
  void didUpdateWidget(covariant PartItemInputText oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.initValue != widget.initValue) {
      setState(() {
        setController();
      });
    }
  }

  setController() {
    _controller.text =
        widget.initValue == null ? "" : widget.initValue.toString();
    _controller.selection = TextSelection.fromPosition(TextPosition(
        affinity: TextAffinity.downstream, offset: _controller.text.length));
  }

  backChange(var text) {
    widget.change(text);
  }

  @override
  Widget build(BuildContext context) {
    return PartItem(
      widget.title,
      Row(
        children: <Widget>[
          Offstage(
            offstage: !widget.isShowPrompt,
            child: ActivityPrompt(),
          ),
          Expanded(
            child: Container(
              padding: EdgeInsetsUtil.only(left: 20),
              child: TextField(
                enabled: widget.isEnabled,
                controller: _controller,
                textAlign: TextAlign.right,
                keyboardType: widget.isNum
                    ? TextInputType.numberWithOptions(decimal: true)
                    : TextInputType.text,
                maxLines: 1,
                minLines: 1,
                inputFormatters: textInputFormatterList,
                style: TextStyleUtil.get(FdColors.fontColor, 30),
                decoration: InputDecoration(
                  border: InputBorder.none,
                  hintText: widget.hint,
                  hintStyle: TextStyleUtil.get(FdColors.c9f9f9f, 30),
                ),
                onChanged: (value) {
                //回调之后不需要刷新
                  widget.change(_controller.text);
                },
              ),
            ),
          ),
          Offstage(
            offstage: Utils.isEmptyString(widget.unit),
            child: XTextUtil.getText(widget.unit, 30, marginLeft: 10),
          ),
        ],
      ),
      detail: widget.detail,
      showMust: widget.showMust,
    );
  }
}

推荐阅读更多精彩内容