1

How to change accent color on runtime in flutter?

Color scolor = Colors.green;
...
...
new MaterialApp(

      theme: new ThemeData(
        brightness: Brightness.light,
        accentColor:  scolor,
        ),
 home: new SetAccentColor());


class SetAccentColor extends StatefulWidget {
  @override
  _SetAccentColor createState() => _SetAccentColor();
}

class _SetAccentColor extends State<SetAccentColor > {
...
...
@override
  Widget build(BuildContext context) {
 return Scaffold(
        body: Builder(
            builder: (context) => Container(
                  child: FutureBuilder<List<ColorDBModel>>(
                          future: ColorDBProvider.db.getAllColorModels(),
                          builder: (BuildContext context, AsyncSnapshot<List<ColorDBModel>> snapshot) {

 if (snapshot.data[0].ColorName == "red")
scolor = Colors.red;
else 
scolor = Colors.blue;
...
...
...
}
}
}

These are the important parts of the code. But accentColor isnt changing according to the changes in scolor.

2
  • can you add the full code, which simulates your problem? Commented Feb 18, 2020 at 13:48
  • updated and simplified Commented Feb 18, 2020 at 14:09

2 Answers 2

2

you can do using provider package easily.

Make sure you add package in pubspec.yaml file.

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

class ThemeChange extends ChangeNotifier {
  Color accentColor = Colors.green;
  void changeColor(Color color) {
    this.accentColor = color;
    notifyListeners();
  }
}

void main() {
  // for bloc transition trace
  //BlocSupervisor.delegate = SimpleBlocDelegate();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<ThemeChange>(
      create: (context) => ThemeChange(),
      child: Consumer<ThemeChange>(builder: (context, themechange, child) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(accentColor: themechange.accentColor),
          initialRoute: '/',
          home: Scaffold(body: HomeWidget()),
        );
      }),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child: RaisedButton(
            onPressed: () {
              Provider.of<ThemeChange>(context, listen: false)
                  .changeColor(Colors.teal);
            },
            child: Text("change accent color"),
            color: Provider.of<ThemeChange>(context).accentColor,
          ),
        ),
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

you can do the same way as i showed to change themedata. you have to use consumer over appbar insted of current location.
@legacyO7 Accept the answer if you get what you was expecting.
Accept the answer if it woks for you.
0

Use the setState to rebuild the widget with the new color. This will work with a StatefulWidget.

onPressed: () {
  setState(() {
    scolor = Colors.red;
  });
}

4 Comments

there is no onPressed here and cant use setState as well
Why can't you use setState?
using setState didnt changed accent color
The setState will change the state of the SetAccentColor class, not the application. I can't test this now, but it is a nice question and I'll have the same problem with the theme and language changes with an app I'm building...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.