我開始使用 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!"),
);
}
}
請幫我!謝謝...
uj5u.com熱心網友回復:
您沒有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!"),
);
}
}
uj5u.com熱心網友回復:
問題是您沒有通過按鈕呼叫對話框功能。根據代碼約定重命名對話框函式,例如“showMyDialog”,然后呼叫它:
return TextButton(
onPressed: () => showMyDialog(),
child: Text("Click On me!"),
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/443407.html
