我有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
有人能帮忙吗?
1条答案
按热度按时间pdtvr36n1#