Flutter UI主题插件leitmotif的使用

Flutter UI主题插件leitmotif的使用

在本教程中,我们将详细介绍如何在Flutter项目中使用leitmotif插件。leitmotif是一个集合了多种自定义UI组件的Flutter插件,基于Material Design,并提供了如SnackbarAppBar和设置面板等自定义实现。


什么是Leitmotif?

Leitmotif 是一个用于Flutter的UI工具包,它实现了Leitmotif设计语言。Leitmotif补充了Flutter默认的Material Widgets,包括定制化的SnackbarsAppBars和实用屏幕(例如验证用户年龄和展示应用许可证)。此外,还包括一些辅助功能屏幕。

Leitmotif 来源于德语单词 Leitmotiv,意为音乐或文学中的主导主题。

Leitmotif 基于已停止维护的 lit_ui_kit 项目构建而成。

了解更多请访问我们的 官网

该插件由 LitLifeSoftware 开发和维护。


开始使用

导入插件

首先,确保在你的Flutter项目中添加leitmotif依赖。在项目的pubspec.yaml文件中添加以下内容:

dependencies:
  leitmotif: ^最新版本号

然后运行以下命令以更新依赖项:

flutter pub get

在代码中导入leitmotif

import 'package:leitmotif/leitmotif.dart';

使用示例

以下是一个完整的示例,展示如何使用leitmotif插件来创建一个基础的应用程序。

示例代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:leitmotif/leitmotif.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

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

class LeitmotifExample extends StatefulWidget {
  @override
  _LeitmotifExampleState createState() => _LeitmotifExampleState();
}

class _LeitmotifExampleState extends State<LeitmotifExample> {
  @override
  void initState() {
    super.initState();
    ImageCacheController(
        assetImages: ["assets/images/launcher_placeholder.png"],
        context: context);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Leitmotif',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        textTheme: LitSansSerifStyles.theme,
        useMaterial3: true,
      ),
      localizationsDelegates: const [
        LeitmotifLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: LeitmotifLocalizations.supportedLocales,
      home: ExampleHomeScreen(),
    );
  }
}

class ExampleHomeScreen extends StatefulWidget {
  @override
  _ExampleHomeScreenState createState() => _ExampleHomeScreenState();
}

class _ExampleHomeScreenState extends State<ExampleHomeScreen> {
  bool shouldHideNavigationBar = false;
  bool dark = false;

  void toggleShouldHideNavigationBar() {
    setState(() {
      shouldHideNavigationBar = !shouldHideNavigationBar;
    });
  }

  @override
  Widget build(BuildContext context) {
    return LitTabView(
      dark: dark,
      tabs: [
        LitNavigableTab(
          tabData: LitBottomNavigationItemData(
            icon: LitIcons.home_alt,
            iconAlt: LitIcons.home,
            index: 0,
            title: "home",
          ),
          screen: _ExampleScreen(
            onHideBottomNavigation: toggleShouldHideNavigationBar,
            dark: dark,
            onToggleDark: (value) {
              setState(() {
                dark = !dark;
              });
            },
          ),
        ),
        LitNavigableTab(
          tabData: LitBottomNavigationItemData(
            icon: Icons.font_download,
            iconAlt: Icons.font_download_off_outlined,
            index: 1,
            title: "Font",
          ),
          screen: _ExampleScreenTwo(),
        ),
        LitNavigableTab(
          tabData: LitBottomNavigationItemData(
            icon: LitIcons.folder_solid,
            iconAlt: LitIcons.folder,
            index: 2,
            title: "Icons",
          ),
          screen: _ExampleScreenThree(),
        ),
      ],
    );
  }
}

// 其他组件省略...

截图

以下是leitmotif的一些示例截图:

示例应用 Snackbar
1 2
Date Picker Licence Screen
3 4

本地化

leitmotif 的小部件目前支持 EnglishGerman 两种语言。要使用默认的本地化,请在 MaterialApp 中添加 LeitmotifLocalizationsDelegate

MaterialApp(
  localizationsDelegates: const [
    LeitmotifLocalizations.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ],
  supportedLocales: const [
    Locale('en', ''),
    Locale('de', ''),
  ],
  home: ExampleHomeScreen(),
);

图标

leitmotif 提供了自己的图标集 (LitIcons),这些图标存储在一个 .ttf 文件中,并作为 Dart 的 IconData 对象实现。

例如:

Icon(LitIcons.home)

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

1 回复

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


Leitmotif 是一个用于 Flutter 的 UI 主题插件,它提供了一种简单而灵活的方式来管理和应用主题。通过 Leitmotif,你可以轻松地定义和应用不同的主题,包括颜色、字体、形状等,从而为你的应用提供一致的外观和感觉。

安装

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

dependencies:
  flutter:
    sdk: flutter
  leitmotif: ^1.0.0  # 请使用最新版本

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

基本用法

1. 定义主题

你可以通过继承 LitTheme 类来定义自定义主题。例如:

import 'package:leitmotif/leitmotif.dart';

class MyCustomTheme extends LitTheme {
  const MyCustomTheme()
      : super(
          primaryColor: Colors.blue,
          secondaryColor: Colors.green,
          textTheme: TextTheme(
            headline1: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
            bodyText1: TextStyle(fontSize: 16, color: Colors.black),
          ),
        );
}

2. 应用主题

MaterialAppCupertinoApp 中,你可以使用 LitThemeProvider 来应用主题:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return LitThemeProvider(
      theme: MyCustomTheme(),
      child: MaterialApp(
        title: 'Leitmotif Example',
        home: MyHomePage(),
      ),
    );
  }
}

3. 使用主题

在应用程序的任何地方,你都可以通过 LitTheme.of(context) 来访问当前主题:

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    final theme = LitTheme.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Leitmotif Example'),
        backgroundColor: theme.primaryColor,
      ),
      body: Center(
        child: Text(
          'Hello, Leitmotif!',
          style: theme.textTheme.headline1,
        ),
      ),
    );
  }
}

动态切换主题

Leitmotif 还支持动态切换主题。你可以通过 LitThemeProviderupdateTheme 方法来更新主题:

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    final theme = LitTheme.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Leitmotif Example'),
        backgroundColor: theme.primaryColor,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Hello, Leitmotif!',
              style: theme.textTheme.headline1,
            ),
            ElevatedButton(
              onPressed: () {
                LitThemeProvider.of(context).updateTheme(AnotherCustomTheme());
              },
              child: Text('Change Theme'),
            ),
          ],
        ),
      ),
    );
  }
}
回到顶部