Skip to content

Instantly share code, notes, and snippets.

@GAM3RG33K
Last active December 3, 2019 09:01
Show Gist options
  • Save GAM3RG33K/0eee874907603aead06c48998b56e396 to your computer and use it in GitHub Desktop.
Save GAM3RG33K/0eee874907603aead06c48998b56e396 to your computer and use it in GitHub Desktop.
Image asset Theme color Issue

#prerequisite

  • Flutter SDK : master branch
    Flutter 1.12.15-pre.26 • channel master • https://github.com/flutter/flutter.git
    Framework • revision 1b835a722b (6 days ago) • 2019-11-26 23:48:51 -0500
    Engine • revision e3e5f8dabc
    Tools • Dart 2.7.0
    

#setup

  • create a new flutter project with name: theme_asset_tint_demo
  • replace the content of main.dart with the one provided here
  • replace the content of pubspec.yaml file with the one provided here
  • add a new folder 'images' with an image file and replace the image name in asset accordingly

#Steps to reproduce

  • run the app on mobile device or web
  • increment the counter twice to change the theme of app
  • observe, when the theme changes, the existing floating action button color changes along with the icon's color but no color changes in the one with ImageIcon.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
ThemeData __theme = ThemeData.light();
ValueNotifier<ThemeData> themeData = ValueNotifier<ThemeData>(__theme);
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MyHomePage(
title: 'Flutter Demo Home Page',
themeData: themeData,
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title, this.themeData}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
final ValueNotifier<ThemeData> themeData;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void toggleTheme() {
final newTheme = themeData.value.brightness == Brightness.dark
? ThemeData.light()
: ThemeData.dark();
print('''newTheme :
{\ntheme.brightness: ${newTheme.brightness}, \ntheme.iconColor: ${newTheme.iconTheme.color},\n}
''');
themeData.value = newTheme;
}
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
if (_counter % 2 == 0) {
toggleTheme();
}
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
/**
* return ChangeNotifierProvider<ValueNotifier<ThemeData>>.value(
notifier: themeData,
child: Consumer<ThemeData>(
builder: (context, currentTheme, child) => MaterialApp(
*
*/
return ChangeNotifierProvider<ValueNotifier<ThemeData>>(
builder: (_) => themeData,
child: Consumer<ValueNotifier<ThemeData>>(
builder: (_, themeDataProvider, __) => MaterialApp(
title: 'Flutter Demo',
theme: themeDataProvider.value,
home: Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: TextStyle(
fontSize: 24.0,
),
),
],
),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
FloatingActionButton(
child: IconButton(
color: Theme.of(context).iconTheme.color,
onPressed: () {
_incrementCounter();
setState(() {});
},
icon: ImageIcon(
Image.asset('images/ic_add_custom.png').image,
color: Theme.of(context).iconTheme.color,
),
),
onPressed: () {},
tooltip: 'Increment(Custom)',
),
FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
],
), // This trailing comma makes auto-formatting nicer for build methods.
),
),
),
);
}
}
name: theme_asset_tint_demo
description: A new Flutter project.
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
provider: ^2.0.1
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- images/ic_add_custom.png
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment