Flutter图片按钮插件picture_button的使用

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

Flutter图片按钮插件picture_button的使用

picture_button 是一个用于在Flutter中创建带有图片的按钮的插件。它允许你轻松设置按钮的图片和点击事件,同时还提供了丰富的自定义选项。

特性 🍜

  • 支持多种图片类型:AssetImage、NetworkImage、FileImage、MemoryImage。
  • 提供振动效果(vibrate)。
  • 可选的气泡效果(Bubble Effect)。
  • 支持按下时显示不同的图片。
  • 支持切换按钮(Toggle Button)。

开始使用 🌱

添加依赖

首先,在你的 pubspec.yaml 文件中添加 picture_button 依赖:

dependencies:
  flutter:
    sdk: flutter
  picture_button: ^最新版本号

然后运行以下命令来安装依赖:

flutter pub add picture_button

导入包

在你的 Dart 文件中导入 picture_button 包:

import 'package:picture_button/picture_button.dart';

配置资源路径

确保你的图片资源路径已在 pubspec.yaml 中配置:

flutter:
  uses-material-design: true

  assets:
    - assets/

示例代码 🚀

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

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

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Demo PictureButton Widget'),
    );
  }
}

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

  final String title;

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

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

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

  Widget get gap => const SizedBox(height: 16);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  'count\n$_counter',
                  textAlign: TextAlign.center,
                  style: Theme.of(context).textTheme.headlineMedium,
                ),
                const SizedBox(height: 32.0),
                const Text("anything parameters setting"),
                PictureButton(
                  onPressed: counting,
                  image: Image.asset("assets/google_sign_image.png").image,
                ),
                gap,
                const Text(
                    "anything parameter setting\n(width:250 in Colum Defined Width parent Widget)"),
                Container(
                  width: 250,
                  color: Colors.black26.withOpacity(0.1),
                  padding: const EdgeInsets.all(6.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      PictureButton(
                        onPressed: () {
                          setState(() {
                            _counter++;
                          });
                        },
                        image: const AssetImage("assets/google_sign_image.png"),
                      ),
                      gap,
                      const Text("width:100, height:100, fit:BoxFit.fill"),
                      PictureButton(
                        onPressed: counting,
                        image: const AssetImage("assets/google_sign_image.png"),
                        width: 100,
                        height: 100,
                        fit: BoxFit.fill,
                      ),
                      gap,
                      const Text("useBubbleEffect: true"),
                      PictureButton(
                        onPressed: counting,
                        image: const AssetImage("assets/google_sign_image.png"),
                        useBubbleEffect: true,
                      ),
                    ],
                  ),
                ),
                gap,
                const Text("[only ripple effect]\n"
                    "highlightColor: Colors.transparent\n"
                    "splashColor: Colors.transparent\n"
                    "useBubbleEffect: true\n"
                    "bubbleEffect: PictureBubbleEffect.expand,"),
                PictureButton(
                  onPressed: counting,
                  image: Image.asset("assets/google_sign_image.png").image,
                  highlightColor: Colors.transparent,
                  splashColor: Colors.transparent,
                  useBubbleEffect: true,
                  bubbleEffect: PictureBubbleEffect.expand,
                ),
                PictureButton(
                  onPressed: () {},
                  image: Image.asset("assets/icon_flutter_default.png").image,
                  imagePressed: Image.asset("assets/icon_flutter_pressed.png").image,
                  useBubbleEffect: true,
                  width: 150,
                  margin: EdgeInsets.symmetric(vertical: 16.0),
                  highlightColor: Colors.transparent,
                  splashColor: Colors.transparent,
                ),
                Text("[isSelectedMode]\n"
                    "onSelectChanged: (isSelected) { \n"
                    "   isSelected:$isSelected\n"
                    "}"),
                PictureButton(
                  onPressed: () {
                    print("onPressed..");
                  },
                  onSelectChanged: (isSelected) {
                    print("onSelectChanged.. isSelected:$isSelected");
                    setState(() {
                      this.isSelected = isSelected;
                    });
                  },
                  image: Image.asset("assets/icon_flutter_default.png").image,
                  imagePressed: Image.asset("assets/icon_flutter_pressed.png").image,
                  imageSelected: Image.asset("assets/icon_flutter_other.png").image,
                  backgroundColor: Colors.grey,
                  useBubbleEffect: true,
                  splashColor: Colors.transparent,
                  highlightColor: Colors.transparent,
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


当然,以下是如何在Flutter项目中使用picture_button插件的一个示例。首先,你需要确保你的Flutter项目中已经添加了picture_button依赖。如果还没有添加,可以在pubspec.yaml文件中添加以下依赖:

dependencies:
  flutter:
    sdk: flutter
  picture_button: ^最新版本号  # 请替换为实际的最新版本号

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

接下来,你可以在Dart文件中使用PictureButton。以下是一个简单的示例,展示了如何使用PictureButton来创建一个带有图片的按钮:

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

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

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

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Picture Button Demo'),
      ),
      body: Center(
        child: PictureButton(
          image: AssetImage('assets/your_image.png'), // 替换为你的图片资源路径
          onPressed: () {
            // 按钮点击事件处理
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('Button clicked!')),
            );
          },
          width: 100,
          height: 100,
          // 可选参数,设置按钮的背景颜色、圆角等
          decoration: BoxDecoration(
            color: Colors.transparent,
            borderRadius: BorderRadius.circular(15),
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们做了以下几件事:

  1. 导入picture_button包。
  2. 创建一个Flutter应用,并在主页中使用PictureButton
  3. 设置PictureButton的图片资源(使用AssetImage,你需要确保图片资源已经放在assets文件夹中,并在pubspec.yaml中声明)。
  4. 设置按钮的点击事件处理函数onPressed
  5. 设置按钮的宽度和高度。
  6. 可选地,设置按钮的装饰,比如背景颜色和圆角。

请确保你的图片资源已经正确放置在assets文件夹中,并在pubspec.yaml文件中声明它们,例如:

flutter:
  assets:
    - assets/your_image.png

这样,你就可以在Flutter应用中使用picture_button插件来创建带有图片的按钮了。

回到顶部