9. Dialog 구성 요소
import 'package:flutter/material.dart';
main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Test',
home: new Scaffold(
appBar: new AppBar(
title: new Text('Test')
),
// body: new MyAlertDialogView()
body: new MySimpleDialogView(),
),
);
}
}
class MyAlertDialogView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new RaisedButton(
child: new Text(' AlertDialog'),
onPressed: () {
showDialog(
context: context,
barrierDismissible: false, // ,
builder: (BuildContext context) {
return new AlertDialog(
title: new Text(' '),
content: new Text(' Windows 7 2020 1 , Windows 10。'),
actions: [
new FlatButton(
child: new Text(' '),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
},
);
}
}
class MySimpleDialogView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new RaisedButton(
child: new Text(' SimpleDialog'),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext ctx) {
return new SimpleDialog(
title: new Text(' SimpleDialog'),
children: [
new SimpleDialogOption(
onPressed: () { Navigator.pop(context); },
child: const Text(' '),
),
new SimpleDialogOption(
onPressed: () { Navigator.pop(context); },
child: const Text(' '),
),
],
);
}
);
},
);
}
}
위 코드는 각각 SimpleDialog와 AlertDialog의 기본 사용법을 보여 줍니다.주의해야 할 것은 여기에 버튼과 대화상자를 표시하는 논리를 MyApp류에 직접 쓰지 않고 두 개의 StatelessWidget로 나뉘어 썼다는 것이다. 만약에 버튼과 대화상자를 표시하는 논리를 MyApp의build 방법에 직접 쓰면 오류가 발생할 수 있다. 구체적인 오류 메시지는 다음과 같다.
Navigator operation requested with a context that does not include a Navigator.
네비게이션 조작에 Navigator가 포함되지 않은 상하문 대상이 필요하다는 뜻이다. 만약에 우리가 showDialog의 논리를 MyApp의build 방법에 썼을 때 MaterialApp의 상하문 대상을 사용했는데 이 상하문 대상은 Navigator를 포함하기 때문에 오류가 발생할 수 있다.위의 코드가 시뮬레이터에서 실행되는 효과는 아래 그림과 같습니다
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.