另一个文件中的Flutter调用弹出窗体小部件

fzsnzjdm  于 2023-05-01  发布在  Flutter
关注(0)|答案(1)|浏览(120)

我有10种不同的表单供用户向数据库添加不同的对象。我希望将它们放在单独的.dart文件中,以便更轻松地管理它们。
我想的形式是一个弹出对话框,所以我需要showDialog()
当用户在main.dart中按下onPress()时,我设法让弹出表单工作,但无法解决如何创建一个单独的表单小部件文件,当从main.dart中选择onPress()时弹出。
代码如下:
main.dart

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(useMaterial3: true, colorScheme: lightColorScheme),
      darkTheme: ThemeData(useMaterial3: true, colorScheme: darkColorScheme),
      home: const Home(),
    );
  }
}

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    const int tabsCount = 3;
    return DefaultTabController(
      initialIndex: 0,
      length: tabsCount,
      child: Scaffold(
        appBar: AppBar(

// more code

floatingActionButton: SpeedDial(
          backgroundColor: const Color(0xFFE9C46A),
          buttonSize: Size(90, 90),
          tooltip: 'Add any Object',
          overlayColor: Colors.grey,
          overlayOpacity: 0.8,
          icon: Icons.add,
          //animatedIcon: AnimatedIcons.add,
          curve: Curves.linear,
          children: [
            SpeedDialChild(
              child: Icon(Icons.person),
              label: "Person",
              backgroundColor: Color.blue,
              onTap: () {           //I want to put part this in a widget in another file 
                showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return AlertDialog(
                      scrollable: true,
                      title: const Text("Login"),
                      content: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Form(
                          child: Column(
                            children: [
                              TextFormField(
                                decoration: const InputDecoration(
                                  labelText: "Name",
                                  icon: Icon(Icons.account_box),
                                ),
                              ),
                              TextFormField(
                                decoration: const InputDecoration(
                                  labelText: "Email",
                                  icon: Icon(Icons.email),
                                ),
                              ),
                              TextFormField(
                                decoration: const InputDecoration(
                                  labelText: "Message",
                                  icon: Icon(Icons.message),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      actions: [
                        ElevatedButton(
                          child: const Text("submit"),
                          onPressed: () {
                            // your code
                          },
                        ),
                      ],
                    );
                  },
                );
              },
            ),
            SpeedDialChild(child:
// more code to the eof
}

以上更改为:onTap: () { CustomForm() }
Custom_Form_Widget.dart中,我尝试了以下和许多其他变体:

import 'package:flutter/material.dart';

class CustomForm extends StatelessWidget {
  const CustomForm({super.key});

  @override
  Widget build(BuildContext context) {
    return showDialog(
                context: context,
                builder: (BuildContext context) {
                return AlertDialog(

//rest of the code

有人能帮忙吗?

pdtvr36n

pdtvr36n1#

**1. you could create this way in the custom_form_widget.dart**
        
        class CustomForm extends StatefulWidget {
      const CustomForm({Key? key}) : super(key: key);
    
      @override
      _CustomFormState createState() => _CustomFormState();
    }
    
    class _CustomFormState extends State<CustomForm> {
      final TextEditingController _nameController = TextEditingController();
      final TextEditingController _emailController = TextEditingController();
      final TextEditingController _messageController = TextEditingController();
    
      @override
      void dispose() {
        _nameController.dispose();
        _emailController.dispose();
        _messageController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return AlertDialog(
          scrollable: true,
          title: const Text("Login"),
          content: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Form(
              child: Column(
                children: [
                  TextFormField(
                    controller: _nameController,
                    decoration: const InputDecoration(
                      labelText: "Name",
                      icon: Icon(Icons.account_box),
                    ),
                  ),
                  TextFormField(
                    controller: _emailController,
                    decoration: const InputDecoration(
                      labelText: "Email",
                      icon: Icon(Icons.email),
                    ),
                  ),
                  TextFormField(
                    controller: _messageController,
                    decoration: const InputDecoration(
                      labelText: "Message",
                      icon: Icon(Icons.message),
                    ),
                  ),
                ],
              ),
            ),
          ),
          actions: [
            ElevatedButton(
              child: const Text("submit"),
              onPressed: () {
                String name = _nameController.text;
                String email = _emailController.text;
                String message = _messageController.text;
                // do something with the input data
                Navigator.pop(context); // close the dialog
              },
            ),
          ],
        );
      }
    }

  **2. how to call the form.**

 Scaffold(
        appBar: AppBar(
          // more code
        ),
        floatingActionButton: SpeedDial(
          backgroundColor: const Color(0xFFE9C46A),
          buttonSize: Size(90, 90),
          tooltip: 'Add any Object',
          overlayColor: Colors.grey,
          overlayOpacity: 0.8,
          icon: Icons.add,
          curve: Curves.linear,
          children: [
            SpeedDialChild(
              child: Icon(Icons.person),
              label: "Person",
              backgroundColor: Color.blue,
              onTap: () {
                showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return CustomForm();
                  },
                );
              },
            ),
            // more code to the eof
          ],
        ),
      ),
    );
  }
}

相关问题