我是flutter的新手)我想创建一个函数,当用户按下主.dart文件中的FloatingActionButton()时,该函数调用带有2个TextField()的showDialog()。
我不知道如何从另一个StatefulWidget(从另一个名为VVid.dart的文件)调用主文件StatefulWidget中的函数
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State<MyHomePage> createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
String cardText = '';
void updateCardText(String text) {
setState(() {
cardText = text;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Transaction App'),
),
body: Container(
color: Colors.amber,
height: 200,
width: 300,
child: Card(
child: Text(cardText),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
// Forma.showTextField(context);
});
},
child: Icon(Icons.add),
));
}
}
// vvid.dart //////////////////////////////////////
class Forma extends StatefulWidget {
@override
State<Forma> createState() => FormaState();
}
class FormaState extends State<Forma> {
final vvidText = TextEditingController();
final vvidAmount = TextEditingController();
void showTextField(BuildContext context) {
showDialog(
context: context,
builder: ((context) {
return AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: vvidText,
decoration: const InputDecoration(
hintText: "Title",
hintStyle: TextStyle(color: Colors.grey)),
),
TextField(
controller: vvidAmount,
decoration: const InputDecoration(
hintText: "Amount",
hintStyle: TextStyle(color: Colors.grey)),
keyboardType: TextInputType.number,
),
],
),
title: Text('My Transaction'),
actions: [
Row(
children: [
TextButton(
onPressed: (() {
Navigator.of(context).pop();
}),
child: Text('Ok')),
TextButton(
onPressed: (() {
Navigator.pop(context, vvidText.text);
}),
child: Text('Close')),
],
),
],
);
}),
);
}
@override
Widget build(BuildContext context) {
return Container();
}
}
2条答案
按热度按时间dgiusagp1#
你可以像这样重写你的
Forma
类:假设这是
onPressed
:您将看到,如果按下关闭按钮,
vvidText
将被传递rbl8hiat2#
你不需要
Forma
类,只需要把dialog方法提取到你的主类中,让它从你的控制器返回值,然后用那个值更新你的卡片:如果你想从两个
textFields
返回值,你甚至可以从showDialog
方法返回List<String>?