Flutter扩展图标按钮插件icon_button_extended的使用

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

Flutter扩展图标按钮插件icon_button_extended的使用

特性

[IconButton]上添加了onLongPressonLongHold功能。

使用方法

要使用icon_button_extended插件,首先需要将其添加到项目的pubspec.yaml文件中。然后,您可以检查示例应用以获取更多信息。

示例代码

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

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 ExamplePage(),
    );
  }
}

class ExamplePage extends StatefulWidget {
  const ExamplePage({super.key});

  @override
  State<ExamplePage> createState() => _ExamplePageState();
}

class _ExamplePageState extends State<ExamplePage> {
  int _counter = 0;
  final Duration _debounce = const Duration(milliseconds: 200);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Example Page'),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            /// 图标按钮
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                IconButtonExtended(
                  longHoldDebounce: _debounce,
                  color: Colors.red.shade300,
                  iconSize: 52,
                  icon: const Icon(Icons.remove_circle),
                  onPressed: _decrement,
                  onLongHold: _decrement,
                  onLongPress: _decrement,
                ),
                IconButtonExtended(
                  longHoldDebounce: _debounce,
                  color: Colors.green.shade300,
                  iconSize: 52,
                  icon: const Icon(Icons.add_circle),
                  onPressed: _increment,
                  onLongHold: _increment,
                  onLongPress: _increment,
                ),
              ],
            ),

            const SizedBox(height: 12),

            /// 计数器
            Text(
              'Counter: $_counter',
              style: TextStyle(
                fontSize: 24,
                color: Theme.of(context).colorScheme.primary,
              ),
            ),
          ],
        ),
      ),
    );
  }

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

  void _decrement() {
    setState(() {
      _counter--;
    });
  }
}

解释

  1. 导入包

    import 'package:flutter/material.dart';
    import 'package:icon_button_extended/icon_button_extended.dart';
    
  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 ExamplePage(),
        );
      }
    }
    
  3. 创建状态管理类

    class ExamplePage extends StatefulWidget {
      const ExamplePage({super.key});
    
      @override
      State<ExamplePage> createState() => _ExamplePageState();
    }
    
    class _ExamplePageState extends State<ExamplePage> {
      int _counter = 0;
      final Duration _debounce = const Duration(milliseconds: 200);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Example Page'),
          ),
          body: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                /// 图标按钮
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    IconButtonExtended(
                      longHoldDebounce: _debounce,
                      color: Colors.red.shade300,
                      iconSize: 52,
                      icon: const Icon(Icons.remove_circle),
                      onPressed: _decrement,
                      onLongHold: _decrement,
                      onLongPress: _decrement,
                    ),
                    IconButtonExtended(
                      longHoldDebounce: _debounce,
                      color: Colors.green.shade300,
                      iconSize: 52,
                      icon: const Icon(Icons.add_circle),
                      onPressed: _increment,
                      onLongHold: _increment,
                      onLongPress: _increment,
                    ),
                  ],
                ),
                const SizedBox(height: 12),
                /// 计数器
                Text(
                  'Counter: $_counter',
                  style: TextStyle(
                    fontSize: 24,
                    color: Theme.of(context).colorScheme.primary,
                  ),
                ),
              ],
            ),
          ),
        );
      }
    
      void _increment() {
        setState(() {
          _counter++;
        });
      }
    
      void _decrement() {
        setState(() {
          _counter--;
        });
      }
    }
    

更多关于Flutter扩展图标按钮插件icon_button_extended的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter扩展图标按钮插件icon_button_extended的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何使用 icon_button_extended 插件在 Flutter 中创建扩展图标按钮的示例代码。icon_button_extended 是一个流行的 Flutter 插件,它提供了更多自定义选项的图标按钮。

首先,确保你的 pubspec.yaml 文件中已经包含了 icon_button_extended 依赖项:

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

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

以下是一个完整的 Flutter 应用示例,展示了如何使用 IconButtonExtended

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

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

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

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('IconButtonExtended Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            IconButtonExtended(
              icon: Icon(Icons.add),
              onPressed: () {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Add button pressed')),
                );
              },
              iconSize: 30,
              padding: EdgeInsets.all(15),
              color: Colors.blue,
              iconColor: Colors.white,
              borderRadius: BorderRadius.circular(20),
            ),
            SizedBox(height: 20),
            IconButtonExtended(
              icon: Icon(Icons.delete),
              onPressed: () {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Delete button pressed')),
                );
              },
              iconSize: 25,
              padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
              backgroundColor: Colors.red,
              iconColor: Colors.white,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(15),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中,我们创建了两个 IconButtonExtended 按钮,分别用于添加和删除操作。每个按钮都有不同的图标大小、内边距、颜色和形状。

  • icon 属性用于设置按钮的图标。
  • onPressed 属性用于设置按钮点击时的回调函数。
  • iconSize 属性用于设置图标的大小。
  • padding 属性用于设置按钮的内边距。
  • colorbackgroundColor 属性用于设置按钮的背景颜色(注意:color 属性在较新版本中可能已经被弃用,建议使用 backgroundColor)。
  • iconColor 属性用于设置图标的颜色。
  • borderRadiusshape 属性用于设置按钮的形状和圆角。

这个示例展示了如何使用 icon_button_extended 插件来创建高度自定义的图标按钮。你可以根据需要进一步调整这些属性来满足你的应用需求。

回到顶部