๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ฑ Mobile/ํ”Œ๋Ÿฌํ„ฐ ์•ฑํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”Œ๋Ÿฌํ„ฐ ์•ฑํ”„๋กœ๊ทธ๋ž˜๋ฐ Day6

ํ”Œ๋Ÿฌํ„ฐ ์•ฑํ”„๋กœ๊ทธ๋ž˜๋ฐ Day6

์ง€๋‚œ ์‹œ๊ฐ„๊นŒ์ง€๋Š” ๊ธฐ๋ณธ์ ์ธ ํ”Œ๋Ÿฌํ„ฐ ์œ„์ ฏ์„ ๋ดค์—ˆ๋‹ค.

์ด๋ฒˆ์—๋Š” ๊ณ„์‚ฐ๊ธฐ ์•ฑ์„ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉ์„ ํ•˜๋Š” ์œ„์ ฏ์„ ๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฒ„ํŠผ์€ ์‚ฌ์šฉ์ž๊ฐ€ ๋ˆŒ๋ €์„ ๋•Œ onPressed๋ผ๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

 

 

 

๋ง์…ˆ ๊ณ„์‚ฐ๊ธฐ ์•ฑ ๋งŒ๋“ค๊ธฐ

1. ์ƒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  main.dart ํŒŒ์ผ์—์„œ Column์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ StatefulWidget์„ ๋งŒ๋“ ๋‹ค.

 

2. Column์— children์„ ์„ ์–ธํ•œ ํ›„ ์ฐจ๋ก€๋Œ€๋กœ Text, Textfield, ElevateButton ์œ„์ ฏ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

ElevateButton ์œ„์ ฏ์˜ onPressed๋Š” ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ธ์„ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ์ด๋‹ค. 

child : Column(
    children : <Widget>[
        Text('flutter'),
        TextField(),
        ElevatedButton(child: const Text(''), onPressed: () {
        })
    ],
),

 

 

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์œ„์ ฏ๋“ค์ด ์„œ๋กœ ๋ถ™์–ด ๋ณด์ธ๋‹ค.

3. ์œ„์ ฏ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ๋” ๋ฒŒ๋ฆฌ๊ธฐ ์œ„ํ•ด Padding์„ ์ด์šฉํ•œ๋‹ค.

child : Column(
    children : <Widget>[
    	Padding(
        	padding: EdgeInsets.all(15),
        	child : Text('flutter'),
        ),
        Padding(
        	padding: EdgeInsets.only(left: 20, right: 20),
        	child : TextField(),
        ),
        Padding(
        	padding: EdgeInsets.all(15),
        	child : ElevatedButton(child: const Text(''), onPressed : () {}),
        ),
    ],
),

 

4. ๋‘ ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„์„œ ๋ง์…ˆ ํ›„ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

๋ฐ์ดํ„ฐ ์ž…๋ ฅ์€ ํ…์ŠคํŠธํ•„๋“œ(TextField)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

ํ…์ŠคํŠธํ•„๋“œ๋ฅผ ๋‹ค๋ฃจ๋ ค๋ฉด TextEditingController๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

_WidgetExampleState ํด๋ž˜์Šค์— TextEditingController๋ฅผ ์„ ์–ธํ•œ๋‹ค.

class WidgetExampleState extends State<WidgetApp> {
    String sum = '';
    TextEditingController value1 = TextEditingController();
    TextEditingController value2 = TextEditingController();
    ...

 

5. ์ฒซ ๋ฒˆ์งธ ํ…์ŠคํŠธํ•„๋“œ์—๋Š” value1์„, ๋‘ ๋ฒˆ์งธ ํ…์ŠคํŠธํ•„๋“œ์—๋Š” value2๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

Padding(
    padding : EdgeInsets.only(left: 20, right: 20),
    child: TextField(keyboardType: TextInputType.number, controller: value1),
),

Padding(
	padding : EdgeInsets.only(left: 20, right: 20),
    child: TextField(keyboardType: TextInputType.number, controller: value2),
),

keyboardType์—๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์ผ ํ‚ค๋ณด๋“œ๋ฅผ ์ง€์ •ํ•œ๋‹ค.

number๋ฅผ ์ง€์ •ํ•˜๋ฉด ์ˆซ์ž ์ž…๋ ฅ์šฉ ํ‚ค๋ณด๋“œ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

 

 

ํ”Œ๋Ÿฌํ„ฐ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ํ‚ค๋ณด๋“œ ์œ ํ˜•์€ number๋ฟ๋งŒ ์•„๋‹ˆ๋ผ

text(๊ธฐ๋ณธ ํ…์ŠคํŠธ), multiline(๋ฉ€ํ‹ฐ ๋ผ์ธ ํ…์ŠคํŠธ), phone(์ „ํ™”๋ฒˆํ˜ธ ์ „์šฉ),

datetime(๋‚ ์งœ ์ž…๋ ฅ), emailAddress(@ ํ‘œ์‹œ), url(์ฃผ์†Œ ์ž…๋ ฅ) ๋“ฑ์ด ์žˆ๋‹ค. 

 

 

 

6. ๋”ํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋‘ ์ˆ˜๋ฅผ ๋ง์…ˆ ํ›„ ์ถœ๋ ฅํ•œ๋‹ค.

๋จผ์ € ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ณผ sum์„ Text ์œ„์ ฏ์— ํ‘œ์‹œํ•˜๋Š” ๋ถ€๋ถ„์„ ์ž‘์„ฑํ•œ๋‹ค.

child: Text(
	'๊ฒฐ๊ณผ: $sum',
    style: TextStyle(fontSize: 20),
),

 

7. ๋‘ ์ˆ˜๋ฅผ ๋”ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ๋‘ ์ˆ˜๋Š” ํ˜„์žฌ ์‚ฌ์šฉ์ž๊ฐ€ ํ…์ŠคํŠธํ•„๋“œ์— ์ž…๋ ฅํ–ˆ์œผ๋ฏ€๋กœ ์ด ๊ฐ’์„ ๊ฐ€์ ธ์™€์•ผ ํ•œ๋‹ค.

๋ง์…ˆ ์—ฐ์‚ฐ์€ ์‚ฌ์šฉ์ž๊ฐ€ ๋”ํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ์ด๋ค„์ ธ์•ผ ํ•˜๋ฏ€๋กœ ElevatedButton์˜ onPressed ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๋ถ€๋ถ„์— ์ž‘์„ฑํ•œ๋‹ค.

onPressed: () {
	setState(() {
		int res = int.parse(value1.value.text) + int.parse(value2.value.text);
		sum = '$result';
	});
}
...

 

 

 

๋บ„์…ˆ, ๋‚˜๋ˆ—์…ˆ, ๊ณฑ์…ˆ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ํ•˜๊ธฐ

์Šคํ…Œ์ดํŠธํ’€์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒํƒœ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ํ•จ์ˆ˜์ธ initState()ํ•จ์ˆ˜ ์ž‘์„ฑํ•œ๋‹ค. 

  @override
  void initState() {
    super.initState();
    for (var item in _buttonList) {
      _dropDownMenuItems.add(DropdownMenuItem(value: item, child: Text(item)));
    }
    buttonText = _dropDownMenuItems[0].value;
  }

initState() ํ•จ์ˆ˜์—์„œ _buttonList์— ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜์”ฉ ๋นผ์„œ _dropDownMenuItems์—

์ถ”๊ฐ€ํ•จ์œผ๋กœ์จ ์•„์ดํ…œ ๋ชฉ๋ก์„ ํŽผ์นจ ๋ฒ„ํŠผ์— ๋„ฃ์„ ๋ฉ”๋‰ด ์•„์ดํ…œ์œผ๋กœ ๋งŒ๋“ ๋‹ค. 

 

 

 

์‚ฌ์šฉ์ž๊ฐ€ ํŽผ์นจ ๋ฒ„ํŠผ์—์„œ ์•„์ดํ…œ์„ ์„ ํƒํ–ˆ์„ ๋•Œ ์—ฐ์‚ฐ ๋ฒ„ํŠผ์˜ ๊ฐ’๋„ ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ๊ฐ’์œผ๋กœ ๋ฐ”๋€Œ๋„๋ก ์ˆ˜์ •ํ•œ๋‹ค.

Text(_buttonText!)

 

 

์—ฐ์‚ฐ์„ ์„ ํƒํ•˜๊ณ  ์—ฐ์‚ฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ…์ŠคํŠธํ•„๋“œ์— ์žˆ๋Š”  ๋‘ ๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•œ๋‹ค. 

var value1Int = double.parse(value1.value.text);
var value2Int = double.parse(value2.value.text);
var result;
if (buttonText == '๋”ํ•˜๊ธฐ') {
	result = value1Int + value2Int;
} 
else if (buttonText == '๋นผ๊ธฐ') {
	result = value1Int - value2Int;
} 
else if (buttonText == '๊ณฑํ•˜๊ธฐ') {
	result = value1Int * value2Int;
} 
else {
	result = value1Int / value2Int;
}

 

 

์ตœ์ข…์ ์œผ๋กœ ์‹คํ–‰์„ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

728x90