Flutter主题管理插件easydev_theme的使用

Flutter主题管理插件easydev_theme的使用

easydev_theme

此包包含来自EasyDev UI Kit的装饰小部件的主题。

此包包括来自以下包的主题:easydev_basics, easydev_base_uieasydev_forms。这些包中的大多数小部件必须用自包中的EasyDevTheme进行包装以设置其样式参数。

安装

要使用此包,在您的pubspec.yaml文件中添加easydev_theme作为依赖项。

dependencies:
  easydev_theme: ^x.x.x

使用

在代码中导入该包:

import 'package:easydev_theme/easydev_theme.dart';

当使用EasyDevApp(来自easydev_basics)时,可以将浅色和深色的EasyDevThemeData添加到相应的字段中。此包包含两个可以使用的默认主题数据:defaultLightThemedefaultDarkTheme

EasyDevApp(
  // 其他配置...
  darkThemeData: defaultDarkTheme,
  lightThemeData: defaultLightTheme,
)

另外,可以直接使用EasyDevTheme来为小部件提供样式。同时,当使用MaterialAppCupertinoApp时,EasyDevTheme会自动解析。

基础

EasyDevThemeData 包含可以通过整个应用应用的公共元素,例如 ColorPaletteTextThemeEasyDevIconThemeData。它还包含可以应用于来自EasyDev UI Kit的特定小部件的具体主题。这些主题如下所示。

EasyDevThemeData 包含来自 easydev_base_ui 的小部件的主题:

  • EasyDevButtonThemeData
  • EasyDevCarouselThemeData
  • EasyDevInputThemeData
  • EasyDevTabsThemeData
  • EasyDevActionSheetThemeData
  • EasyDevActionMenuThemeData

EasyDevThemeData 包含来自 easydev_basics 的小部件的主题:

  • EasyDevAppBarThemeData
  • EasyDevBottomNavigationBarThemeData
  • EasyDevDialogThemeData
  • EasyDevSnackbarThemeData
  • EasyDevToastThemeData
  • EasyDevFloatingActionButtonThemeData
  • EasyDevBottomSheetThemeData

EasyDevThemeData 包含来自 easydev_forms 的小部件的主题:

  • EasyDevSelectablesThemeData
  • EasyDevSwitchThemeData
  • EasyDevCodeInputThemeData
  • EasyDevInputNumberThemeData
  • EasyDevSelectThemeData
  • EasyDevMultiselectThemeData
  • EasyDevSliderThemeData
  • EasyDevDatePickerDialogThemeData
  • EasyDevCalendarThemeData
  • EasyDevDateInputThemeData
  • EasyDevChipsThemeData

完整示例Demo

以下是一个完整的示例代码,展示了如何使用easydev_theme插件。

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return EasyDevApp(
      title: 'EasyDev Theme Demo',
      darkThemeData: defaultDarkTheme,
      lightThemeData: defaultLightTheme,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('EasyDev Theme Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            // 示例按钮
            EasyDevButton(
              text: '点击我',
              onPressed: () {},
            ),
            // 示例输入框
            EasyDevInput(
              hintText: '请输入内容',
              onChanged: (value) {},
            ),
            // 示例开关
            EasyDevSwitch(
              value: false,
              onChanged: (bool newValue) {},
            ),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


easydev_theme 是一个用于 Flutter 应用主题管理的插件,它可以帮助你轻松地管理和切换应用的主题。通过使用 easydev_theme,你可以定义多个主题,并在运行时动态切换它们。以下是如何使用 easydev_theme 的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 easydev_theme 依赖:

dependencies:
  flutter:
    sdk: flutter
  easydev_theme: ^0.1.0  # 请使用最新版本

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

2. 定义主题

接下来,你需要定义你的主题。easydev_theme 允许你定义多个主题,并在应用中使用它们。

import 'package:easydev_theme/easydev_theme.dart';

final lightTheme = EasyDevThemeData(
  brightness: Brightness.light,
  primaryColor: Colors.blue,
  accentColor: Colors.blueAccent,
  // 其他自定义颜色和样式
);

final darkTheme = EasyDevThemeData(
  brightness: Brightness.dark,
  primaryColor: Colors.indigo,
  accentColor: Colors.indigoAccent,
  // 其他自定义颜色和样式
);

3. 使用 EasyDevThemeProvider

在你的应用的根组件中,使用 EasyDevThemeProvider 来包裹你的应用,并传入默认的主题。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return EasyDevThemeProvider(
      theme: lightTheme, // 默认主题
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData.light(), // MaterialApp 的主题
        home: MyHomePage(),
      ),
    );
  }
}

4. 获取和切换主题

在你的应用中,你可以使用 EasyDevTheme.of(context) 来获取当前的主题,并使用 EasyDevThemeProvider.of(context).changeTheme(newTheme) 来切换主题。

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

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final theme = EasyDevTheme.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('EasyDev Theme Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Current Theme: ${theme.brightness == Brightness.light ? "Light" : "Dark"}',
              style: TextStyle(fontSize: 24),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                final newTheme = theme.brightness == Brightness.light ? darkTheme : lightTheme;
                EasyDevThemeProvider.of(context).changeTheme(newTheme);
              },
              child: Text('Switch Theme'),
            ),
          ],
        ),
      ),
    );
  }
}

5. 自定义主题

你可以在 EasyDevThemeData 中自定义更多的颜色、样式等,以满足你的应用需求。

final customTheme = EasyDevThemeData(
  brightness: Brightness.light,
  primaryColor: Colors.purple,
  accentColor: Colors.purpleAccent,
  textTheme: TextTheme(
    headline1: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
    bodyText1: TextStyle(fontSize: 16, color: Colors.black87),
    // 其他文本样式
  ),
  // 其他自定义样式
);
回到顶部