Flutter仿拟物风格UI插件custom_neumorphic的使用

Flutter仿拟物风格UI插件custom_neumorphic的使用

Custom Neumorphic Flutter Package.

示例代码

以下是一个完整的示例,展示了如何在Flutter应用中使用custom_neumorphic插件。

import 'package:flutter/material.dart';
import 'package:custom_neumorphic/custom_neumorphic.dart'; // 导入custom_neumorphic包

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // 这个小部件是你的应用的根。
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              '你已经按下了按钮次数:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            CustomNeumorphic( // 使用CustomNeumorphic小部件
              padding: EdgeInsets.all(20),
              neumorphicShape: NeumorphicShape.concave,
              boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(10)),
              depth: 8,
              accentColor: Colors.grey,
              lightSource: LightSource.topLeft,
              child: Container(
                width: 200,
                height: 200,
                color: Colors.white,
                child: Center(
                  child: Text('Neumorphic Box'),
                ),
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: '增加',
        child: const Icon(Icons.add),
      ),
    );
  }
}

代码解释

  1. 导入包

    import 'package:custom_neumorphic/custom_neumorphic.dart';
    

    首先导入custom_neumorphic包。

  2. 主应用类

    class MyApp extends StatelessWidget {
      const MyApp({super.key});
      
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
            useMaterial3: true,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    

    定义了应用的基本结构和主题。

  3. 主页面类

    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key, required this.title});
      
      final String title;
      
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    

    主页面定义了一个状态管理类_MyHomePageState

  4. 状态管理类

    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
      
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Text(
                  '你已经按下了按钮次数:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headlineMedium,
                ),
                CustomNeumorphic(
                  padding: EdgeInsets.all(20),
                  neumorphicShape: NeumorphicShape.concave,
                  boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(10)),
                  depth: 8,
                  accentColor: Colors.grey,
                  lightSource: LightSource.topLeft,
                  child: Container(
                    width: 200,
                    height: 200,
                    color: Colors.white,
                    child: Center(
                      child: Text('Neumorphic Box'),
                    ),
                  ),
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: '增加',
            child: const Icon(Icons.add),
          ),
        );
      }
    }
    

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

1 回复

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


custom_neumorphic 是一个仿拟物风格(Neumorphic Design)的 Flutter 插件,它可以帮助开发者快速实现拟物风格的 UI 设计。拟物风格的特点是界面元素看起来像是从背景中凸出或凹陷进去的,具有柔和的阴影和高光效果。

安装 custom_neumorphic 插件

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

dependencies:
  flutter:
    sdk: flutter
  custom_neumorphic: ^0.1.0  # 请使用最新版本

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

使用 custom_neumorphic 插件

以下是一个简单的例子,展示如何使用 custom_neumorphic 插件来创建一个拟物风格的按钮:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Neumorphic Design Example'),
        ),
        body: Center(
          child: NeumorphicButton(
            onPressed: () {
              print('Button Pressed');
            },
            style: NeumorphicStyle(
              shape: NeumorphicShape.flat,
              boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(12)),
              depth: 8,
              intensity: 0.8,
              lightSource: LightSource.topLeft,
              color: Colors.grey[300],
            ),
            padding: EdgeInsets.all(20),
            child: Text(
              'Press Me',
              style: TextStyle(fontSize: 20),
            ),
          ),
        ),
      ),
    );
  }
}
回到顶部