简体   繁体   English

Flutter - 多个小部件捕获的异常

[英]Flutter - Exception Caught by multiple widgets

Hi I'm trying to add a drawer to my scaffold with the help of https://flutter.dev/docs/cookbook/design/drawer嗨,我正在尝试在https://flutter.dev/docs/cookbook/design/drawer的帮助下在我的脚手架上添加一个抽屉

And so far I'm getting multiple errors when I try to use it (and found 2, I don't know if there is more).到目前为止,当我尝试使用它时,我遇到了多个错误(发现了 2 个,我不知道是否还有更多错误)。

Code:代码:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        child: Row(
          children: <Widget>[
            IconButton(icon: Icon(Icons.add), onPressed: () {}),
            ListView(
              padding: EdgeInsets.zero,
              children: <Widget>[
                DrawerHeader(
                  child: Text(
                    "What's up?",
                    style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 30),
                  ),
                  decoration: BoxDecoration(color: Color(0xff171719)),
                ),
                ListTile(
                  title: Text(
                    "Change Theme",
                    style: TextStyle(fontSize: 24),
                  ),
                  // ignore: todo
                  onTap: () {}, //TODO add dark mode
                ),
                ListTile(
                  title: Text(
                    "Sign Out",
                    style: TextStyle(fontSize: 24),
                  ),
                  onTap: () {
                    AuthMethods().signOut().then(
                      (s) {
                        Navigator.pushReplacement(context,
                            MaterialPageRoute(builder: (context) => SignIn()));
                        Navigator.pop(context);
                      },
                    );
                    // ignore: todo
                  }, //TODO sign out
                ),
              ],
            ),
          ],
        ),
      ),

Exception caught by gesture:手势捕获的异常:

手势捕获的异常

Exception caught by rendering library:渲染库捕获的异常:

渲染库捕获的异常

应用程序视图

You should put your ListView inside a Widget that will constraint the ListView vertically, such as Expanded:您应该将 ListView 放在一个 Widget 中,该 Widget 将垂直约束 ListView,例如 Expanded:

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(body: MyWidget()),
    ),
  );
}

class MyWidget extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final _drawerKey = useState<GlobalKey<ScaffoldState>>(GlobalKey());
    return Scaffold(
      key: _drawerKey.value,
      drawer: Drawer(
        child: Row(
          children: <Widget>[
            IconButton(icon: Icon(Icons.add), onPressed: () {}),
            Expanded(
              child: ListView(
                padding: EdgeInsets.zero,
                children: <Widget>[
                  DrawerHeader(
                    child: Text(
                      "What's up?",
                      style: TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.bold,
                          fontSize: 30),
                    ),
                    decoration: BoxDecoration(color: Color(0xff171719)),
                  ),
                  ListTile(
                    title: Text(
                      "Change Theme",
                      style: TextStyle(fontSize: 24),
                    ),
                    // ignore: todo
                    onTap: () {}, //TODO add dark mode
                  ),
                  ListTile(
                    title: Text(
                      "Sign Out",
                      style: TextStyle(fontSize: 24),
                    ),
                    onTap: () {
                      print('SIGN OUT');
                    }, //TODO sign out
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _drawerKey.value.currentState.openDrawer(),
      ),
    );
  }
}

I couldn't reproduce it on my end, but this usually means that there's a widget whose viewport doesn't have the dimensions established.我无法最终重现它,但这通常意味着有一个小部件的视口没有建立尺寸。

This generally happens when you add a ListView directly to a Row or Column.当您将ListView直接添加到 Row 或 Column 时,通常会发生这种情况。 Y would suggest wrapping your ListView with an Expanded widget (or a Container). Y 建议使用Expanded小部件(或容器)包装您的 ListView。

  • Solution 1: Set ListView inside Expanded解决方案一:在 Expanded 内设置 ListView
  • Solution 2: ListView with the attribute: shrinkWrap: true,解决方案2:ListView 属性为:shrinkWrap: true,

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM