Flutter界面跳转

第一种带参数跳转

import 'package:flutt/scrond_page.dart';
import 'package:flutt/third_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const CupertinoApp(
    title: 'Navigation Basics',
    home: FirstRoute(),
  ));
}

class FirstRoute extends StatelessWidget {
  const FirstRoute({super.key});

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: const CupertinoNavigationBar(
        middle: Text('First Route'),
      ),
      child: Center(
        child: CupertinoButton(
          child: const Text('O1pen route'),
          onPressed: () {
            Navigator.push(
              context,
              //1.带参数跳转 MaterialPageRoute(builder: (context) => const SecondPage(name: "124")),
              MaterialPageRoute(builder: (context) => const ThirdPage(),settings:
              const RouteSettings(arguments: "008")
              ),
            );
          },
        ),
      ),
    );
  }
}

2、带参数接收

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class SecondPage extends StatelessWidget {
  final String name ;
  const SecondPage({super.key, required this.name});

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar:  CupertinoNavigationBar(
        middle: Text(name),
      ),
      child: Center(
        child: CupertinoButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: const Text('Go back!'),
        ),
      ),
    );
  }
}

3.不带参数接收

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ThirdPage extends StatelessWidget {

  const ThirdPage({super.key});

  @override
  Widget build(BuildContext context) {
    final name = ModalRoute.of(context)!.settings.arguments as String;
    return CupertinoPageScaffold(
      navigationBar:  CupertinoNavigationBar(
        middle: Text(name),
      ),
      child: Center(
        child: CupertinoButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: const Text('Go back!'),
        ),
      ),
    );
  }
}

 

posted @ 2024-02-28 11:58  ZhangShengjie  阅读(67)  评论(0编辑  收藏  举报