flutter getx 更改主题颜色

get: ^4.6.5  # getx

入口

 return GetMaterialApp(
        title: 'Flutter Demo', theme: Themes.green, home: GetxSetting());

更改颜色 GetxSetting:

import 'package:flutter/material.dart';
// import 'package:flutter/src/foundation/key.dart';
// import 'package:flutter/src/widgets/framework.dart';
import 'package:get/get.dart';
import 'package:xindouyin/getx_change_theme/change_colors.dart';

// ignore: must_be_immutable
class GetxSetting extends StatelessWidget {
  GetxSetting({Key? key}) : super(key: key);

  List list = [
    Colors.green,
    Colors.yellow,
    Colors.red,
    Colors.black,
    Colors.white
  ];

  List colorList = [
    Themes.green,
    Themes.yellow,
    Themes.red,
    Themes.black,
    Themes.white,
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // backgroundColor:  ,
      appBar: AppBar(
        // backgroundColor: Themes.green,
        title: const Text("这是变换的颜色"),
      ),
      body: Column(
        children: [
          const Text("这是更换主题"),
          InkWell(
            onTap: () {
              Get.changeTheme(Themes.green);
            },
            child: Container(
              color: Colors.yellow,
              height: 200,
            ),
          ),
          const Text(" 请选择您想切换的主题"),
          Expanded(
            child: ListView.builder(
                itemCount: list.length,
                itemBuilder: (BuildContext context, int index) {
                  return InkWell(
                    onTap: () {
                      Get.changeTheme(colorList[index]);
                    },
                    child: Container(
                      height: 50,
                      decoration: BoxDecoration(
                        color: list[index],
                        border: const Border(
                          bottom: BorderSide(
                            width: 1,
                            color: Colors.pink,
                          ),
                        ),
                      ),
                    ),
                  );
                }),
          )
        ],
      ),
    );
  }
}

颜色类:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Themes {
  static final green = ThemeData.light().copyWith(
    scaffoldBackgroundColor: Colors.green,
    appBarTheme: const AppBarTheme(
        backgroundColor: Colors.green,
        titleTextStyle: TextStyle(color: Colors.white, fontSize: 30)),
  );

  static final yellow = ThemeData.light().copyWith(
    scaffoldBackgroundColor: Colors.yellow,
    appBarTheme: const AppBarTheme(
        systemOverlayStyle:
            //     // SystemUiOverlayStyle(statusBarBrightness: Brightness.dark),   // 这种不起效果
            SystemUiOverlayStyle.dark,
        backgroundColor: Colors.yellow,
        titleTextStyle: TextStyle(color: Colors.white, fontSize: 30)),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.all(Colors.deepOrange),
      ),
    ),
  );

  static final red = ThemeData.dark().copyWith(
    scaffoldBackgroundColor: Colors.red,
    appBarTheme: const AppBarTheme(
        // foregroundColor: ,
        backgroundColor: Colors.red,
        systemOverlayStyle:
            // SystemUiOverlayStyle(statusBarBrightness: Brightness.dark),   // 这种不起效果
            SystemUiOverlayStyle.dark,
        titleTextStyle: TextStyle(color: Colors.white, fontSize: 30)),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.all(Colors.brown),
      ),
    ),
  );

  static final black = ThemeData.light().copyWith(
    scaffoldBackgroundColor: Colors.black,
    appBarTheme: const AppBarTheme(
        backgroundColor: Colors.black,
        titleTextStyle: TextStyle(color: Colors.white, fontSize: 30)),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.all(Colors.brown),
      ),
    ),
  );

  static final white = ThemeData.light().copyWith(
    scaffoldBackgroundColor: Colors.white,
    appBarTheme: const AppBarTheme(
        backgroundColor: Colors.white,
        titleTextStyle: TextStyle(color: Colors.white, fontSize: 30)),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.all(Colors.brown),
      ),
    ),
  );
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值