You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stream Builder导致TextField触发组件重载,无法正常输入

TextField获焦触发组件重载导致键盘消失的问题

点击TextField后,整个组件会立即重载,键盘随之消失。经排查,问题根源在于main.dart中用于检查用户认证状态的StreamBuilder。

MyApp组件简化代码

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  // this widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    SizeConfig.init(context);
    final auth = FirebaseAuth.instance;
    return MultiProvider(
      providers: [
        //Providers go in here...
      ],
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'livit',
        theme: ThemeData.dark().copyWith(
          scaffoldBackgroundColor: mobileBackgroundColor,
        ),
        home: StreamBuilder(
          stream: auth.authStateChanges(),
          builder: (context, snapshot) {
            //over here we have logic to go to the login screen or the main page
          },
        ),
      ),
    );
  }
}

应用能正常根据用户认证状态跳转至登录页或主页,但只要点击任何TextField,组件就会立刻重载,无法输入。

测试组件验证

我写了一个测试组件,单独运行时完全正常,但替换登录页或主页后,同样出现获焦时组件重载、无法输入的问题:

class TextInputTest extends StatefulWidget {
  const TextInputTest({super.key, required this.title});

  final String title;

  @override
  State<TextInputTest> createState() => _TextInputTestState();
}

class _TextInputTestState extends State<TextInputTest> {
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: TextField(
          controller: _controller,
        ),
      ),
    );
  }
}

注:所有包含TextField的页面都是类似的有状态组件,TextEditingController均声明在State内部。

我已经查阅过相关帖子但未找到有效解决方案,恳请帮忙解决这个问题。

内容的提问来源于stack exchange,提问作者saadman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 09:22:49