美文网首页
Flutter 路由与导航

Flutter 路由与导航

作者: Slaser | 来源:发表于2020-03-03 14:37 被阅读0次

在移动端的开发中经常会有页面间的跳转flutter中可以通过路由来达到相同的效果

const MaterialApp({
    Key key,
    this.navigatorKey,
    this.home,
//这里
    this.routes = const <String, WidgetBuilder>{},
    this.initialRoute,
    this.onGenerateRoute,
    this.onUnknownRoute,
    this.navigatorObservers = const <NavigatorObserver>[],
    this.builder,
    this.title = '',
    this.onGenerateTitle,
    this.color,
    this.theme,
    this.darkTheme,
    this.themeMode = ThemeMode.system,
    this.locale,
    this.localizationsDelegates,
    this.localeListResolutionCallback,
    this.localeResolutionCallback,
    this.supportedLocales = const <Locale>[Locale('en', 'US')],
    this.debugShowMaterialGrid = false,
    this.showPerformanceOverlay = false,
    this.checkerboardRasterCacheImages = false,
    this.checkerboardOffscreenLayers = false,
    this.showSemanticsDebugger = false,
    this.debugShowCheckedModeBanner = true,
  })

我们可以看到在MaterialApp中有一个路由属性我们将创建好的界面写在路由里

class MyApp extends StatelessWidget {
  final CounterModel model;
  const MyApp({Key key, @required this.model}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    // At the top level of our app, we'll, create a ScopedModel Widget. This
    // will provide the CounterModel to all children in the app that request it
    // using a ScopedModelDescendant.
    return ScopedModel<CounterModel>(
      model: model,
      child: MaterialApp(
        title: 'Scoped Model Demo',
        home: CounterHome('Scoped Model Demo'),
        routes: <String, WidgetBuilder>
        {
          'Scoped_model':(BuildContext context)=>ScopedTestWidget(),
        },
      ),
    );
  }
}

然后我们写一个按钮

class CounterHome extends StatelessWidget {
  bool byName = false;
  final String title;
  CounterHome(this.title);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('You have pushed the button this many times:'),
            // Create a ScopedModelDescendant. This widget will get the
            // CounterModel from the nearest parent ScopedModel<CounterModel>.
            // It will hand that CounterModel to our builder method, and
            // rebuild any time the CounterModel changes (i.e. after we
            // `notifyListeners` in the Model).
            ScopedModelDescendant<CounterModel>(
              builder: (context, child, model) {
                return Text(
                  model.counter.toString(),
                  style: Theme.of(context).textTheme.display1,
                );
              },
            ),
        Container(
          child: RaisedButton(
            onPressed: () {
              //直接跳转
              //Navigator.push(context,MaterialPageRoute(builder:(context) => ScopedTestWidget()));
              //通过路由名字跳转
              Navigator.pushNamed(context, 'Scoped_model');
            },
            child: Text(title),
          ),
        ),

          ],
        ),

      ),
      // Use the ScopedModelDescendant again in order to use the increment
      // method from the CounterModel
      floatingActionButton: ScopedModelDescendant<CounterModel>(
        builder: (context, child, model) {
          return FloatingActionButton(
            onPressed: model.increment,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          );
        },
      ),
    );
  }
}

此时我们可以直接跳转也可以通过我们注册的路由进行跳转

相关文章

  • 8. 【文档讲解】路由与导航

    3-14 【文档讲解】路由与导航 路由与导航 Flutter中Intent等价于什么?(Android) 在Flu...

  • flutter 导航以及传参方式

    flutter 导航方式有基本路由和命名路由1、基本路由 ============================...

  • Flutter>导航(一)>导航到一个新页面和返回

    关于路由与导航,看了Flutter中文网: https://flutterchina.club/routing-a...

  • Flutter—— 路由(Route)和导航(Navigator

    Flutter的页面,怎么进行跳转的呢?通过路由和导航呢。 一、路由和导航,初认识 言简意赅! 路由(Route)...

  • flutter导航与路由

    导航 导航用flutter的自带组件 思路和vue导航是一样的1.声明一个数组,放入几个导航页面2.声明一个ind...

  • Flutter 路由与导航

    在移动端的开发中经常会有页面间的跳转flutter中可以通过路由来达到相同的效果 我们可以看到在MaterialA...

  • flutter 路由与导航

    参考链接 1. 简单的导航跳转 第一个页面 第二个页面 main 列表跳转的小例子 文章的模型 文章列表 内容界面 实现

  • flutter_boost

    混合开发要点 flutter engine复用 flutter路由和原生导航同步 flutter和原生数据传输->...

  • flutter路由

    在Flutter中,导航器管理应用程序的路由栈。将路由推入(push)到导航器的栈中,将会显示更新为该路由页面。 ...

  • Flutter路由,跳转传值

    路由 Flutter 中的路由就是页面跳转。通过 Navigator 组件管理路由导航。并提供了管理堆栈的方法。 ...

网友评论

      本文标题:Flutter 路由与导航

      本文链接:https://www.haomeiwen.com/subject/mraukhtx.html