14
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Flutterでカスタムテーマを適用する

14
Posted at

Flutterで MaterialApp Widgetを使うと、 themes でテーマを設定できます。

  Widget build(BuildContext context) {
    return new MaterialApp(
      ...
      theme: new ThemeData(
        cardColor: Colors.white,
        primarySwatch: Colors.blue,
      ),
    );
  }
}

この色の指定をカスタムカラーで指定する方法です。
まず theme.dart を作ります。

import 'package:flutter/material.dart';

final ThemeData themeData = new ThemeData(
    brightness: Brightness.light,
    cardColor: Colors.white,
    dividerColor: Colors.grey[300],
    backgroundColor: Colors.grey[100],
    primarySwatch: CustomColors.theme[500],
    primaryColor: CustomColors.theme[500],
    primaryColorBrightness: Brightness.light,
    accentColor: CustomColors.accent[500]);

class CustomColors {
  CustomColors._(); // this basically makes it so you can instantiate this class
  static const Map<int, Color> theme = const <int, Color>{
    500: const Color(0xFFFBC812),
    600: const Color(0xFFFEAD22),
  };

  static const Map<int, Color> accent = const <int, Color>{
    500: const Color(0xFFF57C00),
  };
}

この themeData を指定するだけです。

import 'theme.dart';

...
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      ...
      theme: themeData,
    );
  }
}

以上。

14
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?