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

✅ 总结一句话

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

相关推荐
用户2018792831671 小时前
Binder 同应用内(本地)通信是否存在 1MB 大小限制?
android
一条上岸小咸鱼1 小时前
Kotlin 基本数据类型(四):String
android·前端·kotlin
魏嗣宗2 小时前
Node.js 网络编程全解析:从 Socket 到 HTTP,再到流式协议
前端·全栈
Onion_992 小时前
学习下Github上的Android CICD吧
android·github
三花AI2 小时前
xAI AI 伴侣 Ani 和 Valentine 支持电话实时通话
openai·全栈
来来走走3 小时前
Flutter Form组件的基本使用
android·flutter
顾林海3 小时前
Android MMKV 深度解析:原理、实践与源码剖析
android·面试·源码阅读
雨白4 小时前
TCP/IP 核心概念详解:从网络分层到连接管理
android
Wgllss5 小时前
雷电雨效果:Kotlin+Compose+协程+Flow 实现天气UI
android·架构·android jetpack
归辞...6 小时前
「iOS」————设计架构
ios·架构