Flutter材质霓虹主题插件material_neumorphic_theme的使用

发布于 1周前 作者 eggper 来自 Flutter

Flutter材质霓虹主题插件material_neumorphic_theme的使用

material_neumorphic_theme 是一个用于在 Flutter 应用中设置霓虹风格主题的插件。该插件允许开发者自定义颜色、深度、强度、光源等属性。

使用方法

以下是一个完整的示例代码,展示了如何在 Flutter 应用中使用 material_neumorphic_theme 插件:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 定义亮色主题的颜色方案
    final colorScheme = ColorScheme.fromSeed(
      brightness: Brightness.light,
      seedColor: NeumorphicTheme.defaultSeedColor,
    );

    // 定义暗色主题的颜色方案
    final darkColorScheme = ColorScheme.fromSeed(
      brightness: Brightness.dark,
      seedColor: NeumorphicTheme.defaultSeedColor,
    );

    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Neumorphic Theme Demo',
      theme: ThemeData(
        colorScheme: colorScheme,
        useMaterial3: true,
      ).copyWith(
        extensions: <ThemeExtension<dynamic>>[
          const NeumorphicTheme().fitWithColorSchema(colorScheme),
        ],
      ),
      darkTheme: ThemeData(
        colorScheme: darkColorScheme,
        useMaterial3: true,
      ).copyWith(
        extensions: <ThemeExtension<dynamic>>[
          const NeumorphicTheme().fitWithColorSchema(darkColorScheme),
        ],
      ),
      home: const MyHomePage(),
    );
  }
}

// 主页面
class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Neumorphic Theme Demo'),
      ),
      body: Center(
        child: Text(
          'Hello, Neumorphic Theme!',
          style: TextStyle(fontSize: 24),
        ),
      ),
    );
  }
}

解释

  1. 导入必要的包

    import 'package:flutter/material.dart';
    import 'package:material_neumorphic_theme/material_neumorphic_theme.dart';
    
  2. 定义亮色和暗色主题的颜色方案

    final colorScheme = ColorScheme.fromSeed(
      brightness: Brightness.light,
      seedColor: NeumorphicTheme.defaultSeedColor,
    );
    
    final darkColorScheme = ColorScheme.fromSeed(
      brightness: Brightness.dark,
      seedColor: NeumorphicTheme.defaultSeedColor,
    );
    
  3. 配置 MaterialApp 并设置主题

    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Neumorphic Theme Demo',
      theme: ThemeData(
        colorScheme: colorScheme,
        useMaterial3: true,
      ).copyWith(
        extensions: <ThemeExtension<dynamic>>[
          const NeumorphicTheme().fitWithColorSchema(colorScheme),
        ],
      ),
      darkTheme: ThemeData(
        colorScheme: darkColorScheme,
        useMaterial3: true,
      ).copyWith(
        extensions: <ThemeExtension<dynamic>>[
          const NeumorphicTheme().fitWithColorSchema(darkColorScheme),
        ],
      ),
      home: const MyHomePage(),
    );
    
  4. 创建主页面并展示文本

    class MyHomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Neumorphic Theme Demo'),
          ),
          body: Center(
            child: Text(
              'Hello, Neumorphic Theme!',
              style: TextStyle(fontSize: 24),
            ),
          ),
        );
      }
    }
    

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

1 回复

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


当然,以下是如何在Flutter应用中使用material_neumorphic_theme插件来实现霓虹主题的一个示例代码。这个插件允许你创建具有深度和立体感的材质设计,非常适合创建霓虹风格的用户界面。

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

dependencies:
  flutter:
    sdk: flutter
  material_neumorphic_theme: ^latest_version  # 请替换为最新版本号

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

接下来,是一个完整的示例代码,展示了如何使用material_neumorphic_theme插件来创建一个简单的霓虹主题应用:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Neumorphic Theme Demo',
      theme: ThemeData(
        // 使用NeumorphicThemeData来定义主题
        primarySwatch: Colors.blue,
      ),
      home: NeumorphicTheme(
        theme: NeumorphicThemeData(
          // 定义霓虹主题的颜色和深度
          baseColor: Color(0xFF2C3E50),
          lightSource: LightSource.topLeft,
          intensity: 0.6,
        ),
        child: NeumorphicScaffold(
          appBar: NeumorphicAppBar(
            title: Text('Neumorphic Theme Demo'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                NeumorphicButton(
                  onPressed: () {},
                  child: Text('Neumorphic Button'),
                  style: NeumorphicStyle(
                    color: Color(0xFFFF0000).withOpacity(0.7), // 红色霓虹按钮
                    depth: 10,
                  ),
                ),
                SizedBox(height: 20),
                NeumorphicCard(
                  child: Container(
                    height: 150,
                    width: 150,
                    color: Colors.transparent,
                    child: Center(
                      child: Text(
                        'Neumorphic Card',
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                  margin: EdgeInsets.all(16),
                  style: NeumorphicStyle(
                    color: Color(0xFF00FFFF).withOpacity(0.5), // 青色霓虹卡片
                    depth: 6,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

在这个示例中:

  1. 我们使用了NeumorphicTheme来包裹整个应用,并定义了基本的霓虹主题颜色、光源位置和强度。
  2. NeumorphicScaffold用于创建一个具有霓虹风格的支架。
  3. NeumorphicAppBar用于创建一个具有霓虹风格的应用栏。
  4. NeumorphicButtonNeumorphicCard分别创建了具有霓虹风格的按钮和卡片。你可以通过调整NeumorphicStyle中的colordepth属性来改变它们的外观。

这个示例代码提供了一个基础框架,你可以根据需要进一步定制和扩展以实现你想要的霓虹主题效果。

回到顶部