Как программно открыть ящик при нажатии в flutter

#flutter

#flutter

Вопрос:

Мне нужно открыть ящик приложения, нажав плавающую кнопку, но она не открывается. Пожалуйста, предложите, если есть что-то еще, кроме ящика, я хочу создать раздел фильтров

вот моя часть кода

  final GlobalKey<ScaffoldState> _scaffoldKey =
      new GlobalKey<ScaffoldState>(); // 
 drawer: Drawer(
        // Add a ListView to the drawer. This ensures the user can scroll
        // through the options in the drawer if there isn't enough vertical
        // space to fit everything.
        child: ListView(
          // Important: Remove any padding from the ListView.
          padding: EdgeInsets.zero,
          children: <Widget>[
            DrawerHeader(
              child: Text('Drawer Header'),
              decoration: BoxDecoration(
                color: Colors.blue,
              ),
            ),
            ListTile(
              title: Text('Item 1'),
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer
                Navigator.pop(context);
              },
            ),
            ListTile(
              title: Text('Item 2'),
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer
                Navigator.pop(context);
              },
            ),
          ],
        ),
      ),
      
      floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            // Add your onPressed code here!

            _scaffoldKey.currentState.openDrawer();
           
          },
          label: Text(
            'Filter',
            style:
                TextStyle(decoration: TextDecoration.none, color: Colors.black),
          ),
          icon: Icon(Icons.filter_alt, color: Colors.black),
          backgroundColor: Colors.white),
 

Я получаю сообщение об ошибке

Было выдано другое исключение: NoSuchMethodError: метод ‘openDrawer’ был вызван в null.

Ответ №1:

Вы должны передать _scaffoldKeyparamater своему эшафоту.

 Scaffold(key: _scaffoldKey);
 

Комментарии:

1. не Drawer(key: _scaffoldKey); но Scaffold(key: _scaffoldKey, ...)

2. Не проверено, но если вы так говорите, это должно быть правдой. Спасибо ^^

3. Я проверил после того, как вы сказали, что вы были правы.

Ответ №2:

На самом деле вам нужно установить ключевое свойство в scaffold.

 Scaffold(key:_scaffoldKey)
 

Затем используйте тот же метод, что и вы.