728x90
js에서는 백틱을 이용해 js 변수명과 함께 표현할 수 있다.
let test = 'hello';
let result = `${test} world!`;
console.log(result);
// hello world!
dart에서도 비슷하게 사용할 수 있다.
var a = '나는';
var result = '$a 탐훈입니다';
pring(result);
// 나는 탐훈입니다
js | dart |
`${변수명}` | '$변수명' |
import 'package:flutter/material.dart';
import 'dart:math';
final randomizer = Random(); // 랜덤 함수 final로 할당함
class DiceRoller extends StatefulWidget {
@override
State<DiceRoller> createState() {
return _DiceRollerState();
}
}
class _DiceRollerState extends State<DiceRoller> {
var currentDiceRoll = 2;
void rollDice() {
//..
setState(() {
currentDiceRoll = randomizer.nextInt(6) + 1; // nextInt(6)은 0~5까지이기떄문에
});
}
@override
Widget build(context) {
return Column(mainAxisSize: MainAxisSize.max, children: [
Image.asset('assets/images/dice-$currentDiceRoll.png',
width: 400, height: 500),
const SizedBox(
height: 20,
),
TextButton(
onPressed: rollDice,
style: TextButton.styleFrom(
// padding: const EdgeInsets.only(top: 10),
foregroundColor: Colors.white,
textStyle: TextStyle(color: Colors.white, fontSize: 28),
),
child: Text("Roll Dice"))
]);
}
}
'Flutter' 카테고리의 다른 글
flutter) OutlinedButton을 label과 text로 사용하기 (1) | 2024.02.25 |
---|---|
flutter) 기본적인 플러터 구조 숙지를 위한 단순반복연습 (0) | 2024.02.15 |
flutter) StatefulWidget으로 re-excute build하기 (0) | 2024.02.12 |
flutter) 위젯 중앙으로 배치, 사진과 버튼 사이 간격 주기 (0) | 2024.02.12 |
flutter) TextButton으로 클릭이벤트 달기 (0) | 2024.02.12 |