Flutter拟物化风格UI插件material_neumorphic的使用

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

Flutter拟物化风格UI插件material_neumorphic的使用

在本教程中,我们将介绍如何使用material_neumorphic插件来创建拟物化风格的用户界面。material_neumorphic插件包含了许多用于构建拟物化风格界面的组件。

包含的组件

  • material_neumorphic_app_bar
  • material_neumorphic_background
  • material_neumorphic_button
  • material_neumorphic_checkbox
  • material_neumorphic_dropdown
  • material_neumorphic_icon
  • material_neumorphic_indicator
  • material_neumorphic_progress
  • material_neumorphic_radio
  • material_neumorphic_slider
  • material_neumorphic_switch
  • material_neumorphic_text
  • material_neumorphic_textfield

示例

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

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: MaterialNeumorphicAppBar(
          title: Text("拟物化风格示例"),
        ),
        body: Center(
          child: Container(
            width: 300,
            height: 400,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10),
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.5),
                  spreadRadius: 5,
                  blurRadius: 7,
                  offset: Offset(0, 3), // changes position of shadow
                ),
              ],
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                MaterialNeumorphicText(
                  text: "Hello, Neumorphic!",
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
                SizedBox(height: 20),
                MaterialNeumorphicButton(
                  onPressed: () {},
                  child: Text("点击我"),
                ),
                SizedBox(height: 20),
                MaterialNeumorphicCheckbox(
                  value: false,
                  onChanged: (bool? newValue) {},
                ),
                SizedBox(height: 20),
                MaterialNeumorphicSlider(
                  value: 0.5,
                  min: 0.0,
                  max: 1.0,
                  divisions: 10,
                  label: Text("滑块"),
                  onChanged: (double newValue) {},
                ),
                SizedBox(height: 20),
                MaterialNeumorphicSwitch(
                  value: false,
                  onChanged: (bool newValue) {},
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

代码解释

  1. 导入包:

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

    导入Flutter框架的基本库和material_neumorphic插件。

  2. 创建主应用类:

    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: MaterialNeumorphicAppBar(
              title: Text("拟物化风格示例"),
            ),
            body: Center(
              child: Container(
                width: 300,
                height: 400,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(10),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.5),
                      spreadRadius: 5,
                      blurRadius: 7,
                      offset: Offset(0, 3), // 改变阴影的位置
                    ),
                  ],
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    MaterialNeumorphicText(
                      text: "Hello, Neumorphic!",
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                    SizedBox(height: 20),
                    MaterialNeumorphicButton(
                      onPressed: () {},
                      child: Text("点击我"),
                    ),
                    SizedBox(height: 20),
                    MaterialNeumorphicCheckbox(
                      value: false,
                      onChanged: (bool? newValue) {},
                    ),
                    SizedBox(height: 20),
                    MaterialNeumorphicSlider(
                      value: 0.5,
                      min: 0.0,
                      max: 1.0,
                      divisions: 10,
                      label: Text("滑块"),
                      onChanged: (double newValue) {},
                    ),
                    SizedBox(height: 20),
                    MaterialNeumorphicSwitch(
                      value: false,
                      onChanged: (bool newValue) {},
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      }
    }
    

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

1 回复

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


当然,下面是一个关于如何在Flutter项目中使用material_neumorphic插件来创建拟物化风格UI的示例代码。material_neumorphic插件允许你创建具有深度和质感的拟物化界面元素。

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

dependencies:
  flutter:
    sdk: flutter
  material_neumorphic: ^3.0.3  # 请检查最新版本号

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

接下来,这里有一个简单的示例代码,展示如何使用material_neumorphic来创建一个具有拟物化风格的按钮和卡片:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '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>[
            SizedBox(
              height: 100,
              child: NeumorphicCard(
                margin: EdgeInsets.symmetric(vertical: 16, horizontal: 24),
                padding: EdgeInsets.all(20),
                curveType: CurveType.flat,
                depth: 8,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      'Neumorphic Card',
                      style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
                    ),
                    SizedBox(height: 16),
                    NeumorphicButton(
                      onPressed: () {},
                      child: Text('Click Me'),
                      style: NeumorphicStyle(
                        depth: 6,
                        intensity: 0.8,
                        color: Colors.blue,
                        shape: NeumorphicShape.convex,
                      ),
                    ),
                  ],
                ),
              ),
            ),
            SizedBox(
              height: 50,
            ),
            NeumorphicToggleSwitch(
              value: false,
              onChanged: (bool value) {},
              style: NeumorphicToggleSwitchStyle(
                thumbColor: Colors.blue,
                thumbShape: NeumorphicShape.roundRect,
                activeTrackColor: Colors.blue.withOpacity(0.3),
                inactiveTrackColor: Colors.grey.withOpacity(0.3),
                activeThumbColor: Colors.white,
                inactiveThumbColor: Colors.white,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

代码解释

  1. 依赖引入

    • pubspec.yaml中添加material_neumorphic依赖。
  2. 主应用入口

    • MyApp类定义了一个MaterialApp,其中包含了应用的主题和主页NeumorphicDemoScreen
  3. NeumorphicDemoScreen

    • Scaffold组件包含了应用的主体内容。
    • 使用NeumorphicCard来创建一个具有拟物化风格的卡片,包含文本和一个按钮。
    • NeumorphicButton创建了一个拟物化风格的按钮。
    • NeumorphicToggleSwitch创建了一个拟物化风格的开关。

这个示例展示了如何使用material_neumorphic插件来创建具有深度和质感的UI元素。你可以根据需要调整样式和布局,以创建更复杂和美观的界面。

回到顶部