Flutter新拟物化设计插件itwillrock_neumorphism的使用

Flutter新拟物化设计插件itwillrock_neumorphism的使用

特性

这个插件允许你轻松创建基于新拟物化设计语言的用户界面。目前支持以下控件:

  1. 通用容器
  2. 圆形按钮(带有或不带强调色)
  3. 强调色列表(类似列表小部件)
  4. 指示器按钮
  5. 复选框
  6. 文本输入框
  7. 计数器

更多功能即将推出。

使用方法

只需将它添加到你的项目依赖中。

dependencies:
  itwillrock_neumorphism:

示例

下面是使用该插件的一个完整示例代码。

示例代码

import 'package:flutter/material.dart';
import 'package:itwillrock_neumorphism/charts/label_model.dart';
import 'package:itwillrock_neumorphism/constants/colors.dart';
import 'package:itwillrock_neumorphism/neumorphism.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // 这个小部件是你的应用的根。
  [@override](/user/override)
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        // 设置背景颜色。
        backgroundColor: Colors.white,
        body: MyHomePage(title: 'Flutter Demo Home Page'),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  double intensity1 = 1;
  late AnimationController _animationController;
  late Animation<double> _heightAnimation;
  ValueNotifier<LabelSeriesModel> chartData = ValueNotifier<LabelSeriesModel>(LabelSeriesModel());

  void addDataToChart(double val, LabelSeriesModel model) {
    model.data.add(LabelModel(label: '$val', value: val));
  }

  [@override](/user/override)
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500),
    );
    _heightAnimation = Tween<double>(begin: 10, end: 260).animate(
      CurvedAnimation(parent: _animationController, curve: Curves.easeInOut),
    );
    addDataToChart(122, chartData.value);
    addDataToChart(2, chartData.value);
    addDataToChart(5, chartData.value);
    addDataToChart(23, chartData.value);
    chartData.value.referenceValue = -222;
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _heightAnimation,
      builder: (context, child) {
        return Stack(
          children: [
            child!,
            Positioned(
              left: 0,
              right: 0,
              bottom: 0,
              child: Neumorphism.frostedGlassContainer(
                  margin: const EdgeInsets.all(10),
                  padding: const EdgeInsets.all(10),
                  dropShadow: true,
                  dropInnerShadow: true,
                  child: SizedBox(
                    height: _heightAnimation.value,
                  )),
            ),
          ],
        );
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(
            child: Neumorphism.seriesChart(chartData),
          ),
          Neumorphism.emailFormField(
            hint: 'some hint2',
          ),
          Row(
            children: [
              Neumorphism.container(
                  dropInnerShadow: false,
                  margin: const EdgeInsets.all(10),
                  padding: const EdgeInsets.all(10),
                  accentAligment: const Alignment(1, 0),
                  renderAccent: true,
                  accentIntensity: intensity1,
                  child: Container(
                    child: Neumorphism.softRoundButton(
                        size: const Size(60, 60),
                        toggle: true,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(30)),
                        margin: const EdgeInsets.all(10),
                        icon: Icon(
                          Icons.home,
                          color: AppColors.accentColor,
                          size: 32,
                        )),
                  )),
              Neumorphism.container(
                  dropInnerShadow: false,
                  margin: const EdgeInsets.all(10),
                  padding: const EdgeInsets.all(10),
                  accentAligment: const Alignment(1, 0),
                  renderAccent: true,
                  accentIntensity: intensity1,
                  child: Container(
                    child: Neumorphism.softRoundButton(
                        size: const Size(60, 60),
                        toggle: true,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(30)),
                        margin: const EdgeInsets.all(10),
                        icon: Icon(
                          Icons.home,
                          color: AppColors.accentColor,
                          size: 32,
                        )),
                  )),
              Neumorphism.container(
                dropInnerShadow: false,
                margin: const EdgeInsets.all(10),
                padding: const EdgeInsets.all(10),
                child: Neumorphism.accentButton(
                    size: const Size(60, 60),
                    toggle: false,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(30)),
                    margin: const EdgeInsets.all(10),
                    accentChanged: (intensity) {
                      setState(() {
                        intensity1 = intensity;
                      });
                    },
                    child: Icon(
                      Icons.fingerprint,
                      color: AppColors.mainColor,
                      size: 32,
                    )),
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Neumorphism.softRoundButton(
                  accentAligment: const Alignment(1, -1),
                  renderAccent: true,
                  accentIntensity: intensity1,
                  size: const Size(74, 74),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(37)),
                  margin: const EdgeInsets.all(10),
                  icon: const Icon(
                    Icons.fingerprint,
                    color: Color(0xFFADBAC7),
                    size: 32,
                  )),
              Neumorphism.indicatorButton(
                  accentAligment: const Alignment(1, -1),
                  accentColor: AppColors.accentColor,
                  accentIntensity: intensity1 * 0.5,
                  size: 74,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(37)),
                  margin: const EdgeInsets.all(10),
                  icon: const Icon(
                    Icons.fingerprint,
                    color: Color(0xFFADBAC7),
                    size: 32,
                  )),
              Neumorphism.checkBox(
                margin: const EdgeInsets.all(10),
                padding: const EdgeInsets.all(0),
              ),
            ],
          ),
          Neumorphism.softRoundButton(
              margin: const EdgeInsets.all(10),
              padding: const EdgeInsets.all(0),
              size: const Size(96, 42),
              text: "Login"),
          Neumorphism.softRoundButton(
              main: true,
              margin: const EdgeInsets.all(10),
              padding: const EdgeInsets.all(0),
              size: const Size(96, 42),
              onTap: () {
                if (_animationController.status == AnimationStatus.completed) {
                  _animationController.reverse();
                } else {
                  _animationController.forward();
                }
              },
              text: "Login")
        ],
      ),
    );
  }
}

更多关于Flutter新拟物化设计插件itwillrock_neumorphism的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


当然,下面是一个关于如何使用 itwillrock_neumorphism 插件的 Flutter 代码示例。itwillrock_neumorphism 是一个用于实现新拟物化设计(Neumorphism)效果的 Flutter 插件。它允许你轻松地为你的应用添加内阴影和外阴影,以创建立体和深度感。

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

dependencies:
  flutter:
    sdk: flutter
  itwillrock_neumorphism: ^x.y.z  # 请使用最新版本号

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

以下是一个完整的 Flutter 应用示例,展示了如何使用 itwillrock_neumorphism 插件:

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

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

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

class NeumorphismDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Neumorphism 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(
                color: Colors.grey[300]!,
                shadowDarkColor: Colors.black.withOpacity(0.2),
                shadowLightColor: Colors.white.withOpacity(0.2),
                depth: 8,
                shape: NeumorphicShape.convex,
              ),
            ),
            SizedBox(height: 20),
            NeumorphicCard(
              child: Column(
                children: <Widget>[
                  Text('Neumorphic Card'),
                  SizedBox(height: 10),
                  NeumorphicButton(
                    onPressed: () {
                      print('Card Button pressed!');
                    },
                    child: Text('Inner Button'),
                    style: NeumorphicStyle(
                      color: Colors.grey[400]!,
                      shadowDarkColor: Colors.black.withOpacity(0.3),
                      shadowLightColor: Colors.white.withOpacity(0.3),
                      depth: 6,
                      shape: NeumorphicShape.convex,
                    ),
                  ),
                ],
              ),
              margin: EdgeInsets.all(16),
              style: NeumorphicStyle(
                color: Colors.grey[200]!,
                shadowDarkColor: Colors.black.withOpacity(0.1),
                shadowLightColor: Colors.white.withOpacity(0.1),
                depth: 4,
                shape: NeumorphicShape.roundRect(borderRadius: BorderRadius.circular(16)),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

解释

  1. 依赖项:确保在 pubspec.yaml 中添加 itwillrock_neumorphism 依赖项。

  2. NeumorphicButton:这是一个带有内阴影和外阴影的按钮,可以创建立体效果。通过 NeumorphicStyle,你可以自定义按钮的颜色、阴影颜色、深度和形状。

  3. NeumorphicCard:这是一个带有内阴影和外阴影的卡片,可以包含其他子组件。同样,通过 NeumorphicStyle,你可以自定义卡片的样式。

  4. NeumorphicStyle:这个类用于定义阴影颜色、深度、形状等参数,以创建不同的新拟物化效果。

运行上述代码,你将看到一个包含两个按钮的示例应用,一个单独的 NeumorphicButton 和一个包含 NeumorphicButtonNeumorphicCard。每个组件都应用了不同的新拟物化样式,以展示插件的灵活性。

回到顶部