Flutter 仿霓虹材质风格单选按钮插件 material_neumorphic_radio 的使用

Flutter 仿霓虹材质风格单选按钮插件 material_neumorphic_radio 的使用

material_neumorphic_radio 是 Material Neumorphic 系列组件的一部分,用于创建具有霓虹材质风格的单选按钮。这些按钮可以为您的 Flutter 应用程序添加现代且独特的视觉效果。

使用方法

要使用 NeumorphicRadio 组件,您需要首先安装 material_neumorphic 包。在 pubspec.yaml 文件中添加以下依赖:

dependencies:
  material_neumorphic: ^0.1.0

然后运行 flutter pub get 来获取该包。

接下来,我们来看一个简单的例子,如何在 Flutter 中使用 NeumorphicRadio 组件。

示例代码

import 'package:flutter/material.dart';
import 'package:material_neumorphic/material_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 Radio Example')),
        body: Center(child: MyRadioButtons()),
      ),
    );
  }
}

class MyRadioButtons extends StatefulWidget {
  [@override](/user/override)
  _MyRadioButtonsState createState() => _MyRadioButtonsState();
}

class _MyRadioButtonsState extends State<MyRadioButtons> {
  int _groupValue = 1;

  Widget _buildRadios() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        NeumorphicRadio(
          child: SizedBox(
            height: 50,
            width: 50,
            child: Center(
              child: Text("1"),
            ),
          ),
          value: 1,
          groupValue: _groupValue,
          onChanged: (value) {
            setState(() {
              _groupValue = value;
            });
          },
        ),
        NeumorphicRadio(
          child: SizedBox(
            height: 50,
            width: 50,
            child: Center(
              child: Text("2"),
            ),
          ),
          value: 2,
          groupValue: _groupValue,
          onChanged: (value) {
            setState(() {
              _groupValue = value;
            });
          },
        ),
        NeumorphicRadio(
          child: SizedBox(
            height: 50,
            width: 50,
            child: Center(
              child: Text("3"),
            ),
          ),
          value: 3,
          groupValue: _groupValue,
          onChanged: (value) {
            setState(() {
              _groupValue = value;
            });
          },
        ),
      ],
    );
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text('Selected Value: $_groupValue'),
        SizedBox(height: 20),
        _buildRadios(),
      ],
    );
  }
}

解释

  1. 导入必要的库

    import 'package:flutter/material.dart';
    import 'package:material_neumorphic/material_neumorphic.dart';
    
  2. 初始化状态

    int _groupValue = 1;
    
  3. 构建单选按钮组

    Widget _buildRadios() {
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          NeumorphicRadio(
            child: SizedBox(
              height: 50,
              width: 50,
              child: Center(
                child: Text("1"),
              ),
            ),
            value: 1,
            groupValue: _groupValue,
            onChanged: (value) {
              setState(() {
                _groupValue = value;
              });
            },
          ),
          NeumorphicRadio(
            child: SizedBox(
              height: 50,
              width: 50,
              child: Center(
                child: Text("2"),
              ),
            ),
            value: 2,
            groupValue: _groupValue,
            onChanged: (value) {
              setState(() {
                _groupValue = value;
              });
            },
          ),
          NeumorphicRadio(
            child: SizedBox(
              height: 50,
              width: 50,
              child: Center(
                child: Text("3"),
              ),
            ),
            value: 3,
            groupValue: _groupValue,
            onChanged: (value) {
              setState(() {
                _groupValue = value;
              });
            },
          ),
        ],
      );
    }
    
  4. 将单选按钮组添加到主界面

    [@override](/user/override)
    Widget build(BuildContext context) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text('Selected Value: $_groupValue'),
          SizedBox(height: 20),
          _buildRadios(),
        ],
      );
    }
    

更多关于Flutter 仿霓虹材质风格单选按钮插件 material_neumorphic_radio 的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter 仿霓虹材质风格单选按钮插件 material_neumorphic_radio 的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


material_neumorphic_radio 是一个 Flutter 插件,用于创建具有霓虹材质风格的单选按钮。这种风格的设计通常具有柔和的光影效果,使按钮看起来像浮在背景上,给用户带来一种现代感和科技感。

以下是如何在 Flutter 项目中使用 material_neumorphic_radio 插件的步骤:

1. 添加依赖

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

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

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

2. 导入插件

在你的 Dart 文件中导入 material_neumorphic_radio 插件:

import 'package:material_neumorphic_radio/material_neumorphic_radio.dart';

3. 使用 NeumorphicRadio 组件

NeumorphicRadiomaterial_neumorphic_radio 插件中的主要组件,用于创建霓虹风格的单选按钮。

以下是一个简单的示例,展示如何使用 NeumorphicRadio

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

class NeumorphicRadioExample extends StatefulWidget {
  @override
  _NeumorphicRadioExampleState createState() => _NeumorphicRadioExampleState();
}

class _NeumorphicRadioExampleState extends State<NeumorphicRadioExample> {
  String _selectedValue = 'Option 1';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Neumorphic Radio Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            NeumorphicRadio(
              value: 'Option 1',
              groupValue: _selectedValue,
              onChanged: (value) {
                setState(() {
                  _selectedValue = value;
                });
              },
              child: Text('Option 1'),
            ),
            SizedBox(height: 20),
            NeumorphicRadio(
              value: 'Option 2',
              groupValue: _selectedValue,
              onChanged: (value) {
                setState(() {
                  _selectedValue = value;
                });
              },
              child: Text('Option 2'),
            ),
            SizedBox(height: 20),
            NeumorphicRadio(
              value: 'Option 3',
              groupValue: _selectedValue,
              onChanged: (value) {
                setState(() {
                  _selectedValue = value;
                });
              },
              child: Text('Option 3'),
            ),
          ],
        ),
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: NeumorphicRadioExample(),
  ));
}

4. 自定义样式

NeumorphicRadio 组件允许你自定义按钮的外观。你可以通过 style 参数来调整按钮的颜色、阴影、边框等属性。

NeumorphicRadio(
  value: 'Option 1',
  groupValue: _selectedValue,
  onChanged: (value) {
    setState(() {
      _selectedValue = value;
    });
  },
  style: NeumorphicStyle(
    boxShape: NeumorphicBoxShape.circle(),
    color: Colors.blueGrey[200],
    intensity: 0.8,
    depth: 8,
    lightSource: LightSource.topLeft,
  ),
  child: Text('Option 1'),
),
回到顶部