alertdialog

能否在AlertDialog中嵌入其他控件

小樊
83
2024-09-04 16:50:32
栏目: 编程语言

是的,您可以在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"按钮时,将显示此对话框。请注意,您可以根据需要添加任何其他控件。

0
看了该问题的人还看了