简体   繁体   English

如何从 Flutter 中的下拉按钮导航到另一个屏幕?

[英]How to navigate to another screen from dropdownbutton in Flutter?

I am trying to get a dropdownlist to navigate to another screen once one of the items in the list is pressed via the dropdownbutton.一旦通过下拉按钮按下列表中的一个项目,我就会尝试获取一个下拉列表以导航到另一个屏幕。 I have tried using Navigator.push straight into onChanged but that doesnt work.我曾尝试使用 Navigator.push 直接进入 onChanged 但这不起作用。 And i have tried creating a new button in set state in onChanged.我已经尝试在 onChanged 中创建一个处于设置状态的新按钮。 How can I do this because I do not know how?我该怎么做,因为我不知道怎么做?

import 'package:flutter/material.dart';

void main() => runApp(new HomeNavigator());

class HomeNavigator extends StatefulWidget {
@override
_HomeNavigator createState() => _HomeNavigator();
}

class _HomeNavigator extends State<HomeNavigator> {
List<DropdownMenuItem<String>> listMunicipalities = [];
String selected = null;
void loadData() {
listMunicipalities = [];
listMunicipalities.add(new DropdownMenuItem(
  child: new Text('Port Moody'),
  value: 'Port Moody',
));
listMunicipalities.add(new DropdownMenuItem(
  child: new Text('Vancouver Downtown'),
  value: 'Vancouver Downtown',
));
listMunicipalities.add(new DropdownMenuItem(
  child: new Text('Coquitlam'),
  value: 'Coquitlam',
));
}

@override
Widget build(BuildContext context) {
loadData();
Color gradientStart = Colors.deepOrange[700];
Color gradientEnd = Colors.purple[500];

return new MaterialApp(
    home: new Scaffold(
        body: new Container(
            decoration: new BoxDecoration(
              gradient: new LinearGradient(
                  colors: [gradientEnd, gradientStart],
                  begin: new FractionalOffset(0.0, 0.5),
                  end: new FractionalOffset(0.5, 0.0),
                  stops: [0.0, 1.0]),
            ),
            child: Stack(children: [
              Container(
                  child: Text('',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontSize: 30.0,
                          fontFamily: 'College-Block',
                          color: Colors.white.withOpacity(0.7))),
                  alignment: Alignment(0.0, -0.5)),
              Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: new Center(
                      child: new Container(
                    alignment: Alignment(0.0, 0.05),
                    child: Container(
                        width: 350.0,
                        child: DropdownButtonHideUnderline(
                          child: new DropdownButton(
                            value: selected,
                              items: listMunicipalities,
                              hint: Text(
                                'Select City',
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    color: Colors.white.withOpacity(0.5)),
                              ),
                              onChanged: (value){

                              }),
                        )),
                  )))
            ]))));
}
}


class HomePage extends StatefulWidget{
@override
_HomePage createState() => _HomePage();
}

class _HomePage extends State<HomePage> {


Widget build(BuildContext context){
return Scaffold(
  appBar: AppBar(
    title: Text('')
  ),
 );  
}
}

you can just use simple switch case over there.你可以在那里使用简单的开关盒。 refer below example to clear idea.请参阅下面的示例以明确想法。

  import 'package:flutter/material.dart';

  void main() {
    runApp(MaterialApp(
      title: 'Navigation Basics',
      home: FirstScreen(),
    ));
  }

  class FirstScreen extends StatelessWidget {

    String _selectedGender=null;


    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('First Screen'),
        ),
        body: Column(
          children: <Widget>[
            DropdownButton(
              value: _selectedGender,
              items: _dropDownItem(),
              onChanged: (value){
                _selectedGender=value;
                switch(value){
                  case "Male" :
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => SecondScreen()),
                    );
                    break;
                  case "Others" :
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => SecondScreen()),
                    );
                    break;
                  case "Female" :
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => third()),
                    );
                    break;
                }
              },
              hint: Text('Select Gender'),
            ),
          ],
        ),
      );
    }


    List<DropdownMenuItem<String>> _dropDownItem() {
      List<String> ddl = ["Male", "Female", "Others"];
      return ddl.map(
              (value) =>
              DropdownMenuItem(
                value: value,
                child: Text(value),
              )
      ).toList();
    }
  }


  class SecondScreen extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text("Second Screen"),
        ),
        body: Center(
          child: RaisedButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text('Go back!'),
          ),
        ),
      );
    }
  }

  class third extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text("tgird Screen"),
        ),
        body: Center(
          child: RaisedButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text('Go back!'),
          ),
        ),
      );
    }
  }

for detail go to document https://flutter.io/cookbook/navigation/navigation-basics/有关详细信息,请参阅文档https://flutter.io/cookbook/navigation/navigation-basics/

Add below line in your code for Navigation在导航代码中添加以下行

onChanged: (value){
     Navigator.push(context,MaterialPageRoute(builder: (context) => 
     YourScreenInstance()),);
}
Navigator.popAndPushNamed(context, "/YourScreenInstance");
Navigator.of(context).pushNamed('/NewPage');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM