gpt4 book ai didi

dart - 如何制作可重用的AlertDialog?

转载 作者:行者123 更新时间:2023-12-02 03:17:02 25 4
gpt4 key购买 nike

AlertDialog 的 Flutter 示例是:

Future<void> neverSatisfied() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Rewind and remember'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('You will never be satisfied.'),
Text('You\’re like me. I’m never satisfied.'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Regret'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}

但我想让它可重用,这样我就可以调用它并传入自定义文本,例如:

await neverSatisfied(context, text: text);

怎么办?

谢谢!

最佳答案

您可以像下面这样在单独的类中创建一个警告对话框,并且可以在任何您想要的地方使用它:

Note: pass context with "GlobalKey()" like used in below class otherwise alert dialog will not display and give you error such as "The specific widget that could not find a MaterialLocalizations ancestor was"

声明单独的实用程序类以帮助显示对话框:

Utility

import 'package:flutter/material.dart';

class Utility{

static Utility utility = null;

static Utility getInstance(){
if(utility == null){
utility = Utility();
}
return utility;
}

showAlertDialog(BuildContext context, String alertTitle, String alertMessage){
// set up the buttons
Widget cancelButton = FlatButton(
child: Text("Cancel"),
onPressed: () {
Navigator.pop(context);
},
);
Widget continueButton = FlatButton(
child: Text("Continue"),
onPressed: () {
Navigator.pop(context);
},
);

// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text(alertTitle),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(alertMessage)
],
),
),
actions: [
cancelButton,
continueButton,
],
);

// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);

}

}

在 main.dart 中使用该对话框如下:

Main.dart

import 'package:flutter/material.dart';
import 'package:flutter_demo_app/Utility.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
final navigatorKey = GlobalKey<NavigatorState>();
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: navigatorKey,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(title: Text('AlertDialog')),
body: Column(
children: <Widget>[
RaisedButton(
child: Text('Get AlertDialog 1'),
onPressed: () {
Utility.getInstance().showAlertDialog(navigatorKey.currentState.overlay.context, 'Rewind and remember', 'You will never be satisfied.');
},
),
RaisedButton(
child: Text('Get AlertDialog 2'),
onPressed: () {
Utility.getInstance().showAlertDialog(navigatorKey.currentState.overlay.context, 'RememberRewinded', 'You\’re like me. I’m never satisfied.');
},
)
],
),
),
);
}

}

关于dart - 如何制作可重用的AlertDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55833834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com