Flutter ThemeDataプライマリカラーを追加しようとしても、テーマからプライマリカラーが変更されない質問する

Flutter ThemeDataプライマリカラーを追加しようとしても、テーマからプライマリカラーが変更されない質問する

私は LinkedIn Learning の London App Brewery の BMI Calculator アプリをフォローしています。primaryColor を赤に設定しようとすると、Primary Color を上書きしているにもかかわらず、エミュレーターにはライトブルーのデフォルトの AppBar が表示されます。これがコードです。

    import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.red,
      ),
      home: const InputPage(),
    );
  }
}

class InputPage extends StatefulWidget {
  const InputPage({Key? key}) : super(key: key);

  @override
  _InputPageState createState() => _InputPageState();
}

class _InputPageState extends State<InputPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BMI CALCULATOR'),
      ),
      body: const Center(
        child: Text('Body Text'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: const Icon(Icons.add),
      ),
    );
  }
}

ベストアンサー1

私も LondonAppBrewery の同じトレーニングに参加しています。このコードで問題は解決しました。

Widget build(BuildContext context) {
return MaterialApp(
  title: "BMI Calculator",
  debugShowCheckedModeBanner: false,
  theme: ThemeData.dark().copyWith(
    appBarTheme:AppBarTheme(
      backgroundColor: Color(0xff0a0e21),
    ),
    scaffoldBackgroundColor: Color(0xff0a0e21),
  ),
  home: InputPage(),
);

おすすめ記事