project_name/images 사진 넣기
pubspec.yaml
flutter:
uses-material-design: true
assets:
- images/
MaterialApp ▶ GetMaterialApp
void main() {
return runApp(
GetMaterialApp(
home: Scaffold(
backgroundColor: Colors.lime,
body: _DicePage(),
),
),
);
}
Controller class
class Controller extends GetxController {
var _leftDiceNumber = 1.obs;
var _rightDiceNumber = 1.obs;
changeDiceFaceLeft() {
_leftDiceNumber.value = Random().nextInt(6) + 1; // 0-5
}
changeDiceFaceRight() {
_rightDiceNumber.value = Random().nextInt(6) + 1; // 0-5
}
}
StatefulWidget ▶ StatelessWidget
class _DicePage extends StatelessWidget {
Controller c = Get.put(Controller());
@override
Widget build(BuildContext context) {
return Center(
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: FlatButton(
onPressed: () {
c.changeDiceFaceLeft();
},
child: Obx(() => Image.asset('images/dice${c._leftDiceNumber}.png'))),
),
Expanded(
flex: 1,
child: FlatButton(
onPressed: () {
c.changeDiceFaceRight();
},
child: Obx(() => Image.asset('images/dice${c._rightDiceNumber}.png'))),
)
],
),
);
}
}
결과
320x100
'프로그래밍 > Flutter-Dart' 카테고리의 다른 글
[Flutter] Card (0) | 2021.03.25 |
---|---|
[Flutter] Unsound null safety 관련 (0) | 2021.03.25 |
[Flutter] Shared preferences (0) | 2021.03.25 |
[Flutter] cached_network_image (0) | 2021.03.25 |
[Flutter] get package (2) (0) | 2021.03.25 |
댓글