首頁 > 軟體

Flutter Navigator路由傳參的實現

2022-04-22 10:00:04

Flutter中的預設導航分成兩種,一種是命名的路由,一種是構建路由。

一、命名路由傳參

應用入口處定義路由表

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false, // 隱藏預覽中的debug
      title: 'Flutter Demo',
      routes: {
        '/': (context) => const HomePage(),
        "menu": (context) => const MenuPage()
      },
    );
  }
}
// 定義HomePage
class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("登入"),
      ),
      body: ElevatedButton(
        onPressed: () async {
          // 實現路由跳轉
          var result = await Navigator.pushNamed(context, 'menu',
              arguments: {'name': 'title'});
          print(result);
        },
        child: const Text('登入'),
      ),
    );
  }
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
  const MenuPage({Key? key}) : super(key: key);
  @override
  // 接收傳參
  Widget build(BuildContext context) {
    dynamic argumentsData = ModalRoute.of(context)?.settings.arguments;
    return Scaffold(
      appBar: AppBar(
        title: Text('選單' + argumentsData.toString()),
      ),
      body: ElevatedButton(
        onPressed: () {
          Navigator.pop(context, {'name': "Navigator.pop傳參"});
        },
        child: const Text("返回"),
      ),
    );
  }
}

二、構建路由傳參

從HomePage頁面跳轉MenuPage頁面時,攜帶引數

第一種方式:

// 定義HomePage
class HomePage extends StatelessWidget {
  const HomePage ({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("登入"),
      ),
      body: ElevatedButton(
        onPressed: () {
          // 實現路由跳轉
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => const MenuPage(
                title: '選單123',
              ), // 需要跳轉的頁面
            ), // 修改路由的名稱、資訊等
          );
        },
        child: const Text('登入'),
      ),
    );
  }
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
  // 定義接收的欄位
  final String title;
  const MenuPage({Key? key, required this.title}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: ElevatedButton(
        onPressed: () {
          Navigator.pop(context);
        },
        child: const Text("返回"),
      ),
    );
  }
}

第二種方式:

// 定義HomePage
class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("登入"),
      ),
      body: ElevatedButton(
        onPressed: () {
          // 實現路由跳轉
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => const MenuPage(),
                // 修改路由的名稱、資訊等
                settings: const RouteSettings(
                    name: '選單', arguments: {"name": '123'}) // 需要跳轉的頁面
                ),
          );
        },
        child: const Text('登入'),
      ),
    );
  }
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
  const MenuPage({Key? key}) : super(key: key);
  @override
  // 接收傳參
  Widget build(BuildContext context) {
    dynamic argumentsData = ModalRoute.of(context)?.settings.arguments;
    return Scaffold(
      appBar: AppBar(
        title: Text('選單' + argumentsData.toString()),
      ),
      body: ElevatedButton(
        onPressed: () {
          Navigator.pop(context);
        },
        child: const Text("返回"),
      ),
    );
  }
}

從MenuPage頁面返回HomePage頁面時,攜帶引數

// 定義HomePage
class HomePage extends StatelessWidget {
  const HomePage ({Key? key}) : super(key: key);;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("登入"),
      ),
      body: ElevatedButton(
        onPressed: () async {
          // 實現路由跳轉
          var result = await Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => const MenuPage(),
            ),
          );
          print(result);
        },
        child: const Text('登入'),
      ),
    );
  }
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
  const MenuPage({Key? key}) : super(key: key);
  @override
  // 接收傳參
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('選單'),
      ),
      body: ElevatedButton(
        onPressed: () {
          Navigator.pop(context, {'name': "Navigator.pop傳參"});
        },
        child: const Text("返回"),
      ),
    );
  }
}

到此這篇關於Flutter Navigator路由傳參的實現的文章就介紹到這了,更多相關Flutter Navigator路由傳參內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com