【问题标题】:how to create conditional statement with string in Flutter?如何在 Flutter 中使用字符串创建条件语句?
【发布时间】:2019-11-19 15:07:27
【问题描述】:

我正在尝试在 android studio 3.5.2 上创建一个颤振的应用程序,一切都已完全更新,

概念是这样的,有一个文本字段,还有一个提交按钮, 在提交按钮中,三个字符串/文本需要有效才能进入,

如果有人输入了一个不匹配的词,它将显示,不存在任何词,

例如,

弦乐 - 菲律宾、迪拜、日本

 if { 
       textfield does not match string,
       Show text hint - Does not match data,
    }
 Else
    {
       Welcome from "string"
    }

我还是新手,我知道这不是确切的代码,但我希望有人能帮我把它翻译成颤振代码,谢谢谁会帮忙。

【问题讨论】:

    标签: string if-statement flutter conditional-statements textfield


    【解决方案1】:

    试试这个,

    import 'package:flutter/material.dart';
    
    class Question1 extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return _Question1State();
      }
    }
    
    class _Question1State extends State<Question1> {
      TextEditingController _textController = TextEditingController();
    
      String infoText = '';
    
      List<String> countryList = [
        'philippines',
        'dubai',
        'japan',
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          body: Padding(
            padding: EdgeInsets.all(20.0),
            child: Column(
              children: <Widget>[
                TextField(
                  controller: _textController,
                  decoration: InputDecoration(
                    hintText: 'Enter Country',
                  ),
                ),
                SizedBox(
                  height: 25.0,
                ),
                FlatButton(
                  onPressed: () {
                    _validateUserInput(_textController.text);
                  },
                  child: Text('Submit'),
                  color: Colors.blue,
                ),
                SizedBox(
                  height: 25.0,
                ),
                Text(
                  infoText,
                  style: TextStyle(color: Colors.red),
                ),
              ],
            ),
          ),
        );
      }
    
      _validateUserInput(String input) {
        if (!countryList.contains(input.toLowerCase())) {
          setState(() {
            infoText = 'Does not match data';
          });
        } else {
          setState(() {
            infoText = 'Welcome from $input';
          });
        }
      }
    }
    

    【讨论】:

    • 哦,这很好,谢谢你给我这个样本,你是救命稻草
    • 请问如果有人在没有任何文本输入的情况下按下提交按钮,我要在这里输入什么代码?
    【解决方案2】:

    声明一个你需要匹配的字符串列表,

    List<String> mList = ['philippines', 'dubai', 'japan'];
    

    然后像这样匹配您的文本文件字符串,

        var myStr = 'dubai';//or your textFieldController.text
        if (mList.contains(myStr)) {
          print('Welcome from ${myStr}');
        } else {
          print('Does not match data');
        }
    
    

    或者正如@Rahul Patel 在他的回答中建议的那样,您可以通过三元运算符来完成,

    mList.contains(myStr) ? print('Welcome from ${myStr}') : print('Does not match data');
    
    

    【讨论】:

    • 这是为了颤振,对吧?我有点理解这个概念:)
    【解决方案3】:

    您可以使用三元运算符来解决此问题,它是条件 if else 的简单压缩语法。语法如下:

    (some conditional || or other conditional && required conditional)?someMethodToHandleSuccess:someMethodToHandleFailure
    

    (条件)?ifTrue:ifFalse 是通用语法。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2012-06-23
      • 1970-01-01
      • 2021-03-23
      • 2018-08-23
      • 2022-01-18
      • 2013-04-29
      • 2011-12-05
      • 2018-12-17
      • 1970-01-01
      相关资源
      最近更新 更多