MP - List (not just list)

灵魂直击...

上代码

less 复制代码
// 你还需要 -> 获取网络list数据
Widget fetchListPage(BuildContext context) {
  return Padding(
    padding: const EdgeInsets.symmetric(horizontal: 20.0),
    child: SingleChildScrollView(
      child: Column(
        children: [
          const Text('HeroPage'),// 你还需要 替换为实际的 "HeroPage"
          ...List.generate(10, (index) { // 你还需要 替换10为实际的length
            return GestureDetector(
              onTap: (){
                // 你还需要 替换为自定义的 "Detail Page"
                Knavigator(context).magicPush(Morepush.normal, () => Text('Detail Page $index'));
              },
              child: Text('List Element $index'),
            );
          }),
        ],
      ),
    ),
  );
}
  • Navigator
typescript 复制代码
enum Morepush {
  normal,
  replace,
  removeuntil,
}

sealed class CoreNavigator {
  // push magic
  final BuildContext context;

  CoreNavigator(this.context);

  void magicPush(Morepush morepush, Widget Function() block) {
     _navigator(morepush, block());
  }

  void magicPop() {
     Navigator.pop(context);
  }

  void _navigator(Morepush morepush,Widget what) {
    switch (morepush) {
      case Morepush.normal:
      Navigator.push(context,MaterialPageRoute(builder: (context){
          return what; // push
      }));
      break;
      case Morepush.replace:
      Navigator.pushReplacement(context, MaterialPageRoute(builder: (context){
        return what; // pushReplacement
      }));
      break;
      case Morepush.removeuntil:
      Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context){
        return what; // pushAndRemoveUntil
      }), (route) => false); // 这需要注意(靓仔们,自己去去拓展,哈哈哈。。。)
    }
  }
}

// 使用 CoreNavigator
class Knavigator extends CoreNavigator {
  Knavigator(super.context);
}

优点总结

优点 描述
✅ 解耦 页面跳转行为不再与 Widget 层直接耦合
✅ 统一风格 所有跳转统一走 magicPush()
✅ 易扩展 想添加带动画、带参数、路径记录等逻辑,非常方便
✅ 提高可读性 比起写一堆 Navigator.push...,现在一行可读性强
✅ 灵活传参 支持传匿名函数(block)延迟执行 Widget 构建

✅ 推荐用于以下 - 项目:

  • 中大型项目,页面多、路由方式不统一
  • 页面跳转带有业务逻辑(埋点、权限判断)
  • 需要统一风格 & 增强可维护性
  • 后续准备支持 Web、DeepLink、模块跳转等能力

✅ 总结一句话

这是一种"可插拔、低耦合、强可维护"的页面跳转架构,推荐中大型项目采用,轻松驾驭复杂导航逻辑

相关推荐
王正南2 分钟前
安卓逆向之LSposed开发(一)
android·xposed·lsposed
CocoaKier19 分钟前
1月12日最新用户隐私保护政策出炉,政策解读
ios
YIN_尹1 小时前
【MySQL】数据类型(上)
android·mysql·adb
robotx2 小时前
AOSP设备节点权限添加相关
android
顾林海2 小时前
Android文件系统安全与权限控制:给应用数据上把“安全锁”
android·面试·操作系统
青莲8433 小时前
Android 动画机制完整详解
android·前端·面试
城东米粉儿3 小时前
android 离屏预渲染 笔记
android
未知名Android用户3 小时前
Android自定义 View + Canvas—声纹小球动画
android
_李小白3 小时前
【Android FrameWork】延伸阅读:AMS 的 handleApplicationCrash
android·开发语言·python