Code Monkey home page Code Monkey logo

nop_flutter's Introduction

状态管理,路由生成

demo: shudu

状态管理

    Class CounterState {

    }
    //...
    Nav.put(() => CounterState());
    Nav.put((context) => CounterState());
    //...
    runApp(MyApp());

获取

    //...
    Widget build(BuildContext context) {
        // 需要使用 Nop
        final counter = context.getType<CounterState>();
        //...
    }

路由生成

需要添加依赖:

  dependencies:
    nop_annotations:

  dev_dependencies:
    nop_gen:
    build_runner:

示例

file: routes.dart;

    import 'package:nop_annotations/nop_annotations.dart';

    part 'routes.g.dart';

    @NopRouteMain(
      main: MyHomePage,
      pages: [
        RouteItem(page: SecondPage),
        RouteItem(page: ThirdPage),
      ],
    )
    class AppRoutes {}

    class SecondPage extends StatelessWidget {
        const SecondPage({Key? key, String? title}): super(key: key);
        //...
    }

run: dart run build_runner build
将会生成 routes.g.dart 文件

    class Routes {
        //...
      static final _secondPage =  NopRoute(
        name: '/secondPage',
        fullName: '/secondPage',
        builder: (context, arguments) => const Nop.page(
        child: SecondPage(title: arguments['title']),
        ),
      );
    }
    /// 将默认构造器转换成普通函数
    class NavRoutes {

      // Navigator.pushNamed(context, Routes._secondPage.fullName, arguments: {'title': 'secondTitle'});
      // secondPage(title: 'secondTitle').go;
      static NopRouteAction<T> secondPage<T>({
        BuildContext? context, String? title,
      }) {
        return NopRouteAction(
            context: context, route: Routes._secondPage, arguments:  {'title': title});
      }
    }

demo shudu

路由生成注解为 NavNop 提供了支持;路由跳转提供明确的参数(以函数调用提供)

说明

Nop 是一个标准的 StatefullWidget

状态管理依赖于 Nop,而路由生成注解会在每一个页面使用 Nop.page,page 是一个节点,存储可共享对象,生命周期由State管理

    void main() {
        runApp(Nop(child: MyApp()));
    }

在开始之前使用Nop,即可任意使用context.getType
创建的对象会自动判断是否是全局或页面,局部共享;
当共享的对象没有监听者时会被释放

nop_flutter's People

Contributors

sersr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.