首页 > 解决方案 > Flutter:导航回同一屏幕后键盘自动打开

问题描述

我有一个屏幕,其中有一个文本字段。如果我点击文本字段然后关闭键盘,然后我使用导航到其他屏幕Navigator,然后导航回该屏幕,即使自动对焦设置为 false,键盘也会自动弹出

文本字段代码,

Container(
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8.sdpWidth),
                  color: themeManager.themeMode == ThemeMode.dark
                      ? Colors.white10
                      : Colors.white,
                  border: Border.all(color: Colors.grey)),
              margin: EdgeInsets.symmetric(horizontal: 16.sdpWidth),
              width: MediaQuery.of(context).size.width * 0.9,
              child: TextFormField(
                controller: textController,
                obscureText: false,  
                onChanged: (text) async {
                  store.orgPlantList.clear();
                  await store.getOrgPlantList(
                      isLoading: true,
                      searchText: text,
                      lat: store.lat,
                      long: store.long);
                  if (text.isEmpty) {
                    store.orgPlantList.clear();
                    await store.getOrgPlantList(
                        isLoading: true,
                        searchText: text,
                        lat: store.lat,
                        long: store.long);
                  }
                },
                autofocus: false,//<<<<<<<<<<<<<<<<<<< autoFocus is turned off
                decoration: InputDecoration(
                  prefixIcon: Icon(
                    Icons.search,
                    color: FlutterFlowTheme.tertiaryColor,
                  ),
                  hintText: langText().searchPointOfInterest,
                  hintStyle: FlutterFlowTheme.bodyText1.override(
                    fontFamily: 'Playfair Display',
                    fontSize: 18.sdpHeight,
                    color: themeManager.themeMode == ThemeMode.dark
                        ? Colors.white70
                        : FlutterFlowTheme.tertiaryColor,
                  ),
                  suffixIcon: InkWell(
                    onTap: () async {
                      if (textController.text.isNotEmpty) {
                        textController.clear();
                        store.orgPlantList.clear();
                        await store.getOrgPlantList(
                            isLoading: true,
                            searchText: '',
                            lat: store.lat,
                            long: store.long);
                      }
                    },
                    child: Icon(
                      Icons.cancel,
                      color: FlutterFlowTheme.tertiaryColor,
                    ),
                  ), 
                ),
                style: FlutterFlowTheme.bodyText1.override(
                  fontFamily: 'Playfair Display',
                  fontSize: 18.sdpWidth,
                ),
              ),
            ),

导航功能,

                  Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                    builder: (context) => NavBarPage(
                                      initialPage: 'HomePage',
                                      orgPlantId: store.orgPlantList[index].id,
                                    ),
                                  ),
                                ),

我在下一个字段中没有任何其他文本字段,所以我该如何解决这个问题

标签: flutterdart

解决方案


在导航到其他屏幕之前尝试添加以下代码。它将删除当前的键盘焦点。

FocusManager.instance.primaryFocus.unfocus();

推荐阅读