Flutter开发技巧集锦

flutter中单例的写法

class AccountManager {
 factory AccountManager() => _instance ??= AccountManager._();
 
   /// 销毁单例
  static void destroyInstance() => _instance = null;
}

flutter 页面路由

利用GetX框架来实现

get: ^4.6.5

CoinRankPage 为具体的页面实现

点击查看代码
abstract class Routes {
  Routes._();
static const main = "/main";
  ///页面合集
  static final routePage = [
      GetPage(
      name: coinRink,
      page: () => const CoinRankPage(),
      binding: CoinRankBinding(),
    ),
  ]
  }

刷新和加载更多的实现

依赖框架

pull_to_refresh: ^2.0.0

头部刷新和底部加载更多widget封装

点击查看代码
import 'package:flutter/cupertino.dart';

import 'package:pull_to_refresh/pull_to_refresh.dart';

/// 头部刷新控件
class RefreshHeader extends StatelessWidget {
  const RefreshHeader({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const WaterDropHeader(
      complete: Center(
        child: Text("下拉刷新完成"),
      ),
    );
  }
}

/// 底部加载更多控件
class RefreshFooter extends StatelessWidget {
  const RefreshFooter({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomFooter(
      builder: (context, mode) {
        Widget body;
        if (mode == LoadStatus.idle) {
          body = const Text("上拉加载");
        } else if (mode == LoadStatus.loading) {
          body = const CupertinoActivityIndicator();
        } else if (mode == LoadStatus.failed) {
          body = const Text("加载失败!点击重试!");
        } else if (mode == LoadStatus.canLoading) {
          body = const Text("松手,加载更多!");
        } else {
          body = const Text("没有更多数据了!");
        }
        return Center(child: body);
      },
    );
  }
}

posted @ 2023-03-05 22:52  西北野狼  阅读(57)  评论(0编辑  收藏  举报