카테고리 없음

플러터 flutter primarySwatch 적용 안 됨 / 적용하는 방법

테토 2024. 5. 22. 00:09
반응형

 

처음으로 플러터 앱을 만들면서 시키는대로 했는데 제대로 적용되지가 않아서 구글링을 열심히 해서 찾아냈다.

 

 

수정전

 

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget{
  const MyApp({super.key});
  @override
  Widget build(BuildContext context){
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.lightBlue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

 

primarySwatch를 사용하면 테마 기본색상으로 적용되어 앱바의 색이 변경되어야하는데 무슨 색을 넣어도 흰색으로 유지되었다.

 

이유

Flutter 3.16 버전부터 기본 테마가 Meterial 3로 적용되었기 때문이다.

 

적용되지 않은 상태로 설정 후 실행하면 앱바 색이 변경된다.

 

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget{
  const MyApp({super.key});
  @override
  Widget build(BuildContext context){
    return MaterialApp(
      theme: ThemeData(
        useMaterial3: false,
        primarySwatch: Colors.lightBlue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

 

useMeterial3: false

위의 코드를 추가한다

반응형