如何使用GetX弹奏自定义按钮加载?

发布于 2025-02-14 01:18:55 字数 2371 浏览 0 评论 0原文

我想自定义一个按钮,该按钮在使用GETX从API获取数据时显示了加载动作,我可以通过任何控制器。

注意:每个控制器都有字段 isloading

示例:

  1. usercontroller
  2. dectionsController
  3. objecterController,

class BtnAction<T> extends GetView<ConnectivityController> {
  final String text;
  final GestureTapCallback press;
  final Color color;
  final Color textColor;
  final T isLoading;

  const BtnAction(
      {Key? key,
      required this.text,
      required this.press,
      required this.color,
      required this.isLoading,
      required this.textColor})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      if (controller.connectionStatus.value.name == 'none') {
        return TextButton.icon(
            onPressed: null,
            style: TextButton.styleFrom(backgroundColor: Colors.blueGrey),
            label: Text(text, style: TextStyle(color: textColor)),
            icon: Icon(Icons.wifi_off_sharp, color: textColor));
      } else {
        if (isLoading as (T).isLoading) {
          return TextButton(
            onPressed: press,
            style: TextButton.styleFrom(
              splashFactory: NoSplash.splashFactory,
              backgroundColor: color,
              primary: textColor,
              padding: EdgeInsets.zero,
            ),
            child: Text(text),
          );
        } else {
          return TextButton(
            onPressed: press,
            style: TextButton.styleFrom(
              splashFactory: NoSplash.splashFactory,
              backgroundColor: color,
              primary: textColor,
              padding: EdgeInsets.zero,
            ),
            child: Text(text),
          );
        }
      }
    });
  }
}

//on click
SizedBox(
            width: double.infinity,
            child: Padding(
              padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 15),
              child: BtnAction<UserController>(
                text: "Filter",
                textColor: Colors.white,
                color: Colors.black,
                isLoading: controller,
                press: () async {
                  controller.fetchUser(UserRequest());
             
                },
              ),
            ),
          ),

请给我一些建议,以了解我该如何实现这一目标。

谢谢你!!

I would like to customize a button that showed loading action while fetching data from API using GetX which I can pass any controllers.

Note: Each controller has field isLoading

Example:

  1. UserController
  2. DepartmentController
  3. SubjectController

class BtnAction<T> extends GetView<ConnectivityController> {
  final String text;
  final GestureTapCallback press;
  final Color color;
  final Color textColor;
  final T isLoading;

  const BtnAction(
      {Key? key,
      required this.text,
      required this.press,
      required this.color,
      required this.isLoading,
      required this.textColor})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      if (controller.connectionStatus.value.name == 'none') {
        return TextButton.icon(
            onPressed: null,
            style: TextButton.styleFrom(backgroundColor: Colors.blueGrey),
            label: Text(text, style: TextStyle(color: textColor)),
            icon: Icon(Icons.wifi_off_sharp, color: textColor));
      } else {
        if (isLoading as (T).isLoading) {
          return TextButton(
            onPressed: press,
            style: TextButton.styleFrom(
              splashFactory: NoSplash.splashFactory,
              backgroundColor: color,
              primary: textColor,
              padding: EdgeInsets.zero,
            ),
            child: Text(text),
          );
        } else {
          return TextButton(
            onPressed: press,
            style: TextButton.styleFrom(
              splashFactory: NoSplash.splashFactory,
              backgroundColor: color,
              primary: textColor,
              padding: EdgeInsets.zero,
            ),
            child: Text(text),
          );
        }
      }
    });
  }
}

//on click
SizedBox(
            width: double.infinity,
            child: Padding(
              padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 15),
              child: BtnAction<UserController>(
                text: "Filter",
                textColor: Colors.white,
                color: Colors.black,
                isLoading: controller,
                press: () async {
                  controller.fetchUser(UserRequest());
             
                },
              ),
            ),
          ),

Please kindly give me some advice on how can I achieve this.

Thank you!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文