Flutter拟物风格UI插件flutter_neumorphic_null_safety的使用

Flutter拟物风格UI插件flutter_neumorphic_null_safety的使用

安装

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

dependencies:
  flutter_neumorphic: ^3.0.3

然后在你的.dart文件中导入该库:

import 'package:flutter_neumorphic_null_safety/flutter_neumorphic.dart';

小部件

Neumorphic

Neumorphic 是主要的拟物化小部件,它会根据光源和深度添加白色或暗色渐变。

Neumorphic

Neumorphic(
  style: NeumorphicStyle(
    shape: NeumorphicShape.concave,
    boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(12)), 
    depth: 8,
    lightSource: LightSource.topLeft,
    color: Colors.grey
  ),
  child: Container(
    height: 100,
    width: 100,
    color: Colors.white,
  ),
)

NeumorphicButton

NeumorphicButton 是一个可以响应用户交互的拟物化按钮。

NeumorphicButton

NeumorphicButton(
  onPressed: () {
    print("Button pressed");
  },
  style: NeumorphicStyle(
    shape: NeumorphicShape.concave,
    boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(8)),
    depth: 8,
    lightSource: LightSource.topLeft,
    color: Colors.grey,
  ),
  padding: const EdgeInsets.all(12.0),
  child: Text("Press Me"),
)

NeumorphicText

NeumorphicText 是一个拟物化的文本组件(仅支持正深度)。

NeumorphicText

NeumorphicText(
  "Hello Flutter",
  style: NeumorphicStyle(
    depth: 4,
    color: Colors.white,
  ),
  textStyle: NeumorphicTextStyle(
    fontSize: 18,
  ),
)

NeumorphicIcon

NeumorphicIcon 是一个拟物化的图标组件。

NeumorphicIcon

NeumorphicIcon(
  Icons.add_circle,
  size: 80,
)

NeumorphicSwitch

NeumorphicSwitch 是一个开关按钮,当激活时会采用强调色。

NeumorphicSwitch

NeumorphicSwitch(
  value: true,
  onChanged: (value) {
    print("Switch changed to $value");
  },
  style: NeumorphicSwitchStyle(
    activeTrackColor: Colors.green,
    inactiveTrackColor: Colors.grey,
    thumbColor: Colors.white,
  ),
)

NeumorphicApp

NeumorphicApp 是一个直接在项目中使用的应用,它处理了主题和导航。

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return NeumorphicApp(
      debugShowCheckedModeBanner: false,
      title: 'Neumorphic App',
      themeMode: ThemeMode.light,
      theme: NeumorphicThemeData(
        baseColor: Color(0xFFFFFFFF),
        lightSource: LightSource.topLeft,
        depth: 10,
      ),
      darkTheme: NeumorphicThemeData(
        baseColor: Color(0xFF3E3E3E),
        lightSource: LightSource.topLeft,
        depth: 6,
      ),
      home: MyHomePage(),
    );
  }
}

示例代码

以下是一个完整的示例代码,展示了如何使用 flutter_neumorphic_null_safety 插件创建一个简单的拟物风格界面。

import 'package:flutter/material.dart';
import 'package:flutter_neumorphic_null_safety/flutter_neumorphic.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return NeumorphicApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      themeMode: ThemeMode.light,
      theme: NeumorphicThemeData(
        baseColor: Color(0xFFFFFFFF),
        lightSource: LightSource.topLeft,
        depth: 10,
      ),
      darkTheme: NeumorphicThemeData(
        baseColor: Color(0xFF3E3E3E),
        lightSource: LightSource.topLeft,
        depth: 6,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: NeumorphicFloatingActionButton(
        child: Icon(Icons.add, size: 30),
        onPressed: () {},
      ),
      backgroundColor: NeumorphicTheme.baseColor(context),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            NeumorphicButton(
              onPressed: () {
                print("onClick");
              },
              style: NeumorphicStyle(
                shape: NeumorphicShape.flat,
                boxShape: NeumorphicBoxShape.circle(),
              ),
              padding: const EdgeInsets.all(12.0),
              child: Icon(
                Icons.favorite_border,
                color: _iconsColor(context),
              ),
            ),
            NeumorphicButton(
              margin: EdgeInsets.only(top: 12),
              onPressed: () {
                NeumorphicTheme.of(context).themeMode =
                    NeumorphicTheme.isUsingDark(context)
                        ? ThemeMode.light
                        : ThemeMode.dark;
              },
              style: NeumorphicStyle(
                shape: NeumorphicShape.flat,
                boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(8)),
              ),
              padding: const EdgeInsets.all(12.0),
              child: Text(
                "Toggle Theme",
                style: TextStyle(color: _textColor(context)),
              ),
            ),
            NeumorphicButton(
              margin: EdgeInsets.only(top: 12),
              onPressed: () {
                Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) {
                  return FullSampleHomePage();
                }));
              },
              style: NeumorphicStyle(
                shape: NeumorphicShape.flat,
                boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(8)),
              ),
              padding: const EdgeInsets.all(12.0),
              child: Text(
                "Go to full sample",
                style: TextStyle(color: _textColor(context)),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Color _iconsColor(BuildContext context) {
    final theme = NeumorphicTheme.of(context);
    if (theme.isUsingDark) {
      return theme.current.accentColor;
    } else {
      return null;
    }
  }

  Color _textColor(BuildContext context) {
    if (NeumorphicTheme.isUsingDark(context)) {
      return Colors.white;
    } else {
      return Colors.black;
    }
  }
}

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

1 回复

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


当然,以下是一个关于如何使用 flutter_neumorphic_null_safety 插件来创建拟物风格UI的示例代码。这个插件为Flutter应用提供了丰富的拟物风格组件。

首先,确保在你的 pubspec.yaml 文件中添加该依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_neumorphic_null_safety: ^x.y.z  # 请替换为最新版本号

然后,运行 flutter pub get 来获取依赖。

接下来,让我们编写一个简单的Flutter应用,展示如何使用 flutter_neumorphic_null_safety 插件。

import 'package:flutter/material.dart';
import 'package:flutter_neumorphic_null_safety/flutter_neumorphic.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Neumorphic UI Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: NeumorphicDemoScreen(),
    );
  }
}

class NeumorphicDemoScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Neumorphic UI Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            NeumorphicButton(
              onPressed: () {
                print('Button pressed!');
              },
              child: Text('Neumorphic Button'),
              style: NeumorphicStyle(
                depth: 10,
                shape: NeumorphicShape.convex,
                color: NeumorphicColor(base: Colors.white, shadow: Colors.grey[300]!),
              ),
            ),
            SizedBox(height: 20),
            NeumorphicTextField(
              decoration: InputDecoration(
                labelText: 'Neumorphic TextField',
              ),
              style: NeumorphicStyle(
                depth: 5,
                shape: NeumorphicShape.roundRect(borderRadius: BorderRadius.circular(16)),
                color: NeumorphicColor(base: Colors.white, shadow: Colors.grey[300]!),
              ),
            ),
            SizedBox(height: 20),
            NeumorphicCard(
              margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      'Neumorphic Card',
                      style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                    ),
                    SizedBox(height: 8),
                    Text(
                      'This is a simple Neumorphic card example.',
                      style: TextStyle(fontSize: 16),
                    ),
                  ],
                ),
              ),
              style: NeumorphicStyle(
                depth: 20,
                shape: NeumorphicShape.roundRect(borderRadius: BorderRadius.circular(16)),
                color: NeumorphicColor(base: Colors.white, shadow: Colors.grey[400]!),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

代码解释:

  1. 依赖导入

    import 'package:flutter_neumorphic_null_safety/flutter_neumorphic.dart';
    
  2. NeumorphicButton

    • 创建一个拟物风格的按钮,点击时会在控制台打印信息。
    • NeumorphicStyle 用于定义按钮的深度、形状和颜色。
  3. NeumorphicTextField

    • 创建一个拟物风格的输入框。
    • 同样使用 NeumorphicStyle 来定义样式。
  4. NeumorphicCard

    • 创建一个拟物风格的卡片,内部包含一些文本信息。
    • 使用 NeumorphicStyle 来定义卡片的深度、形状和颜色。

这些组件共同展示了 flutter_neumorphic_null_safety 插件如何帮助开发者快速创建具有拟物风格的UI。你可以根据需要调整样式属性,以符合你的应用设计需求。

回到顶部