简体   繁体   English

Flutter: Navigator.of(context).pop() 返回黑屏

[英]Flutter: Navigator.of(context).pop() return black screen

so the problem with my code is that as soon as I press the submit button my screen blanks out and then I'm unable to save the next response.所以我的代码的问题是,一旦我按下提交按钮,我的屏幕就会空白,然后我无法保存下一个响应。 I want that as soon as I submit my form should reset for reuse and should be ready for taking next response from user and yes that black screen too should disappear.我希望一旦我提交我的表单就应该重置以供重用,并且应该准备好接受用户的下一个响应,是的,黑屏也应该消失。

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  TextEditingController problemBox = TextEditingController();

  final db = FirebaseFirestore.instance;

  List _listItem = ["Category 1", "Category 2", "Category 3", "Category 4"];
  List _listItem1 = [
    "Sub Category 1",
    "Sub Category 2",
    "Sub Category 3",
    "Sub Category 4"
  ];
  List _listItem2 = ["CRIS", "ADMINISTRATION", "ZONE", "DEPARTMENT"];

  String dropdownValue;
  String holder = '';

  void getDropDownItem() async {
    setState(() {
      holder = dropdownValue;
    });
  }

  String dropdownValue1;
  String holder1 = '';

  void getDropDownItem1() async {
    setState(() {
      holder1 = dropdownValue1;
    });
  }

  String dropdownValue2;
  String holder2 = '';

  void getDropDownItem2() async {
    setState(() {
      holder2 = dropdownValue2;
    });
  }

  bool autoValidate = true;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: false,
        appBar: AppBar(
          title: Text("Feedback"),
          centerTitle: true,
          leading: IconButton(
            onPressed: () {},
            icon: Icon(Icons.home),
          ),
        ),
        body: Container(
            child: Center(
          child: SingleChildScrollView(
            child: Form(
              autovalidateMode: AutovalidateMode.onUserInteraction,
              key: _formKey,
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Flexible(child: Text("CATEGORY")),
                        Flexible(
                            child: RichText(
                          textAlign: TextAlign.center,
                          text: TextSpan(
                            children: <TextSpan>[
                              TextSpan(
                                text: '*',
                                style: TextStyle(color: Colors.red),
                              ),
                            ],
                          ),
                        )),
                        SizedBox(width: 30),
                        Flexible(
                            child: DropdownButtonFormField<String>(
                          isExpanded: true,
                          hint: Text("Select"),
                          value: dropdownValue,
                          validator: (value) =>
                              value == null ? "Please select a value" : null,
                          items: _listItem
                              .map<DropdownMenuItem<String>>((valueItem) {
                            return DropdownMenuItem<String>(
                              value: valueItem,
                              child: Text(valueItem),
                            );
                          }).toList(),
                          onChanged: (value) {
                            setState(() {
                              dropdownValue = value;
                            });
                          },
                        )),
                      ],
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Flexible(child: Text("SUB-CATEGORY")),
                        Flexible(
                            child: RichText(
                          textAlign: TextAlign.center,
                          text: TextSpan(
                            children: <TextSpan>[
                              TextSpan(
                                text: '*',
                                style: TextStyle(color: Colors.red),
                              ),
                            ],
                          ),
                        )),
                        SizedBox(width: 30),
                        Flexible(
                            child: DropdownButtonFormField<String>(
                          isExpanded: true,
                          hint: Text("Select"),
                          value: dropdownValue1,
                          validator: (value) =>
                              value == null ? "Please select a value" : null,
                          items: _listItem1
                              .map<DropdownMenuItem<String>>((valueItem1) {
                            return DropdownMenuItem<String>(
                              value: valueItem1,
                              child: Text(valueItem1),
                            );
                          }).toList(),
                          onChanged: (value) {
                            setState(() {
                              dropdownValue1 = value;
                            });
                          },
                        )),
                      ],
                    ),
                    SizedBox(height: 10),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Flexible(child: Text("MARKED TO")),
                        Flexible(
                            child: RichText(
                          text: TextSpan(
                            children: <TextSpan>[
                              TextSpan(
                                text: '*',
                                style: TextStyle(color: Colors.red),
                              ),
                            ],
                          ),
                        )),
                        SizedBox(width: 30),
                        Flexible(
                            child: DropdownButtonFormField<String>(
                          isExpanded: true,
                          hint: Text("Select"),
                          value: dropdownValue2,
                          validator: (value) =>
                              value == null ? "Please select a value" : null,
                          items: _listItem2
                              .map<DropdownMenuItem<String>>((valueItem2) {
                            return DropdownMenuItem<String>(
                              value: valueItem2,
                              child: Text(valueItem2),
                            );
                          }).toList(),
                          onChanged: (value) {
                            setState(() {
                              dropdownValue2 = value;
                            });
                          },
                        )),
                      ],
                    ),
                    SizedBox(height: 10),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Padding(
                          padding: EdgeInsets.fromLTRB(70, 00, 70, 00),
                          child: TextFormField(
                            validator: (String value) {
                              if (value.isEmpty) {
                                return "Description is required";
                              }
                              return null;
                            },
                            controller: problemBox,
                            decoration: InputDecoration(
                              hintText: "Describe your problem here.",
                            ),
                            maxLength: 1000,
                            maxLines: 5,
                          ),
                        )
                      ],
                    ),
                    SizedBox(height: 10),
                    ButtonTheme(
                      child: ElevatedButton(
                        child: Text("Submit"),
                        onPressed: () {
                          if (_formKey.currentState.validate()) {
                            print(" form is validate");
                            db.collection("COLLECTION").add(
                              {
                                "CATEGORY": dropdownValue,
                                "SUB-CATEGORY": dropdownValue1,
                                "MARKED TO": dropdownValue2,
                                "PROBLEM": problemBox.text,
                              },
                            );
                            Navigator.of(context).pop();
                          }
                        },
                        style: ElevatedButton.styleFrom(
                          padding: EdgeInsets.symmetric(
                              horizontal: 25, vertical: 15),
                        ),
                      ),
                    ),
                  ]),
            ),
          ),
        )));
  }
}

At any point, if you find that on popping your stack gives you blank screen which means the current screen is the last route of your stack.在任何时候,如果您发现弹出堆栈时会出现空白屏幕,这意味着当前屏幕是堆栈的最后一条路径。 So, if you pop your last visible screen it's popping the last route and showing blank black canvas.因此,如果您弹出最后一个可见屏幕,它会弹出最后一条路线并显示空白黑色 canvas。

So, at some places where you are uncertain about the screen popping you can check like,所以,在一些你不确定屏幕弹出的地方,你可以检查一下,

if (Navigator.of(context).canPop()) {
   Navigator.of(context).pop();
}

If you are using NavigatorKey then you can try like,如果您使用的是 NavigatorKey,那么您可以尝试,

if (navigatorKey.currentState.canPop()) {
   navigatorKey.currentState.pop();
}

Still if you can't figure out what to do and want to try some workaround at last you can try like,不过,如果你不知道该怎么做,最后想尝试一些解决方法,你可以试试,

// you will need to import services API
import 'package:flutter/services.dart';

SystemNavigator.pop();

This will manage your navigation stack.这将管理您的导航堆栈。

Navigator.of(context).pop(); Pop the top-most route off the navigator that most tightly encloses the given context.从导航器中弹出最紧密地包围给定上下文的最顶层路线。

The current route's Route.didPop method is called first.当前路由的 Route.didPop 方法首先被调用。 If that method returns false, then the route remains in the Navigator's history (the route is expected to have popped some internal state; see eg LocalHistoryRoute).如果该方法返回 false,则该路线仍保留在 Navigator 的历史记录中(预计该路线已弹出一些内部 state;参见例如 LocalHistoryRoute)。 Otherwise, the rest of this description applies.否则,适用本说明的 rest。

If non-null, result will be used as the result of the route that is popped;如果非空,result 将作为被弹出的路由的结果; the future that had been returned from pushing the popped route will complete with result.从推送弹出的路由返回的未来将完成结果。 Routes such as dialogs or popup menus typically use this mechanism to return the value selected by the user to the widget that created their route.诸如对话框或弹出菜单之类的路由通常使用此机制将用户选择的值返回给创建其路由的小部件。 The type of result, if provided, must match the type argument of the class of the popped route (T).结果的类型(如果提供)必须与弹出路由 (T) 的 class 的类型参数匹配。

you dont have a route previous route, so popping leads to a black screen.您没有上一条路线,因此弹出会导致黑屏。

you can clear the fields / reset to defaults您可以清除字段/重置为默认值

`
clearFields(){
problemBox.clear();
dropdownValue="";
...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在SFML sf :: RenderWindow中只有黑屏 - I get just black screen in an SFML sf::RenderWindow MS ACCESS 2007-当所有其他表单都为全屏显示时,将小弹出表单变为非全屏显示 - MS ACCESS 2007 - Making a small pop form not full screen, when all other forms are full screen jQuery ajax弹出并在弹出表单中返回选中的复选框 - jquery ajax popup and return selected checkbox inside pop up form 如何从另一个屏幕重置Flutter中的表单? - How to reset a Form in Flutter from another screen? 模态弹出窗口在屏幕一侧闪烁。 我怎样才能解决这个问题? - Modal pop-ups flashing to the side of the screen. How can I fix this? Flutter - 验证器没有响应名称是必需的文本,按下后屏幕没有移动到下一个屏幕 - Flutter - Validator is not responding with the name is required text and the screen is not moving to next screen after the press 每次点击登录,它都会返回登录界面 - every time click login it return me to the login screen 当输入元素在特定上下文中具有焦点时,在使用回车/返回键时触发事件 - JavaScript - When An Input Element Has Focus In A Specific Context, Fire An Event On Use Of the Enter/Return Key - JavaScript 移动到另一个屏幕时,如何使下拉列表保持最后选择的选项颤动 - How can I make drop down lists keep the last selected option in flutter when moving to another screen 颤振、溢出、SingleChildScrollView。 这是一个屏幕内的表单,这次 SingleChildScollView Fix 对我有用吗? - Flutter, Overflow, SingleChildScrollView. It's a form inside a screen and SingleChildScollView Fix working for me this time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM