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 @   西北野狼  阅读(60)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2019-03-05 java实现 链表反转
2014-03-05 Android 学习 之 无需类名启动其他程序
2014-03-05 android xml文件中出现如下提醒:This tag and its children can be replaced by one <TextView/> and a compound drawable
点击右上角即可分享
微信分享提示