Flutter error while using drawer while switching to new page

996 views Asked by At

im trying to learn flutter by doing an app, everything was good, but now i get an error while passing trougth a new page with a drawer, and, to be honest idk why but when i implement good the code for page changing the page start to the settings page here down the code


//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),
    );
  }
}

Here the flutter doctor:

[√] 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.

And idk why but errors arent anymore displayed, but here the "main" error

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

1

There are 1 answers

5
Abhishek Ghaskata On BEST ANSWER

first pop then pushes to a new screen.

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);
             }
        )
      ],
    ),
  );