在Flutter中使用步进器实现注册表单时,如何解决“ [_LocalizationsScope- [GlobalKey#27fdc],_ InheritedTheme])”错误?

时间:2019-06-16 06:04:53

标签: flutter flutter-layout

我正在使用Flutter Stepper为我的应用程序实现注册表单。我想在alertdialogbox中显示每个步骤的form字段的值。对话框中显示表单字段的值。但是,它在后台显示以下错误。

flutter:在构建Builder(脏,依赖项:

颤动:[_LocalizationsScope- [GlobalKey#27fdc],_ InheritedTheme]):

flutter:'package:flutter / src / material / stepper.dart':失败的断言:第148行pos 15:'0 <= currentStep && 颤抖:currentStep

颤抖:要么断言表明框架本身有错误,要么我们应该在此错误消息中提供实质上更多的信息,以帮助您确定并解决根本原因。

flutter:无论哪种情况,请通过在GitHub上提交错误来报告此断言: 颤抖:https://github.com/flutter/flutter/issues/new?template=BUG.md

这是我尝试在步进器中实现表格的代码:

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() =>  _MyAppState();
}

class MyData {
  String name = '';
  String phone = '';
  String email = '';
  String age = '';
  String address='';

}
 MyData data = new MyData();
class _MyAppState extends State<MyApp> {
  int _currentStep = 0;
List<GlobalKey<FormState>> _formKeys = [GlobalKey<FormState>(), GlobalKey<FormState>(),GlobalKey<FormState>()];
  @override
  Widget build(BuildContext context) {


    return  MaterialApp(
      title: 'App',
      home:  Scaffold(
        appBar:  AppBar(title:  Text('App')),
        body: Builder(builder:(context)=>
         Stepper(
          type: StepperType.horizontal,
          currentStep: _currentStep,
          onStepTapped: (int step) => setState(() => _currentStep = step),
          onStepContinue:(){
             if (_formKeys[_currentStep].currentState.validate())
          {
                if (_currentStep == 0) {  
                  _formKeys[0].currentState.save(); 
                  setState(() => _currentStep ++);

          }
          else if(_currentStep == 1){
              _formKeys[1].currentState.save(); 
                  setState(() => _currentStep ++);
          }
          else if (_currentStep == 2){
            _formKeys[2].currentState.save();  
            setState(() => _currentStep ++);

            showDialog(
            context: context,
             builder: (BuildContext context) =>  AlertDialog(
              title:  Text("Details"),
              //content:  Text("Hello World"),
              content:  SingleChildScrollView(
                child:  ListBody(
                  children: <Widget>[
                     Text("Name : " + data.name),
                     Text("Phone : " + data.phone),
                     Text("Email : " + data.email),
                     Text("Age : " + data.age),
                     Text("Email : " + data.address),

                  ],
                ),
              ),
              actions: <Widget>[
                 FlatButton(
                  child:  Text('OK'),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
              ],
            ),
            );
          }

          }
          },
          onStepCancel: _currentStep > 0 ? () => setState(() => _currentStep -= 1) : null,
          steps: <Step>[
             Step(
              title:  Text('start'),
              isActive: _currentStep >= 0,
              state: _currentStep >= 0 ? StepState.complete : StepState.disabled,
               content:  Form(
            key: _formKeys[0], 
            child: TextFormField(
          keyboardType: TextInputType.text,
        //  autocorrect: false,
          onSaved: (String value) {
            data.name = value;
          },
          validator: (String value) {
            if (value.isEmpty || value.length < 1) {
              return 'Please enter name';
            }
          else{
            return null;
          }
          },
          decoration: new InputDecoration(
              labelText: 'Enter your name',
              hintText: 'Enter a name',
              //filled: true,

              labelStyle:
                  new TextStyle(decorationStyle: TextDecorationStyle.solid)),
        ),
        ),
            ),
             Step(
              title:Text('middle'),

              isActive: _currentStep >= 0,
              state: _currentStep >= 1 ? StepState.complete : StepState.disabled,
             content:  Form(
            key: _formKeys[1], 
            child:Column(children: <Widget>[
             TextFormField(
          keyboardType: TextInputType.text,
          autocorrect: false,
          onSaved: (String value) {
            data.phone= value;
          },
          validator: (String value) {
            if (value.isEmpty || value.length < 1) {
              return 'Please enter Phone number';
            }
          else{
            return null;
          }
          },
          decoration: new InputDecoration(
              labelText: 'Enter your number',
              hintText: 'Enter a number',
              //filled: true,

              labelStyle:
                  new TextStyle(decorationStyle: TextDecorationStyle.solid)),
        ), 
          TextFormField(
          keyboardType: TextInputType.text,
          autocorrect: false,
          onSaved: (String value) {
            data.email = value;
          },
          validator: (String value) {
            if (value.isEmpty || value.length < 1) {
              return 'Please enter email';
            }
          else{
            return null;
          }
          },
          decoration: new InputDecoration(
              labelText: 'Enter your email',
              hintText: 'Enter a email',
              //filled: true,
              labelStyle:
                  new TextStyle(decorationStyle: TextDecorationStyle.solid)),
        ),
         ],
        )
        ),
            ),
             Step(
              title:Text('end'), 
              isActive: _currentStep >= 0,
              state: _currentStep >= 2 ? StepState.complete : StepState.disabled,
               content: Form(
                 key:_formKeys[2], 
                 child: Column(children: <Widget>[
             TextFormField(
          keyboardType: TextInputType.text,
          autocorrect: false,
          onSaved: (String value) {
            data.age= value;
          },
          validator: (String value) {
            if (value.isEmpty || value.length < 1) {
              return 'Please enter age';
            }
          else{
            return null;
          }
          },
          decoration: new InputDecoration(
              labelText: 'Enter your age',
              hintText: 'Enter a age',
              //filled: true,
              labelStyle:
                  new TextStyle(decorationStyle: TextDecorationStyle.solid)),
        ), 
          TextFormField(
          keyboardType: TextInputType.text,
          autocorrect: false,
          onSaved: (String value) {
            data.address = value;
          },
          validator: (String value) {
            if (value.isEmpty || value.length < 1) {
              return 'Please enter address';
            }
          else{
            return null;
          }
          },
          decoration: new InputDecoration(
              labelText: 'Enter your addres',
              hintText: 'Enter a address',
              //filled: true,
              labelStyle:
                  new TextStyle(decorationStyle: TextDecorationStyle.solid)),
        ),
         ],
        ),
        ),
        ),
          ],
        ),
      ),
    ),);

  }}

0 个答案:

没有答案