Проблема в том, что от родительского виджета отсутствует обратный вызов, который позволяет узнать, когда текстовое поле было визуализировано, если оно динамически добавляется в дерево таким образом. Вместо этого просто перенесите запрос на фокус в новый виджет-оболочку, который знает, когда он рендерится в первый раз, и поэтому может запросить фокус после того, как рендеринг произошел, например:
import 'package:flutter/material.dart';
class TextWrapper extends StatefulWidget {
const TextWrapper({
this.controller,
this.focusNode,
this.onChanged,
});
final TextEditingController controller;
final FocusNode focusNode;
final ValueChanged<String> onChanged;
@override
_TextWrapperState createState() => _TextWrapperState();
}
class _TextWrapperState extends State<TextWrapper> {
@override
Widget build(BuildContext context) {
return TextField(
controller: widget.controller,
focusNode: widget.focusNode,
onChanged: widget.onChanged,
style: TextStyle(
color: Colors.white,
),
decoration: InputDecoration(
border: const UnderlineInputBorder(
borderSide: BorderSide.none,
),
prefixIcon: Icon(Icons.search, color: Colors.white),
hintText: 'Search...',
hintStyle: TextStyle(color: Colors.white),
),
);
}
void _onRendered(Duration _) {
FocusScope.of(context).requestFocus(widget.focusNode);
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback(_onRendered);
}
}
Затем внутри вашего Scaffold()
сделайте это вместо:
appBar: AppBar(
title: appBarTitle,
centerTitle: true,
actions: <Widget>[
Center(
child: IconButton(
icon: actionIcon,
onPressed: () {
setState(() {
if (this.actionIcon.icon == Icons.search) {
this.actionIcon = Icon(Icons.navigate_next);
this.appBarTitle = TextWrapper(
controller: editingController,
focusNode: _searchInsideFocus,
onChanged: (findNext) {
webView.findAllAsync(find: findNext);
},
);
} else {
webView.findNext(forward: true);
}
});
},
),
),
],
),
Надеюсь, это поможет ...