Flutter圆形加载按钮插件circular_loading_button的使用
Flutter圆形加载按钮插件circular_loading_button的使用
插件介绍
loading_button
是一个用于Flutter的圆形加载按钮插件。它通过native的flutter按钮类型(ElevatedButton, FilledButton, OutlinedButton)实现,因此可以通过你的应用主题进行自定义。
该插件支持通过expandedSize
和loadingSize
参数来定义按钮在加载和闲置状态下的尺寸。
使用步骤
- 添加依赖项:
dependencies:
circular_loading_button: ^1ersion^
- 导入包:
import 'package:circular_loading_button/loading_button.dart';
- 简单示例:
// 设置初始按钮状态
LoadingButtonState _currentState = LoadingButtonState.idle;
LoadingButton(
type: LoadingButtonType.elevated,
state: _currentState,
onPressed: () {
// 改变按钮状态为加载
setState(() => _currentState = LoadingButtonState.loading);
// 等待3秒后将按钮状态设置为闲置
Future.delayed(const Duration(seconds: 3), () => setState(() => _currentState = LoadingButtonState.idle));
},
child: const Text('Tap me!')
)
参数说明
你可以通过添加expandedSize
和loadingSize
参数来简单地定义按钮在加载和闲置状态下的大小。
LoadingButton(
type: LoadingButtonType.elevated,
state: _currentState,
expandedSize: const Size(250.0, 80.0),
loadingSize: const Size(30.0, 30.0),
onPressed: () {
// TODO your actions
},
child: const Text('Tap me!')
)
贡献指南
所有贡献都是受欢迎的!任何贡献都大大感谢!
示例代码
import 'package:example/provider_theme.dart';
import 'package:example/themes/custom_theme.dart';
import 'package:flutter/material.dart';
import 'package:circular_loading_button/loading_button.dart';
import 'package:circular_loading_button/loading_button_state.dart';
import 'package:provider/provider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => ThemeProvider(),
child: Consumer<ThemeProvider>(
builder: (context, ThemeProvider provider, child) {
return MaterialApp(
title: 'Flutter Demo',
theme: provider.currentTheme,
home: const MyHomePage(title: 'LoadingButton Demo'),
);
}),
);
}
}
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> {
List<LoadingButtonType> types = [
LoadingButtonType.elevated,
LoadingButtonType.filled,
LoadingButtonType.filledTonal,
LoadingButtonType.outlined
];
List<LoadingButtonState> currentStates = [
LoadingButtonState.idle,
LoadingButtonState.idle,
LoadingButtonState.idle,
LoadingButtonState.idle
];
int _currentIndex = 0;
List<ThemeData> themes = [
CustomTheme.purple,
CustomTheme.purpleSquared,
CustomTheme.orange,
CustomTheme.orangeSquared
];
void _changeState(int index) {
setState(() {
currentStates[index] = (currentStates[index] == LoadingButtonState.idle)
? LoadingButtonState.loading
: LoadingButtonState.idle;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
ThemeProvider provider = Provider.of<ThemeProvider>(context, listen: false);
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 40.0),
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
SegmentedButton<int>(
showSelectedIcon: false,
segments: const <ButtonSegment<int>>[
ButtonSegment<int>(value: 0, label: Text('Purple')),
ButtonSegment<int>(value: 1, label: Text('Purple SQ')),
ButtonSegment<int>(value: 2, label: Text('Orange')),
ButtonSegment<int>(value: 4, label: Text('Orange SQ')),
],
selected: <int>{_currentIndex},
onSelectionChanged: (Set<int> newSelection) {
setState(() {
_currentIndex = newSelection.first;
});
provider.currentTheme = themes[_currentIndex];
},
),
...List.generate(
types.length,
(index) => Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20.0, vertical: 10.0),
child: LoadingButton(
type: types[index],
state: currentStates[index],
expandedSize: const Size(250.0, 80.0),
loadingSize: const Size(30.0, 30.0),
onPressed: () {
_changeState(index);
Future.delayed(const Duration(seconds: 3),
() => _changeState(index));
},
child: const Text('Tap me!')),
),
),
]),
),
),
),
);
}
}
更多关于Flutter圆形加载按钮插件circular_loading_button的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter圆形加载按钮插件circular_loading_button的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用circular_loading_button
插件的示例代码。这个插件允许你创建一个具有加载状态的圆形按钮。
首先,确保在你的pubspec.yaml
文件中添加circular_loading_button
依赖:
dependencies:
flutter:
sdk: flutter
circular_loading_button: ^2.0.0 # 请确保版本号是最新的
然后,运行flutter pub get
来安装依赖。
接下来,你可以在你的Flutter项目中使用这个插件。以下是一个完整的示例,展示了如何使用CircularLoadingButton
:
import 'package:flutter/material.dart';
import 'package:circular_loading_button/circular_loading_button.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Circular Loading Button Example'),
),
body: Center(
child: CircularLoadingButtonExample(),
),
),
);
}
}
class CircularLoadingButtonExample extends StatefulWidget {
@override
_CircularLoadingButtonExampleState createState() => _CircularLoadingButtonExampleState();
}
class _CircularLoadingButtonExampleState extends State<CircularLoadingButtonExample> {
bool isLoading = false;
void onLoadingButtonClick() async {
setState(() {
isLoading = true;
});
// 模拟一个异步操作,例如网络请求
await Future.delayed(Duration(seconds: 2));
setState(() {
isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return CircularLoadingButton(
size: 50.0,
width: 50.0,
height: 50.0,
color: Colors.blue,
child: Icon(Icons.arrow_forward),
loadingColor: Colors.red,
loading: isLoading,
onCompleted: () => print("Loading completed!"),
onError: (error, stackTrace) => print("Error: $error"),
onPressed: onLoadingButtonClick,
);
}
}
代码解释:
-
依赖添加:在
pubspec.yaml
文件中添加circular_loading_button
依赖。 -
主应用结构:
MyApp
类定义了一个基本的Flutter应用,包含一个Scaffold
,AppBar
和一个居中的CircularLoadingButtonExample
组件。 -
状态管理:
CircularLoadingButtonExample
是一个有状态的Widget,用于管理加载状态(isLoading
)。 -
按钮配置:
size
、width
和height
用于设置按钮的大小。color
是按钮的默认颜色。child
是按钮上的图标或文字,当按钮处于非加载状态时显示。loadingColor
是按钮在加载状态时的颜色。loading
是一个布尔值,表示按钮是否处于加载状态。onCompleted
是加载完成时的回调。onError
是发生错误时的回调。onPressed
是按钮被点击时的回调,这里用于启动加载状态并模拟一个异步操作。
运行这个示例代码,你会看到一个蓝色的圆形按钮,点击它后按钮会变为红色并显示加载状态,两秒后加载完成并恢复为原始状态。
希望这个示例能帮助你理解如何在Flutter项目中使用circular_loading_button
插件。