[Flutter]Drawerを閉じるボタンを追加するには?

Flutter

どうも、ちょげ(@chogetarou)です。

Drawerを閉じるボタンを追加する方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

Drwerを閉じるボタンを追加するには、Navigator.pop(context)を使います。

まず、ElevatedButtonやTextButtonなどのボタンを用意します。

そして、ボタンの引数「onPressed」で、Navigator.pop(context)を呼び出します。

TextButton(
  onPressed: () {
    Navigator.pop(context);
  },
  child: Text('閉じる'),
),

これをDrawer内に配置すれば、Drawerを閉じるボタンを追加することが出来ます。

以下は、使用例です。

使用例
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(),
        drawer: Drawer(
          child: TextButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text('閉じる'),
          ),
        ),
      ),
    );
  }

コメント

タイトルとURLをコピーしました