Flutter 路由 Navigator基本使用

返回到上一个界面

Navigator.pop(context);

返回到上一个界面 并且传参

Navigator.pop(context, "TestRooterPage 返回并传值");

push 到界面TestRooterPage并且传参TestRooter

import 'package:flutter/services.dart';

   void _pushFlutterPage() async {
    String result = await Navigator.push(context,
        new MaterialPageRoute(
            builder: (context) => new TestRooterPage("TestRooter")
        )
    );
// result是pop回来的结果
    if (result != null) {
// import 'package:bot_toast/bot_toast.dart';
      BotToast.showText(text: result);
    }
  }

路由 push到界面TestRooterPage并传参

    Navigator.of(context).pushNamed('/TestRooterPage', arguments: {
      "title": "23",
    });

路由传参到界面TestRooterPage 取参数方法

  @override
  Widget build(BuildContext context) {
    dynamic obj = ModalRoute.of(context).settings.arguments;
    if (obj != null) {
      this.title = obj["title"];
    }
    return Scaffold(
    );
  }
}

路由push到界面TestRooterPage之后 返回传传参使用then

    Navigator.of(context).pushNamed('/TestRooterPage').then((value) {
      if (value != null) {
        BotToast.showText(text: value);
      }
    });

跳转之后不能返回

    Navigator.of(context).pushNamedAndRemoveUntil('/TestRooterPage', (Route route) => false);

TestRooterPage代码

import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:bot_toast/bot_toast.dart';

class TestRooterPage extends StatelessWidget {

  String title;
  TestRooterPage(this.title);

  @override
  Widget build(BuildContext context) {
    dynamic obj = ModalRoute.of(context).settings.arguments;
    if (obj != null) {
      this.title = obj["title"];
    }
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
      ),
      body: Center(
        child: Column(
          children: [
            Text(this.title),
            MaterialButton(
              color: Colors.blue,
              textColor: Colors.white,
              child: new Text('跳转Flutter界面'),
              onPressed: () {
                Navigator.pop(context, "TestRooterPage 返回并传值");
              },
            )
          ],
        ),
      ),
    );
  }
}
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:bot_toast/bot_toast.dart';
import 'Test/TestRooterPage.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BotToastInit(
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        navigatorObservers: [BotToastNavigatorObserver()],
        home: MyHomePage(title: '12'),
        routes:  {
          '/HomePage': (BuildContext context) => new MyApp(),
          '/TestRooterPage' : (BuildContext context) => new TestRooterPage("TestRooter"),
        },
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
  int _counter = 0;
  String _textString = "00";

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  // 创建一个给native的channel (类似iOS的通知)
  static const MethodChannel methodChannel =
      MethodChannel('jianan.YYFramework/test');

  _iOSPushToVC() async {
    await methodChannel.invokeMethod('FlutterPopIOS', '参数');
  }

  void _backAction() {
    _iOSPushToVC();
  }

  void _pushIOSNewVC() async {
    Map map = {
      "code": "200",
      "data": [1, 2, 3]
    };

    await methodChannel.invokeMethod('FlutterCickedActionPushIOSNewVC', map);
  }

  Future _FlutterGetIOSArguments(para) async {
    BotToast.showText(text: "_FlutterGetIOSArguments");
    try {
      final result =
          await methodChannel.invokeMethod('FlutterGetIOSArguments', para);

      BotToast.showText(text: result["a"]);
      _textString = result["a"];
    } on PlatformException catch (error) {
      print(error);
    }
  }

  void _pushFlutterPage() async {
    // Navigator
//    String result = await Navigator.push(context,
//        new MaterialPageRoute(
//            builder: (context) => new TestRooterPage("TestRooter")
//        )
//    );
//    
//    if (result != null) {
//      BotToast.showText(text: result);
//    }

    // 路由
    Navigator.of(context).pushNamed('/TestRooterPage', arguments: {
      "title": "23",
    });
//    Navigator.of(context).pushNamed('/TestRooterPage').then((value) {
//      if (value != null) {
//        BotToast.showText(text: value);
//      }
//    });
    // 跳转之后不能返回
//    Navigator.of(context).pushNamedAndRemoveUntil('/TestRooterPage', (Route route) => false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'You have pushed the button this many times1:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
            Text("iOS传值给Flutter:"),
            Text(_textString),
            MaterialButton(
              color: Colors.blue,
              textColor: Colors.white,
              child: new Text('返回到原生界面-有传值'),
              onPressed: () {
                _backAction();
              },
            ),
            MaterialButton(
              color: Colors.blue,
              textColor: Colors.white,
              child: new Text('跳转到一个新的原生界面'),
              onPressed: () {
                _pushIOSNewVC();
              },
            ),
            MaterialButton(
              color: Colors.blue,
              textColor: Colors.white,
              child: new Text('iOS传值给Flutter'),
              onPressed: () {
                _FlutterGetIOSArguments("iOS传值给Flutter");
              },
            ),
            MaterialButton(
              color: Colors.blue,
              textColor: Colors.white,
              child: new Text('跳转Flutter界面'),
              onPressed: () {
                _pushFlutterPage();
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}


你可能感兴趣的:(Flutter 路由 Navigator基本使用)