Flutter图像按钮组件插件imagebutton的使用
Flutter图像按钮组件插件imagebutton
的使用
关于
ImageButton
是一个简单的插件,允许你创建带有两种状态(按下和未按下)的图像按钮。它适用于那些希望通过用户交互来改变视觉效果的场景。
如何使用ImageButton
基本示例
以下是如何在Flutter项目中使用ImageButton
的基本示例:
import 'package:flutter/material.dart';
import 'package:imagebutton/imagebutton.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("ImageButton Example")),
body: Center(
child: ImageButton(
child: Row(
children: [
Icon(Icons.thumb_up, color: Colors.white),
SizedBox(width: 5),
Text("Like", style: TextStyle(color: Colors.white))
],
),
width: 120,
height: 50,
pressedImage: Image.asset("assets/button_pressed.png"),
unpressedImage: Image.asset("assets/button_unpressed.png"),
isLoading: false,
onTap: () {
print("Button Pressed");
},
),
),
),
);
}
}
ImageButton
属性
以下是ImageButton
的主要属性及其描述:
属性 | 类型 | 描述 |
---|---|---|
child |
Widget? |
显示在按钮内部的自定义小部件。优先于children 。 |
unpressedImage |
Image |
按钮未被按下时显示的图像。 |
pressedImage |
Image |
按钮被按下时显示的图像。 |
label |
Widget? |
可选的小部件,显示在按钮下方。 |
onTap |
VoidCallback? |
按钮被点击时触发的回调函数。 |
width |
double? |
按钮的宽度。 |
height |
double? |
按钮的高度。 |
isEnabled |
bool |
决定按钮是否可交互,默认为true 。 |
isLoading |
bool |
如果设置为true ,显示加载指示器,默认为false 。 |
完整示例Demo
下面是一个完整的示例代码,展示了如何在一个实际的应用程序中使用ImageButton
:
import 'package:flutter/material.dart';
import 'package:imagebutton/imagebutton.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
[@override](/user/override)
void initState() {
super.initState();
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.blue),
home: Scaffold(
appBar: AppBar(
title: const Text('ImageButton Example'),
centerTitle: true,
),
body: Center(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Column(
children: [
const Text(
"Simple ImageButton",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
ImageButton(
pressOffset: 5,
width: 91,
height: 36,
pressedImage: Image.asset("assets/pressed.png"),
unpressedImage: Image.asset("assets/normal.png"),
onTap: () {
debugPrint('Simple button pressed');
},
),
],
),
const SizedBox(height: 24),
Column(
children: [
const Text(
"ImageButton with Icon and Text",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
ImageButton(
pressOffset: 6,
padding: const EdgeInsets.only(bottom: 6),
width: 120,
height: 50,
loadingColor: Colors.white,
isLoading: false,
pressedImage: Image.asset("assets/pressed_notext.png"),
unpressedImage: Image.asset("assets/normal_notext.png"),
onTap: () {
debugPrint('Button with icon and text pressed');
},
child: const Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
Icons.thumb_up,
color: Colors.white,
size: 16,
),
SizedBox(width: 5),
Text(
'Like',
style: TextStyle(color: Colors.white, fontSize: 16),
),
],
),
),
],
),
],
),
),
),
),
),
);
}
}
更多关于Flutter图像按钮组件插件imagebutton的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复