Flutter主题切换插件yako_theme_switch的使用

Flutter主题切换插件yako_theme_switch的使用

Yako Theme Switch 是一个用于在应用程序中轻松切换主题的插件。它提供了一个漂亮的开关组件来改变应用的主题。

展示动画

安装

首先,在你的 pubspec.yaml 文件中添加以下依赖项:

dependencies:
  yako_theme_switch: ^1.0.0

然后运行 flutter pub get 来安装该包。

基本用法

以下是一个基本示例,展示如何使用 YakoThemeSwitch 组件:

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

void main() {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  [@override](/user/override)
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  ThemeMode themeMode = ThemeMode.light;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      darkTheme: ThemeData(scaffoldBackgroundColor: Colors.black),
      themeMode: themeMode,
      home: Scaffold(
        body: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              YakoThemeSwitch(
                enabled: themeMode == ThemeMode.light,
                onChanged: ({bool? changed}) {
                  if (changed != null) {
                    setState(() {
                      themeMode = changed ? ThemeMode.light : ThemeMode.dark;
                    });
                  }
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

高级用法

除了基本用法外,你还可以自定义 YakoThemeSwitch 的外观和行为。例如,你可以设置宽度、背景颜色、切换颜色等属性。

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

void main() {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  [@override](/user/override)
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  ThemeMode themeMode = ThemeMode.light;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      darkTheme: ThemeData(scaffoldBackgroundColor: Colors.black),
      themeMode: themeMode,
      home: Scaffold(
        body: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              YakoThemeSwitch(
                // 是否启用(根据当前主题模式)
                enabled: themeMode == ThemeMode.light,
                // 切换主题时的回调函数
                onChanged: ({bool? changed}) {
                  if (changed != null) {
                    setState(() {
                      themeMode = changed ? ThemeMode.light : ThemeMode.dark;
                    });
                  }
                },
                // 自定义宽度
                width: 50,
                // 启用时的背景颜色
                enabledBackgroundColor: Colors.blue,
                // 禁用时的背景颜色
                disabledBackgroundColor: Colors.red,
                // 启用时的切换颜色
                enabledToggleColor: Colors.white,
                // 禁用时的切换颜色
                disabledToggleColor: Colors.white,
                // 动画持续时间
                animationDuration: const Duration(milliseconds: 300),
                // 启用时的切换圆角半径
                enabledToggleBorderRadius: 8,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter主题切换插件yako_theme_switch的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter主题切换插件yako_theme_switch的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter应用中使用yako_theme_switch插件来实现主题切换的一个简单示例。这个插件可以帮助你轻松地在应用中实现明暗主题的切换。

首先,确保你已经在pubspec.yaml文件中添加了yako_theme_switch依赖:

dependencies:
  flutter:
    sdk: flutter
  yako_theme_switch: ^最新版本号 # 请替换为实际的最新版本号

然后,运行flutter pub get来安装依赖。

接下来,我们创建一个简单的Flutter应用,展示如何使用yako_theme_switch插件。

main.dart

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Theme Switch Example',
      theme: ThemeData(
        brightness: Brightness.light,
        primarySwatch: Colors.blue,
      ),
      darkTheme: ThemeData(
        brightness: Brightness.dark,
        primarySwatch: Colors.blueGrey,
      ),
      themeMode: ThemeMode.system, // 初始主题模式,可以是 system, light, dark
      builder: (context, child) {
        return ThemeProvider(
          child: child,
          saveThemeMode: true, // 是否保存主题模式到 SharedPreferences
        );
      },
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final themeProvider = ThemeProvider.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Theme Switch Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Current Theme Mode: ${themeProvider.themeMode == ThemeMode.light ? 'Light' : themeProvider.themeMode == ThemeMode.dark ? 'Dark' : 'System'}',
              style: TextStyle(fontSize: 20),
            ),
            SizedBox(height: 20),
            ThemeSwitch(
              onChanged: (ThemeMode mode) {
                themeProvider.setThemeMode(mode);
              },
              builder: (context, isOn) {
                return Icon(
                  isOn ? Icons.dark_mode : Icons.light_mode,
                  size: 50,
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

解释

  1. 依赖添加:在pubspec.yaml中添加yako_theme_switch依赖。
  2. MaterialApp:在MaterialApp中设置themedarkTheme属性来定义亮暗主题。themeMode属性设置为ThemeMode.system,表示默认跟随系统主题模式。
  3. ThemeProvider:使用ThemeProvider包装应用,并设置saveThemeModetrue,以便保存主题模式到SharedPreferences
  4. HomePage:在HomePage中,使用ThemeProvider.of(context)获取当前主题模式,并使用ThemeSwitch组件来切换主题。ThemeSwitchonChanged回调用于更新主题模式。

这个示例展示了如何使用yako_theme_switch插件来轻松实现Flutter应用中的主题切换功能。你可以根据需要进行进一步的自定义和扩展。

回到顶部