代码之家  ›  专栏  ›  技术社区  ›  mikezang

如何解决main.dart中“ParentDataWidget.使用不正确”的问题

  •  0
  • mikezang  · 技术社区  · 5 年前

    我有main.dart如下:

    //main.dart
    import 'dart:io';
    import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    import 'src/loginPage.dart';
    import 'src/tabsPage.dart';
    
    void main() async {
      Widget _defaultPage = new LoginPage();
      runApp(MyApp(defaultPage: _defaultPage));
    }
    
    class MyApp extends StatelessWidget {
      final Widget defaultPage;
    
      const MyApp({Key key, @required this.defaultPage}) : super(key: key);
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Gaia',
          debugShowCheckedModeBanner: false,
          home: defaultPage,
          routes: <String, WidgetBuilder>{
            '/tabs': (BuildContext context) => new TabsPage(),
            '/login': (BuildContext context) => new LoginPage(),
          },
        );
      }
    }
    
    //LoginPage.dart
    import 'package:flutter/material.dart';
    import '../main.dart';
    
    class LoginPage extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => new _State();
    }
    
    class _State extends State<LoginPage> {
      TextEditingController userIdController = TextEditingController();
      TextEditingController passwordController = TextEditingController();
    
      void login() async {
         Navigator.of(context).pushReplacementNamed('/tabs');
      }
    
      void forgotPassword() {
        print("Forgot password!");
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: Padding(
                padding: EdgeInsets.all(10),
                child: ListView(
                  children: <Widget>[
                    Container(
                        alignment: Alignment.center,
                        padding: EdgeInsets.all(40),
                        child: Text(
                          'Gaia',
                          style: TextStyle(
                              color: Colors.blue,
                              fontWeight: FontWeight.w500,
                              fontSize: 30),
                        )),
                    Container(
                      padding: EdgeInsets.all(10),
                      child: TextField(
                        style: TextStyle(fontSize: 25),
                        maxLength: 6,
                        keyboardType: TextInputType.number,
                        controller: userIdController,
                        decoration: InputDecoration(
                          border: OutlineInputBorder(),
                          labelText: 'User Id',
                          labelStyle: TextStyle(fontSize: 25),
                          counter: Spacer(),
                        ),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
                      child: TextField(
                        style: TextStyle(fontSize: 25),
                        obscureText: true,
                        controller: passwordController,
                        decoration: InputDecoration(
                            border: OutlineInputBorder(),
                            labelText: 'password',
                            labelStyle: TextStyle(fontSize: 25)),
                      ),
                    ),
                    SizedBox(height: 50),
                    Container(
                        padding: EdgeInsets.fromLTRB(100, 30, 100, 0),
                        child: RaisedButton(
                            child: Text(
                              'ログイン',
                              style: TextStyle(fontSize: 30),
                            ),
                            textColor: Colors.white,
                            color: Colors.blue,
                            onPressed: () {
                              login();
                            })),
                    SizedBox(height: 120),
                    FlatButton(
                      child: Text('Forgot password'),
                      onPressed: () {
                        forgotPassword();
                      },
                      textColor: Colors.blue,
                    ),
                    Container(
                      alignment: Alignment.center,
                      child: Text('Information'),
                    ),
                    Container(
                      alignment: Alignment.center,
                      child: Text('Here'),
                    ),
                  ],
                )));
      }
    }
    

    当我运行它并显示警告消息时,我能解决什么问题?

    ════════ Exception caught by widgets library ═══════════════════════════════════
    The following assertion was thrown while applying parent data.:
    Incorrect use of ParentDataWidget.
    
    The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type BoxParentData.
    
    Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets.
    The offending Expanded is currently placed inside a _Decorator widget.
    
    The ownership chain for the RenderObject that received the incompatible parent data was:
      SizedBox.shrink ← Expanded ← Spacer ← _Decorator ← InputDecorator ← AnimatedBuilder ← _PointerListener ← Listener ← RawGestureDetector ← TextSelectionGestureDetector ← ⋯
    When the exception was thrown, this was the stack
    #0      RenderObjectElement._updateParentData.<anonymous closure>
    package:flutter/…/widgets/framework.dart:5770
    #1      RenderObjectElement._updateParentData
    package:flutter/…/widgets/framework.dart:5786
    #2      RenderObjectElement.attachRenderObject
    package:flutter/…/widgets/framework.dart:5808
    #3      RenderObjectElement.mount
    package:flutter/…/widgets/framework.dart:5501
    #4      SingleChildRenderObjectElement.mount
    package:flutter/…/widgets/framework.dart:6117
    ...
    
    0 回复  |  直到 5 年前