[Flutter]Drawerの背景色を設定するには?

Flutter

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

Drawerの背景色を設定する方法を紹介します。

スポンサーリンク

方法

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

Drawerの背景色を変えるには、ThemeのcanvasColorを使います。

まず、 DrawerをThemeのchildに指定します。

次に、Themeの引数「data」に「Theme.of(context).copyWith()」」を指定します。

Theme(
  data: Theme.of(context).copyWith(),
  child: Drawer(),
),

そして、copyWithの引数「canvasColor」で、Drawerの背景色を指定します。

Theme(
  data: Theme.of(context).copyWith(
    canvasColor: /*背景色*/,
  ),
  child: Drawer(),
),

このように、ThemeとcanvasColorでDrawerの背景色を設定することが出来ます。

以下は、使用例です。

使用例
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(),
        drawer: Theme(
          data: Theme.of(context).copyWith(
            canvasColor: Colors.green,
          ),
          child: Drawer(),
        ),
      ),
    );
  }

コメント

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