bilibili案例学习—框架搭建

fluro跳转路由库

引入:

dependencies:
  fluro: "^1.4.0"

初始化:在main.dart文件中main方法中初始化

void main() {
  //路由配置
  var router = new Router();
  Routers.configRouters(router);
  Application.router = router;
  runApp(MyApp());
}

1.编写Routers类:

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'rotuer_handler.dart';
import '../global/contants.dart';

class Routers{
  static String root='/';
  static void configRouters(Router router){
    //路由找寻不到时,作出提示
    router.notFoundHandler = new Handler(
      handlerFunc: (BuildContext context,Map> params){
        print('ERROR====>ROUTE WAS NOT FONUND!!!');
      }
    );
    //定义跳转到首页的路由
    router.define(Constant.homePage, handler: homeHandler);
  }
}

2.编写router_handler文件:
handler相当于一个路由的规则,比如我们要到详细页面,这时候就需要传递商品的ID,那就要写一个handler。

import 'package:flutter/material.dart';
import '../pages/HomePage.dart';
import 'package:fluro/fluro.dart';

Handler homeHandler = new Handler(
  handlerFunc: (BuildContext context,Map> map){
    return HomePage();
  }
);

3.路由_fluro的静态化

import 'package:fluro/fluro.dart';

class Application{
  static Router router;
}

4.使用过程

void goToHomePage(){
    ////如果页面还未跳转过则跳转页面
    if(!isUserLoginPage){
      //跳转主页 且销毁当前页面
      Application.router.navigateTo(context, Constant.homePage);
      isUserLoginPage=true;
    }
  }

Provide状态管理库

引入:

dependencies:
    provide: ^1.0.2

创建Provide:

import 'package:flutter/material.dart';

class CurrentIndexProvide extends ChangeNotifier{
  int currentIndex = 0;
  changeIndex(int newIndex){
    currentIndex = newIndex;
    notifyListeners();
  }
}

初始化:

void main(){
  var curProvide =CurrentIndexProvide();
  var providers  =Providers();
  providers
    ..provide(Provider.value(curProvide));
  runApp(ProviderNode(child:MyApp(),providers:providers));
}

获取状态:
使用Provide Widget的形式就可以获取状态,

Provide(builder: (context,child,val){
int currentIndex = Provide.value(context).currentIndex;
}

修改状态:

onTap: (index) {
            Provide.value(context).changeIndex(index);
          },

你可能感兴趣的:(flutter)