문제 설명
나는 세 개의 텍스트 필드가 있는데 그 중 처음 두 개 중에서 선택하고 세 번째 필드로 작업하고 싶습니다. (I have three text fields, want to choose between first two of them, & work with third one)
텍스트 필드가 세 개 있습니다. 나는 그들 중 처음 두 가지 중에서 선택하고 싶습니다. & 세 번째로 작업하세요.
어떻게 할까요? 안내해주세요
6import 'package:flutter/material.dart';
void main() => runApp(new CalculatorApp());
class CalculatorApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(title:'Bending Calculator',
home: Calculator()
);
}
}
class Calculator extends StatefulWidget {
@override
State<StatefulWidget> createState() => Calculatore();
}
class Calculatore extends State<Calculator> {
final a = TextEditingController();
final b = TextEditingController();
final c = TextEditingController();
// controller mentioned
String total= "";
void calculate() {
int numberA = int.parse(a.text);
int numberB = int.parse(b.text);
int numberC = int.parse(c.text);
int result;
// if numberA have value then answer will be a+c
if(
// what condition to do here for between choosing between textfields a or b.
// i tried numberB ==null that does not work
// very much confused, no idea what to do please help
){
result = numberA + numberC
} else{ result = numberB + numberC
}
setState(() {
total = "$result";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(appBar: AppBar(title:Text("Calculator")),
body: SafeArea(
child: ListView(
children: <Widget>[
Row( mainAxisAlignment: MainAxisAlignment.center ,
children: <Widget>[
// first textfield
Container(width: MediaQuery.of(context).size.width *0.45 ,height: 50,
child: TextField(
controller: a,
decoration: InputDecoration(hintText: " Enter a value"),
keyboardType: TextInputType.number),
),
Container(width: MediaQuery.of(context).size.width * 0.04,height: 50,),
// second textfield
Container(width: MediaQuery.of(context).size.width* 0.45,height: 50,
child: TextField(
controller: b,
decoration: InputDecoration(hintText: " Enter b value"),
keyboardType: TextInputType.number),), ],
),
Row(mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// third textfield
Container(width: MediaQuery.of(context).size.width *0.9, height: 50,
child: TextField(
controller: c,
decoration: InputDecoration(hintText: " Enter c value"),
keyboardType: TextInputType.number),),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//button
RaisedButton(
onPressed: calculate,
child: Text('Calculate'),),
],
),
Text(" Total : $total", style: TextStyle(fontSize: 20.0),),
],
))
);
}
}
참조 솔루션
방법 1:
Based on your comment this should perhaps help. A normal int.parse
will throw error if the text you provide to parse is empty. Instead use int.tryParse
as in the documentation here. This will return a null
if the provided string is empty. Live version available in this dartpad.
int numberA = int.tryParse(a.text);
int numberB = int.tryParse(b.text);
int numberC = int.tryParse(c.text);
int result;
// if numberA have value then answer will be a+c
// Note following conditions have order precedence. So only one of them execute.
if (numberA != null && numberC != null) {
result = numberA + numberC;
} else if (numberB != null && numberC != null){
result = numberB + numberC;
}
If you want to calculate the totat if all three fields are present. Use the following condition.
if (numberA != null && numberB != null && numberC != null) {
result = numberA + numberB + numberC;
} else if (numberA != null && numberC != null) {
result = numberA + numberC;
} else if (numberB != null && numberC != null){
result = numberB + numberC;
}
You can achieve the same by checking the text instead of parsed number.
방법 2:
As I understand you have tried checking whether the value of text field b is null. Have you tried to check whether b.text.isEmpty
or isNotEmpty
.
방법 3:
"" can't parse to int. You can use "try & catch" or "tryParese". How about this?
void calculate() {
int numberA = int.tryParse(a.text);
int numberB = int.tryParse(b.text);
int numberC = int.tryParse(c.text);
int result;
if (numberB == null) {
result = numberA + numberC;
} else {
result = numberB + numberC;
}
setState(() {
total = "$result";
});
}
(by Manish Kumar Tomar、Abhilash Chandran、Dennis、Yuu Woods)