[英]the ShowDialog is not Working in Flutter Android
我从 Flutter 开始。今天,我学习了如何编写 showDialog 代码,但它不起作用。 我试图一次又一次地写它,但没有任何影响......这是我的代码:
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: Scaffold(appBar: AppBar(), body: MyHome())));
}
class MyHome extends StatefulWidget {
const MyHome({Key? key}) : super(key: key);
@override
State<MyHome> createState() => _MyHomeState();
}
class _MyHomeState extends State<MyHome> {
@override
void MyDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("title"),
content: Text("Thanks to clock"),
actions: <Widget>[
TextButton(
onPressed: () {},
child: Text("close"),
)
],
);
});
}
Widget build(BuildContext context) {
return TextButton(
onPressed: () {},
child: Text("Click On me!"),
);
}
}
请帮我。 谢谢...
您没有在代码中的任何地方调用MyDialog
。
更新代码:
import 'package:flutter/material.dart';
class SignUpScreen extends StatefulWidget {
const SignUpScreen({Key? key}) : super(key: key);
@override
State<SignUpScreen> createState() => _SignUpScreenState();
}
class _SignUpScreenState extends State<SignUpScreen> {
@override
void MyDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("title"),
content: Text("Thanks to clock"),
actions: <Widget>[
TextButton(
onPressed: () {},
child: Text("close"),
)
],
);
});
}
Widget build(BuildContext context) {
return TextButton(
onPressed: () {
MyDialog();
},
child: Text("Click On me!"),
);
}
}
问题是您没有通过按钮调用对话框功能。 根据代码约定重命名对话框函数,例如“showMyDialog”,然后调用它:
return TextButton(
onPressed: () => showMyDialog(),
child: Text("Click On me!"),
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.