TextField has abormal focus

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:yikongce/util/input_formatter.dart';

import 'form.dart';
const TextStyle _style = TextStyle(fontSize: 16);
class FormInput extends StatelessWidget{
  final String title;
  final ValueChanged<String> change;
  final String? hint;
  final bool enable;
  final TextInputFormatter? formatter;
  final int maxLines;
  final TextEditingController controller;
  final bool showTitle;
  final FocusNode focusNode = FocusNode();
  FormInput({
    required this.controller,
    required this.title,
    required this.change,
    this.hint,
    this.enable=true,
    this.formatter,
    this.showTitle=true,
    this.maxLines = 1
  });
  @override
  Widget build(BuildContext context) {
    focusNode.addListener(_listener);
    TextInputType? inputType;
    if(formatter is TIFZhengShu || formatter is TIFXiaoShu || formatter is TIFNumber)inputType=TextInputType.number;
    TextField textField = TextField(
      style: _style,
      enabled: enable,
      controller: controller,
      keyboardType: inputType,
      textInputAction: TextInputAction.done,
      inputFormatters: formatter==null?null:[formatter!],
      minLines: 1,
      maxLines: 3,
      focusNode: focusNode,
      decoration: InputDecoration(
        contentPadding: EdgeInsets.fromLTRB(0, 0, 20, 0),
        border: InputBorder.none,
        hintText: this.hint,
        suffix: Text(this.hint??""),
        suffixStyle: TextStyle(color: Colors.blue)
      ),
      // onSubmitted: (v)=>change(controller.text),
      onSubmitted:(v)=>change(controller.text),
      // onChanged: (v)=>change(controller.text),//不能使用onChanged,会导致小数输入异常
    );
    Text text = Text("${this.title} : ",style: _style,);
    return FormItem(
      title: text,
      value: textField,
    );
  }
  void _listener(){
    if(!focusNode.hasFocus){
      print("日志:失去焦点");
      // change(controller.text);
      // change("123");
      Future.delayed(Duration.zero,()=>onChange());
      focusNode.removeListener(_listener);
    }
  }
  void onChange()=>print("日志:XXX");
}



我在页面上有许多这个输入框,但是只要在focusNode的Listener执行回调方法,当我点击其他的输入框时,之前的输入框的焦点就不会失去.

1 thought on “TextField has abormal focus

  1. @hu70258tao 我在此幫忙協助翻譯 @TahaTesser 給你的回覆:
    他說你最開始提供的代碼不完全,他無法做測試,你可否提供你目前”可執行”後的代碼(最少需要包含輸入框的部分)? 然後詳細解釋一下你的問題在哪?你執行後的結果以及你需要的結果? 謝謝

Comments are closed.