как определить начало набора текста и конец (закрытие клавиатуры) на Flutter - PullRequest
0 голосов
/ 05 мая 2020

Можно ли определить начало набора текста и конец (закрытие клавиатуры) на Flutter?

       TextField(
            decoration: InputDecoration(
              hintText: 'Type a message',
              border: InputBorder.none,
            ),
            cursorColor: Palette.defaultColor,
            maxLines: 6,
            minLines: 1,
            controller: controller,
          ),

1 Ответ

2 голосов
/ 05 мая 2020

Вы можете определить логическое значение и сделать его истинным в методе onChanged и превратить его в ложное через 2 секунды, и если запись остановлена, оно останется ложным, иначе оно превратится в истину.

  bool _isWriting = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(18.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                '${_isWriting ? "Writing..." : "writing stopped"}',
                style: Theme.of(context).textTheme.headline4,
              ),
              TextField(
                decoration: InputDecoration(
                  hintText: 'Type a message',
                  border: InputBorder.none,
                ),
                cursorColor: Colors.cyan,
                maxLines: 6,
                minLines: 1,
                controller: controller,
                onChanged: (text) {
                  if (!_isWriting){
                    _isWriting = true;
                    setState((){});
                    Future.delayed(Duration(seconds: 2)).whenComplete((){
                      _isWriting = false;
                      setState((){});
                    });
                  }
                }
              ),
            ],
          ),
        ),
      ),      
    );
  }

вот живая демонстрация; https://codepen.io/malibayram91/pen/ExVoWzB

...