如何在onTap中使用条件语句

时间:2019-01-24 14:15:02

标签: dart flutter

关于dart中的if语句,我有一个问题,我希望用户点击城市以转到新屏幕。这段代码工作得很好


class citySec extends StatelessWidget {
  Widget getListView(BuildContext context) {
    var listView = ListView(
      children: <Widget>[
        Text(
          "choose ur city:",
          textDirection: TextDirection.rtl,
          textAlign: TextAlign.center,
        ),
        ListTile(
          leading: Icon(Icons.location_city),
          title: Text("Toronto ", textDirection: TextDirection.rtl),

          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => TorontoUniversitySection(),
              ),
            );
          },
        ),
      ],
    );
    return listView;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: getListView(context));
  }
}

因为我的城市列表很长,并且以前的代码会使我的代码很长,所以我不得不更改代码。但是,我在使用if语句时遇到了一些错误,这是我到目前为止所做的。


import 'package:flutter/material.dart';
import 'package:rate/screens/firstScreen.dart';

void main() {
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'Rate',
    home: Scaffold(
      appBar: AppBar(
        title: Text("jgfnjfnj ", textDirection: TextDirection.rtl),
      ),
      body: ListDisplay(),
    ),
  ));
}
class ListDisplay extends StatelessWidget {
  List<String> litems = ["Toronto","NewYork","London","Riyadh","Dubai","Istanbul"];

  @override
  Widget build (BuildContext ctxt) {
    return new Scaffold(
      appBar: AppBar(title: Text("Please Choose your city: ", textDirection: TextDirection.ltr,),
      ),

    body: new ListView.builder
    (
    itemCount: litems.length,

    itemBuilder: (BuildContext ctxt, int index) {

    return new ListTile(
      leading: Icon(Icons.location_city),
      title: Text(litems[index], textDirection: TextDirection.rtl),

      onTap: () {
// begin of all IF statements

        if (litems.contains("Totonto")){
          Navigator.push(
            ctxt,
            MaterialPageRoute(

              builder: (ctxt) => TorontoUniversitySection()
            ),
          );

        }
if (litems.contains("London")){
          Navigator.push(
            ctxt,
            MaterialPageRoute(

              builder: (ctxt) => LondonUniversitySection()
            ),
          );

        }

// end of all If statements

      },
    );

    }
    )
    );
  }
}

3 个答案:

答案 0 :(得分:1)

  

例如,当我按下多伦多时,它将带我到LondonUniversitySection()

那是因为在您的if语句中,您检查您的列表中是否包含多伦多/伦敦,而不是如果当前按的是多伦多/伦敦,则不会。将litems.contains("x")更改为litems[index] == "x"将达到目的。这是经过编辑的片段:

    return new ListTile(
      leading: Icon(Icons.location_city),
      title: Text(litems[index], textDirection: TextDirection.rtl),
      onTap: () {
        if (litems[index] == "Toronto") {
          Navigator.push(
            ctxt,
            MaterialPageRoute(builder: (ctxt) => TorontoUniversitySection()),
          );
        } else if (litems[index] == "London") {
          Navigator.push(
            ctxt,
            MaterialPageRoute(builder: (ctxt) => LondonUniversitySection()));
        }
      },
    );

此外,我建议为此使用switchelse-if,而不是一堆ifs。

答案 1 :(得分:0)

尝试onTap: litems.contains("Totonto")? Navigator.push( ctxt, MaterialPageRoute( builder: (ctxt) => TorontoUniversitySection() ), ) : null

答案 2 :(得分:0)

class _RegisterBodyState extends State<RegisterBody> {
  FocusNode myFocusNode = new FocusNode();
  FocusNode myFocusNode2 = new FocusNode();
  void initState() {
    super.initState();

    myFocusNode = FocusNode();
    myFocusNode2 = FocusNode();
  }

  @override
  void dispose() {
    // Clean up the focus node when the Form is disposed.
    myFocusNode.dispose();
    myFocusNode2.dispose();

    super.dispose();
  }

  Color color;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              "Register",
              style: TextStyle(
                color: Theme.of(context).primaryColor,
                fontSize: 70.0,
                fontWeight: FontWeight.bold,
              ),
            ),
            Form(
                child: Column(
              children: [
                TextFormField(
                  onTap: () {
                    setState(() {
                      color = Colors.red;
                    });
                  },
                  autofocus: true,
                  focusNode: myFocusNode,
                  decoration: InputDecoration(
                    icon: Icon(
                      Icons.supervised_user_circle,
                      size: 40.0,
                    ),
                    labelText: "User Name",
                    labelStyle: TextStyle(
                        color:
                            myFocusNode.hasFocus ?color : Colors.yellow),
                  ),
                ),
                TextFormField(
                  focusNode: myFocusNode2,
                  onTap: () {
                    setState(() {
                      color = Colors.black;
                    });
                  },
                  autofocus: false,
                  decoration: InputDecoration(
                    icon: Icon(
                      Icons.supervised_user_circle,
                      size: 40.0,
                    ),
                    labelText: "User Name",
                    labelStyle: TextStyle(
                      color: myFocusNode2.hasFocus ? color : Colors.teal,
                    ),
                  ),
                ),
              ],
            ))
          ],
        ),
      ),
    );
  }
}