日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Flutter?Navigator路由傳參的實(shí)現(xiàn)_Android

作者:WEB前端李志杰 ? 更新時(shí)間: 2022-06-20 編程語(yǔ)言

Flutter中的默認(rèn)導(dǎo)航分成兩種,一種是命名的路由,一種是構(gòu)建路由。

一、命名路由傳參

應(yīng)用入口處定義路由表

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false, // 隱藏預(yù)覽中的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 {
          // 實(shí)現(xiàn)路由跳轉(zhuǎn)
          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("返回"),
      ),
    );
  }
}

二、構(gòu)建路由傳參

從HomePage頁(yè)面跳轉(zhuǎn)MenuPage頁(yè)面時(shí),攜帶參數(shù)

第一種方式:

// 定義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: () {
          // 實(shí)現(xiàn)路由跳轉(zhuǎn)
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => const MenuPage(
                title: '菜單123',
              ), // 需要跳轉(zhuǎn)的頁(yè)面
            ), // 修改路由的名稱、信息等
          );
        },
        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: () {
          // 實(shí)現(xiàn)路由跳轉(zhuǎn)
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => const MenuPage(),
                // 修改路由的名稱、信息等
                settings: const RouteSettings(
                    name: '菜單', arguments: {"name": '123'}) // 需要跳轉(zhuǎn)的頁(yè)面
                ),
          );
        },
        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頁(yè)面返回HomePage頁(yè)面時(shí),攜帶參數(shù)

// 定義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 {
          // 實(shí)現(xiàn)路由跳轉(zhuǎn)
          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("返回"),
      ),
    );
  }
}

原文鏈接:https://blog.csdn.net/qq_16221009/article/details/123347768

欄目分類
最近更新