【问题标题】:Flutter error while using drawer while switching to new page切换到新页面时使用抽屉时出现颤振错误
【发布时间】:2021-01-16 06:05:18
【问题描述】:

我试图通过做一个应用程序来学习颤振,一切都很好,但现在我在通过一个带有抽屉的新页面时遇到错误,而且,老实说,我知道为什么但是当我实现页面更改的代码时页面从代码开始到设置页面


//flutter build apk --split-per-abi
passToHomePage(BuildContext context) {
  Navigator.push(context, MaterialPageRoute(builder: (context) => HomePage()));
}

passToSettingsPage(BuildContext context) {
  Navigator.push(
      context, MaterialPageRoute(builder: (context) => SettingsStateful()));
}

Drawer drawerReturner(BuildContext context) {
  return Drawer(
    child: ListView(
      padding: EdgeInsets.zero,
      children: <Widget>[
        Container(
          height: 100,
          child: DrawerHeader(
            decoration: BoxDecoration(
              color: Colors.green,
            ),
            child: Text(
              'Menù',
              style: TextStyle(
                color: Colors.white,
                fontSize: 24,
              ),
            ),
          ),
        ),
        ListTile(
          leading: Icon(Icons.add),
          title: Text('"Add" button :D'),
          onTap: passToSettingsPage(context),
        ),
        ListTile(
          leading: Icon(Icons.settings),
          title: Text('Settings (WIP)'),
          onTap: passToSettingsPage(context),
        )
      ],
    ),
  );
}

void main() {
  runApp(MainApp());
}

class MainApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Random app',
      theme: ThemeData(
        primarySwatch: Colors.green,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({this.title = "Home page"});

  final String title;
  @override
  HomePageState createState() => HomePageState();
}

class HomePageState extends State<HomePage> {
  int counter = 0;

  void incrementCounter() {
    setState(() {
      counter++;
    });
  }

  void decrementCounter() {
    setState(() {
      if (counter > -1) {
        counter--;
      }
    });
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          if (counter > -1)
            Text('Hai cliccato il bottone $counter volte')
          else
            Text('Non puoi far arrivare il bottone a meno di 0!'),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              FloatingActionButton(
                  onPressed: incrementCounter,
                  tooltip: 'Increment',
                  heroTag: null,
                  child: Icon(Icons.add)),
              Divider(
                indent: 75,
              ),
              FloatingActionButton(
                  onPressed: decrementCounter,
                  tooltip: 'Decrement',
                  heroTag: null,
                  child: Icon(Icons.remove)),
            ],
          ),
        ],
      )),
      drawer: drawerReturner(context),
    );
  }
}

class SettingsStateful extends StatefulWidget {
  SettingsStateful({this.title = "Settings"});

  final String title;
  @override
  SettingsPage createState() => SettingsPage();
}

class SettingsPage extends State<SettingsStateful> {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: new Center(
        child: Column(children: <Widget>[Text("Hewwo")]),
      ),
      drawer: drawerReturner(context),
    );
  }
}

这里是颤振医生:

[√] Flutter (Channel stable, 1.20.4, on Microsoft Windows [Versione 10.0.19041.508], locale it-IT)
    • Flutter version 1.20.4 at C:\flutter
    • Framework revision fba99f6cf9 (2 weeks ago), 2020-09-14 15:32:52 -0700
    • Engine revision d1bc06f032
    • Dart version 2.9.2


[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at C:\Users\giuse\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.2
    • Java binary at: D:\Softwares\Android studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[!] Android Studio (version 4.0)
    • Android Studio at D:\Softwares\Android studio
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (1 available)
    • sdk gphone x86 arm (mobile) • emulator-5554 • android-x86 • Android 11 (API 30) (emulator)

! Doctor found issues in 1 category.

我知道为什么错误不再显示,但这里是“主要”错误

Failed assertion: line 4016 pos 12: '!_debugLocked': is not true.

【问题讨论】:

    标签: android flutter dart flutter-test


    【解决方案1】:

    先弹出然后推送到新屏幕。

    Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            Container(
              height: 100,
              child: DrawerHeader(
                decoration: BoxDecoration(
                  color: Colors.green,
                ),
                child: Text(
                  'Menù',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 24,
                  ),
                ),
              ),
            ),
            ListTile(
              leading: Icon(Icons.add),
              title: Text('"Add" button :D'),
              onTap: (){
                      Navigator.pop(context);
                      passToSettingsPage(context);
                 }
            ),
            ListTile(
              leading: Icon(Icons.settings),
              title: Text('Settings (WIP)'),
              onTap: (){
                      Navigator.pop(context);
                      passToSettingsPage(context);
                 }
            )
          ],
        ),
      );
    

    【讨论】:

    • 哦,谢谢你,真的很有用
    • Navigator.pop 正在弹出抽屉并推送到一个新屏幕,该屏幕也包含该抽屉,因此可能存在一些死锁,因为您没有正确弹出抽屉。感谢您接受我的回答
    • 什么都没有:D 我可以发送一个 pastebin 代码,这样你就可以看到它为什么不起作用?按钮没有被按下:(
    • 你知道为什么不@only一个问题
    • 你可以通过电报联系我吗? t.me/TEMPOTrishUna
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2019-05-19
    • 2022-06-14
    • 2020-04-01
    相关资源
    最近更新 更多