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、模块跳转等能力

✅ 总结一句话

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

相关推荐
有味道的男人9 小时前
Open Claw对接1688平台
android·rxjava
_李小白10 小时前
【android opencv学习笔记】Day 17: 目标追踪(MeanShift)
android·opencv·学习
用户860225046747211 小时前
AI 分析头部APP系统优化框架
android
用户860225046747211 小时前
AI分析头部APP优化框架
android
我是谁的程序员11 小时前
Mac 上生成 AppStoreInfo.plist 文件,App Store 上架
后端·ios
sweet丶11 小时前
微信Matrix 卡顿监控原理梳理与图解
ios
2501_9160074714 小时前
iOS开发中抓取HTTPS请求的完整解决方法与步骤详解
android·网络协议·ios·小程序·https·uni-app·iphone
lvronglee16 小时前
【数字图传第四步】Android App查看图传视频
android·音视频
90后的晨仔16 小时前
Android 程序入口与核心组件详解
android
90后的晨仔16 小时前
Kotlin 简介与开发环境搭建
android