jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.25k stars 1.61k forks source link

Get.changeTheme does not work for arbitrary themes #1878

Open qbait opened 3 years ago

qbait commented 3 years ago

I've been able to change the theme for light and dark. However, I would like to give the user possibility to choose from any theme (over 10).

I created a sample with the below themes

class Themes {
  static final green = ThemeData.light().copyWith(
    buttonColor: Colors.green,
  );

  static final yellow = ThemeData.light().copyWith(
    buttonColor: Colors.yellow,
  );

  static final red = ThemeData.light().copyWith(
    buttonColor: Colors.red,
  );
}

I'm trying to change my theme in the controller by calling

Get.changeTheme(Themes.green);
Get.changeTheme(Themes.yellow);
Get.changeTheme(Themes.red);

However, it does not work. I was able to change only between 2 (light and dark).

AmineLAHRIM commented 2 years ago

also, change the system theme didn't make the app theme change from light to dark, solution 👉 to use get: 4.3.4 in pubspec.yaml instead of the latest version until the bug fixed

PepeExpress commented 2 years ago

also, change the system theme didn't make the app theme change from light to dark, solution 👉 to use get: 4.3.4 in pubspec.yaml instead of the latest version until the bug fixed

I changed get dependency to 4.3.4 but still not working. I tried few more and I found out that on 4.0.0 it works.

My code:

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

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

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

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: Themes.blackTheme,
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    Get.changeTheme(Themes.greenTheme);
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

class Themes {
  static ThemeData blackTheme = ThemeData.light().copyWith(
      floatingActionButtonTheme: FloatingActionButtonThemeData(
    backgroundColor: Colors.grey[800],
  ));

  static ThemeData greenTheme = ThemeData.light().copyWith(
      floatingActionButtonTheme: FloatingActionButtonThemeData(
    backgroundColor: Colors.green[400],
  ));
}
nik2208 commented 1 year ago

any news about this issue? I ended up using providers. It works in mobile simulators but it doesnt in the real device, web, and mac (haven't tried on windows and linux)

0xManjeet commented 1 year ago

I just faced the same issue. Here's a temporary fix if you need:

Get.rootController.darkTheme = _your_dark_theme;
Get.forceAppUpdate();