Flutter-Navigator普通路由以及传参

class _HomePageState extends State {
  @override
  Widget build(BuildContext context) {
    return Container(
        child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        ElevatedButton(
            onPressed: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) => SearchPage()));
            },
            child: Text('跳转到搜索')),
        ElevatedButton(
            onPressed: () {
              Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                return SearchPage(value: "传入参数",title: 'TITLE',);
              }));
            },
            child: Text('跳转到搜索'))
      ],
    ));
  }
}
import 'package:flutter/material.dart';

class SearchPage extends StatefulWidget {
  final String title;
  final String value;

  SearchPage({
     this.value='',
     this.title='',
  });

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

class _SearchPageState extends State {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () { Navigator.pop(context); },
        child: Text('返回'),
      ),
      appBar: AppBar(
        title: Text(widget.title.toString()),
      ),
      body: Text(widget.value.toString()),
    );
  }
}

这里是一个简单的普通路由

你可能感兴趣的:(Flutter-Navigator普通路由以及传参)