16Flutter中的路由 基本路由 基本路由跳转传值(上)

た 入场券 2023-08-17 15:58 210阅读 0赞

/*

Flutter中的普通路由、普通路由传值、命名路由、命名路由传值

Flutter中的路由通俗的讲就是页面跳转。在Flutter中通过Navigator组件管理路由导航。

并提供了管理堆栈的方法。如:Navigator.push和Navigator.pop

Flutter 中给我们提供了两种配置路由跳转的方式;1.基本路由。2.命名路由

*/

main.dart

  1. import 'package:flutter/material.dart';
  2. import 'pages/Tabs.dart';
  3. void main() {
  4. runApp(MyApp());
  5. }
  6. class MyApp extends StatelessWidget {
  7. @override
  8. Widget build(BuildContext context) {
  9. // TODO: implement build
  10. return MaterialApp(
  11. home: Tabs(),
  12. );
  13. }
  14. }

pages/Form.dart

  1. import 'package:flutter/material.dart';
  2. class FormPage extends StatelessWidget {
  3. String title;
  4. FormPage({
  5. this.title="表单"});
  6. @override
  7. Widget build(BuildContext context) {
  8. return Scaffold(
  9. floatingActionButton: FloatingActionButton(
  10. child: Text('返回'),
  11. onPressed: (){
  12. Navigator.of(context).pop();
  13. },
  14. ),
  15. appBar: AppBar(
  16. title: Text(this.title),
  17. ),
  18. body: ListView(
  19. children: <Widget>[
  20. ListTile(
  21. title: Text('我是表单页面'),
  22. ),
  23. ListTile(
  24. title: Text('我是表单页面'),
  25. )
  26. ],
  27. ),
  28. );
  29. }
  30. }

pages/Search.dart

  1. import 'package:flutter/material.dart';
  2. class SearchPage extends StatelessWidget {
  3. const SearchPage({Key key}) : super(key: key);
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: Text('我是搜索页面'),
  8. ),
  9. body: Text('搜索页面内容区域'),
  10. );
  11. }
  12. }

pages/Tabs.dart

  1. import 'package:flutter/material.dart';
  2. import 'tabs/Home.dart';
  3. import 'tabs/Category.dart';
  4. import 'tabs/Setting.dart';
  5. class Tabs extends StatefulWidget {
  6. Tabs({Key key}) : super(key: key);
  7. _TabsState createState() => _TabsState();
  8. }
  9. class _TabsState extends State<Tabs> {
  10. int _currentIndex = 0;
  11. List _pageList=[
  12. HomePage(),
  13. CategoryPage(),
  14. SettingPage()
  15. ];
  16. @override
  17. Widget build(BuildContext context) {
  18. return Scaffold(
  19. appBar: AppBar(
  20. title: Text('Flutter Demo'),
  21. ),
  22. bottomNavigationBar: BottomNavigationBar(
  23. currentIndex: this._currentIndex,
  24. onTap: (int index) {
  25. // print(index);
  26. setState(() {
  27. this._currentIndex = index;
  28. });
  29. },
  30. iconSize: 36.0,
  31. type: BottomNavigationBarType.fixed,
  32. fixedColor: Colors.red,
  33. items: [
  34. BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
  35. BottomNavigationBarItem(
  36. icon: Icon(Icons.category), title: Text('分类')),
  37. BottomNavigationBarItem(
  38. icon: Icon(Icons.settings), title: Text('设置')),
  39. ]),
  40. body: this._pageList[this._currentIndex],
  41. );
  42. }
  43. }

pages/tabs/Category.dart

  1. import 'package:flutter/material.dart';
  2. import '../Form.dart';
  3. class CategoryPage extends StatelessWidget {
  4. const CategoryPage({Key key}) : super(key: key);
  5. @override
  6. Widget build(BuildContext context) {
  7. return Column(
  8. crossAxisAlignment: CrossAxisAlignment.center,
  9. mainAxisAlignment: MainAxisAlignment.center,
  10. children: <Widget>[
  11. RaisedButton(
  12. child: Text('跳转到表单页面并传值'),
  13. onPressed: (){
  14. Navigator.of(context).push(
  15. MaterialPageRoute(
  16. builder:(context)=>FormPage(title: '我是跳转传值')
  17. )
  18. );
  19. },
  20. color: Theme.of(context).accentColor,
  21. textTheme: ButtonTextTheme.primary
  22. )
  23. ],
  24. );
  25. }
  26. }

pages/tabs/Home.dart

  1. import 'package:flutter/material.dart';
  2. import '../Search.dart';
  3. class HomePage extends StatelessWidget {
  4. const HomePage({Key key}) : super(key: key);
  5. @override
  6. Widget build(BuildContext context) {
  7. return Column(
  8. mainAxisAlignment: MainAxisAlignment.center,
  9. crossAxisAlignment: CrossAxisAlignment.center,
  10. children: <Widget>[
  11. RaisedButton(
  12. child: Text('跳转到搜索页面'),
  13. onPressed: (){
  14. //页面跳转:
  15. Navigator.of(context).push(
  16. MaterialPageRoute(
  17. builder: (context)=>SearchPage()
  18. )
  19. );
  20. },
  21. color: Theme.of(context).accentColor,
  22. textTheme: ButtonTextTheme.primary
  23. ),
  24. RaisedButton(
  25. child: Text('跳转到表单页面并传值'),
  26. onPressed: (){
  27. },
  28. color: Theme.of(context).accentColor,
  29. textTheme: ButtonTextTheme.primary
  30. )
  31. ],
  32. );
  33. }
  34. }

pages/tabs/Setting.dart

  1. import 'package:flutter/cupertino.dart';
  2. class SettingPage extends StatelessWidget {
  3. const SettingPage({Key key}) : super(key: key);
  4. @override
  5. Widget build(BuildContext context) {
  6. return Container(
  7. child: Text('设置'),
  8. );
  9. }
  10. }

转载于:https://www.cnblogs.com/yiweiyihang/p/11468852.html

发表评论

表情:
评论列表 (有 0 条评论,210人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Flutter详解

    lutter中跳转一直记了忘,忘了记,在这里记录下吧: 和安卓中的intent类似,跳转新页面需要当前上下文以及需要跳转的页面。如果有数据需要传输也可,参数通过跳转页面...

    相关 Vue+

    之前在原生JS的开发中,我们经常会用到根据某一状态进行页面的跳转。 比如:登录成功跳到首页,点击商品列表的某个商品跳转商品详情等。 而常见的写法就是: locat