是的,您可以在AlertDialog
中嵌入其他控件
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('AlertDialog with Other Widgets')),
body: Center(child: Builder(builder: (context) {
return ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Title'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('This is a text widget.'),
SizedBox(height: 10),
TextFormField(
decoration: InputDecoration(labelText: 'Name'),
),
SizedBox(height: 10),
RaisedButton(
onPressed: () {},
child: Text('A button widget'),
),
],
),
actions:<Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Close'),
)
],
);
},
);
},
child: Text('Show AlertDialog'),
);
})),
),
);
}
}
在這個示例中,我們創建了一個包含文本、輸入框和按鈕的AlertDialog
。當用戶點擊"Show AlertDialog"按鈕時,將顯示此對話框。請注意,您可以根據需要添加任何其他控件。