Flutter加载状态按钮插件loading_elevated_button的使用

Flutter加载状态按钮插件loading_elevated_button的使用

Loading Elevated Button 是一个具有加载状态下禁用功能的按钮。该按钮可以根据加载状态启用或禁用,并且可以在加载时显示不同的子部件。

开始使用

简单版的 ElevatedButton 具有在加载状态下启用/禁用的功能,还可以根据其状态添加不同的子部件。

你可以查看下面的简单示例,或者查看代码中的示例项目,该示例项目包含其他基本行为(上图来自该示例)。

简单示例

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Loading Elevated Button',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Demo(),
    );
  }
}

class Demo extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Container(
            height: 200,
            width: 200,
            child: LoadingElevatedButton(
              isLoading: true,
              disabledWhileLoading: true,
              onPressed: () {
                print("Button clicked");
              },
              child: Text('Hello'),
            ),
          ),
        ),
      ),
    );
  }
}

按钮属性

LoadingElevatedButton({
    Key key,
    VoidCallback? onPressed,
    VoidCallback? onLongPress,
    ButtonStyle? style,
    FocusNode? focusNode,
    bool autoFocus = false,
    Clip clipBehavior = Clip.none,
    required Widget child,
    Widget? loadingChild,
    bool isLoading = false,
    bool disabledWhileLoading = true,
});

示例代码

以下是完整的示例代码,展示了如何使用 Loading Elevated Button 插件的不同状态:

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

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Loading Elevated Button',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const Demo(),
    );
  }
}

class Demo extends StatelessWidget {
  const Demo({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              buildNotLoadingButton(),
              buildLoadingButton(),
              buildLoadingWithCustomizedMessageButton(),
              buildLoadingButtonNotDisabled(),
            ],
          ),
        ),
      ),
    );
  }

  // 不加载状态的按钮
  Container buildNotLoadingButton() {
    return Container(
      padding: const EdgeInsets.all(8.0),
      height: 58,
      width: 200,
      child: LoadingElevatedButton(
        isLoading: false,
        disabledWhileLoading: true,
        onPressed: () {
          print("Button clicked");
        },
        child: const Text('Not Loading'),
      ),
    );
  }

  // 加载状态的按钮
  Container buildLoadingButton() {
    return Container(
      padding: const EdgeInsets.all(8.0),
      height: 58,
      width: 200,
      child: LoadingElevatedButton(
        isLoading: true,
        disabledWhileLoading: true,
        onPressed: () {
          print("Button clicked");
        },
        child: const Text('Default Loading'),
      ),
    );
  }

  // 加载状态并自定义消息的按钮
  Container buildLoadingWithCustomizedMessageButton() {
    return Container(
      padding: const EdgeInsets.all(8.0),
      height: 58,
      width: 200,
      child: LoadingElevatedButton(
        isLoading: true,
        disabledWhileLoading: true,
        onPressed: () {
          print("Button clicked");
        },
        child: const Text('Not Loading'),
        loadingChild: const Text(
          'Customized Loading',
          textAlign: TextAlign.center,
        ),
      ),
    );
  }

  // 加载状态但不被禁用的按钮
  Container buildLoadingButtonNotDisabled() {
    return Container(
      padding: const EdgeInsets.all(8.0),
      height: 58,
      width: 200,
      child: LoadingElevatedButton(
        style: ElevatedButton.styleFrom(
          primary: Colors.green,
        ),
        isLoading: true,
        disabledWhileLoading: false,
        onPressed: () {
          print("Button clicked");
        },
        child: const Text(
          'Loading but not Disabled',
          textAlign: TextAlign.center,
        ),
        loadingChild: const Text(
          'Customized and Enabled Loading',
          textAlign: TextAlign.center,
        ),
      ),
    );
  }
}

更多关于Flutter加载状态按钮插件loading_elevated_button的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


当然,下面是一个关于如何使用Flutter中的loading_elevated_button插件的示例代码。这个插件允许你创建一个在点击后显示加载状态的按钮。

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

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

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

接下来是一个完整的示例代码,展示如何使用LoadingElevatedButton

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

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

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool isLoading = false;

  void handleButtonPress() async {
    setState(() {
      isLoading = true;
    });

    // 模拟一个异步操作,比如网络请求
    await Future.delayed(Duration(seconds: 2));

    setState(() {
      isLoading = false;
    });

    // 这里可以添加你的操作完成后的逻辑,比如显示一个SnackBar
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      content: Text('操作完成!'),
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Loading Elevated Button Demo'),
      ),
      body: Center(
        child: LoadingElevatedButton(
          child: Text('点击我'),
          onPressed: isLoading ? null : handleButtonPress,
          loading: isLoading,
          loadingText: '加载中...',
        ),
      ),
    );
  }
}

在这个示例中:

  1. 我们创建了一个MyHomePage类,它是一个StatefulWidget,因为我们需要在按钮点击后更新按钮的加载状态。
  2. _MyHomePageState类中,我们定义了一个isLoading变量来跟踪按钮是否处于加载状态。
  3. handleButtonPress函数模拟了一个异步操作(比如网络请求),并在操作开始时将isLoading设置为true,操作完成后设置为false
  4. LoadingElevatedButtononPressed属性在isLoadingtrue时为null(禁用按钮点击),否则为handleButtonPress函数。
  5. loading属性表示按钮是否处于加载状态,loadingText属性定义了加载时显示的文本。

运行这个示例,你会看到一个按钮,在点击后会显示“加载中…”文本,并在2秒后恢复为可点击状态,同时显示一个SnackBar提示操作完成。

回到顶部