Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 98 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,78 @@
# animated_theme_switcher
# Animated_Theme_Switcher v3

[![Pub](https://img.shields.io/pub/v/animated_theme_switcher.svg)](https://pub.dartlang.org/packages/animated_theme_switcher)


Animated theme switcher.
This is an updated version that support switching theme mode and switching themes (light and dark). Both examples have been updated too.

This library starts from [Peyman's](https://stackoverflow.com/users/4910935/peyman) stackoverflow question [how-to-add-animation-for-theme-switching-in-flutter](https://stackoverflow.com/questions/60897816/how-to-add-animation-for-theme-switching-in-flutter)

![demo](demo.gif)
<img src="demo.gif" height="30%" width="30%"/>

## Getting started

Add animated_theme_switcher: "^2.0.8" in your pubspec.yaml dependencies.
Add animated_theme_switcher in your pubspec.yaml dependencies.

```yaml
dependencies:
animated_theme_switcher: "^2.0.8"
animated_theme_switcher:
git:
url: https://github.com/mllrr96/animated_theme_switcher.git
```

### How To Use
### Usage Overview

An overview on how to use it

1- Wrap your material app with ThemeProvider.

2- Wrap the screen where you wanna switch theme/theme mode with ThemeSwitchingArea.

3- Wrap every widget that handles theme switching with ThemeSwitcher.

----------------------------------------------------

### Usage

Import the following package in your dart file

```dart
import 'package:animated_theme_switcher/animated_theme_switcher.dart';
```

Wrap MaterialApp with ThemeProvider widget, as it has shown in the following example:
1. Wrap MaterialApp with ThemeProvider widget, ThemeProvider requires a ThemeModel to be passed, ThemeModel contains theme mode, light, and dark themes. if you want to persist Theme/mode then here is a good place to provide your saved theme/mode, check the examples for more info

```dart
ThemeProvider(
initTheme: initTheme,
builder: (context, myTheme) {
themeModel: ThemeModel(
themeMode: ThemeMode.system,
lightTheme: PinkTheme.light,
darkTheme: PinkTheme.dark,
),
builder: (context, themeModel) {
return MaterialApp(
title: 'Flutter Demo',
theme: myTheme,
themeMode: themeModel.themeMode,
theme: themeModel.lightTheme,
darkTheme: themeModel.darkTheme,
home: MyHomePage(),
);
}),
),
```

But if all you want is to _provide_ a theme, use as follows:
If you just need to provide a theme to a widget:

```dart
ThemeProvider(
initTheme: initTheme,
themeModel: ThemeModel(
themeMode: ThemeMode.system,
lightTheme: PinkTheme.light,
darkTheme: PinkTheme.dark,
),
child: SomeCoolPage(),
),
```

Wrap the screen where you whant to make them switch with ThemeSwitchingArea widget, as it has shown in the following example:
2. Wrap the area where you want to switch themes with ThemeSwitchingArea:


```dart
ThemeSwitchingArea(
Expand All @@ -61,55 +83,94 @@ Wrap the screen where you whant to make them switch with ThemeSwitchingArea widg
```


Wrap every switcher with ThemeSwitcher builder, and use ThemeSwitcher.of(context).changeTheme function to switch themes;
3. Wrap widgets that handle theme updates with ThemeSwitcher:

```dart
* Update Theme Mode (light, dark, system)

ThemeData newTheme = ThemeData(
primaryColor: Colors.amber
);
use ThemeSwitcher.of(context).updateThemeMode() or the shortcut context.updateThemeMode()
Tip: if you want to switch the mode from light to dark or vice versa then use ThemeSwitcher.of(context).toggleThemeMode() or the shortcut context.toggleThemeMode()

Note: To use these methods a ThemeSwitcher widget is required otherwise a null exception will be thrown.

```dart
...
ThemeSwitcher(
builder: (context) {
...
onTap: () => ThemeSwitcher.of(context).changeTheme(
theme: newTheme,
isReversed: false // default: false
onTap: () => context.updateThemeMode(
themeMode: ThemeMode.light,
);
...
},
);
```

Alternatively you could use ThemeSwitcher.switcher() or ThemeSwitcher.withTheme().
* Toggle theme mode (Switch from current theme mode to the opposite)

```dart
...
ThemeSwitcher(
builder: (context) {
...
onTap: () => context.toggleThemeMode();
...
},
);
```

Alternatively you could use ThemeSwitcher.switcher() or ThemeSwitcher.withThemeModel().
Builders of this constructors already provide you ThemeSwitcher.
ThemeSwitcher.withTheme() also provides current theme:
ThemeSwitcher.withThemeModel() also provides current ThemeModel:

```dart
ThemeSwitcher.switcher(
builder: (context, switcher) {
...
onTap: () => switcher.changeTheme(
theme: newTheme,
onTap: () => switcher.updateThemeMode(
themeMode: ThemeMode.dark,
);
...
},
);

ThemeSwitcher.withTheme(
builder: (context, switcher, theme) {
```

```dart
ThemeSwitcher.withThemeModel(
builder: (context, switcher, themeModel) {
...
onTap: () {
final currentLightTheme = themeModel.lightTheme;
final currentDarkTheme = themeModel.darkTheme;
final themeMode = themeModel.themeMode;
}
...
},
);
```

* Update Theme (light theme, dark theme)

If you want to change the theme of your application then use ThemeSwitcher.of(context).updateTheme() or the shortcut context.updateTheme(), lightTheme and darkTheme are optional, if non provide then the method will not excute.

```dart
...
ThemeSwitcher(
builder: (context) {
...
onTap: () => switcher.changeTheme(
theme: theme.brightness == Brightness.light
? darkTheme
: lightTheme,
onTap: () => context.updateTheme(
// optional light theme
lightTheme: newLightTheme,
// optional dark theme
darkTheme: newDarkTheme,
// default is false, Aniamte theme updating, when set to false the default flutter animation will be used
animateTransition: false
);
...
},
);
```

Use optional named parameter clipper to pass the custom clippers.
* Use optional named parameter clipper to pass the custom clippers.

```dart
...
Expand All @@ -123,6 +184,6 @@ Use optional named parameter clipper to pass the custom clippers.

**Notes:**

1. This package is not intended to persist selected theme on the local device. But we added [special example](https://github.com/kherel/animated_theme_switcher/blob/master/example/lib/with_saving_theme.dart) to show how to do it using [shared_preferences](https://pub.dev/packages/shared_preferences) package.
1. This package is not intended to persist selected theme on the local device. But we added [special example](https://github.com/mllrr96/animated_theme_switcher/blob/master/example/lib/with_saving_theme.dart) to show how to do it using [shared_preferences](https://pub.dev/packages/shared_preferences) package.

2. Use the CanvasKit rendering engine to use it on **web**, [more about it..](https://github.com/kherel/animated_theme_switcher/issues/23)
Binary file modified demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.pub-cache/
.pub/
/build/

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
30 changes: 30 additions & 0 deletions example/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "761747bfc538b5af34aa0d3fac380f1bc331ec49"
channel: "stable"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
- platform: android
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
16 changes: 16 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# example

A new Flutter project.

## Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)

For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
Loading