我想导航到主屏幕而不使用抽屉

时间:2020-06-21 06:32:25

标签: flutter dart flutter-navigation

在传递下面的字符串属性时,导航是否可以直接进入主屏幕而不是pop(抽屉)?

不能使用pushNamed传递cityName属性。 请告诉我是否有办法

我可以在onpressed上使用navigator.pop以外的其他内容吗?

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

class CityScreen extends StatelessWidget {
static const String routeName = '/cityScreen';

@override
Widget build(BuildContext context) {
    final _mycontroller = TextEditingController();
    String cityName;

    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
            image: DecorationImage(
          image: AssetImage('assets/city.jpg'),
          colorFilter: ColorFilter.mode(
              Colors.black.withOpacity(0.6), BlendMode.dstATop),
          fit: BoxFit.cover,
        )),
        constraints: BoxConstraints.expand(),
        child: SafeArea(
          child: Column(
            children: <Widget>[
              Align(
                alignment: Alignment.topLeft,
                child: FlatButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Icon(Icons.arrow_back_ios, size: 30),
                ),
              ),
              Container(
                padding: EdgeInsets.all(25),
                child: TextField(
                  decoration: InputDecoration(
                      icon: Icon(Icons.search),
                      filled: true,
                      hintText: 'Enter City Name'),
                  controller: _mycontroller,
                  autofocus: true,
                  onChanged: (value) {
                   cityName = value;
                  },
                ),
              ),
              FlatButton(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(18.0),
                    side: BorderSide(color: Colors.white)),
                onPressed: () {
                  Navigator.pop(context, cityName);
                },
                child: Text(
                  'Click here',
                  style: kbuttonStyle,
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

您也可以像下面这样在pushReplacementNamed / pushNamed中传递参数:

 Navigator.pushReplacementNamed(
      context,
      "dashboard_route",
      arguments: "cityName",
    );

然后在仪表板Widget构建函数中执行此操作,以获取cityName:

final String cityName = ModalRoute.of(context).settings.arguments;