我有一个带列表框的小部件,单击某个项目可打开一个用于编辑的窗口。在其中,我可以更改2个字段,均为DateTime类型。
我不明白如何将这些变量从模态窗口返回到主窗口小部件,我将很高兴获得帮助
在模式下,我有两个用于编辑的TextEdit控制器:
TextEditingController _comeInController = TextEditingController();
TextEditingController _goOutController = TextEditingController();
我希望将此变量返回到主窗口小部件并发送到服务器
还有我的模态:
showMaterialModalBottomSheet(
expand: false,
context: context,
builder: (context, scrollController) =>
Container(
height: 400,
child: RedactTimesheeft(
comeIN: DateTime.parse(
staffTimeList.parseComeIn),
goOut: staffTimeList.parseGoOut != null
? DateTime.parse(
staffTimeList.parseGoOut)
: null,
),
),
)
答案 0 :(得分:1)
如果要在ModalBottomSheet中键入文本并在基础窗口小部件中获取文本,只需在两者中使用相同的TextEditingController,即可将其初始化并放置在主窗口小部件中。
这是一个可行的示例:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
main() {
runApp(MaterialApp(
home: Scaffold(
body: MyApp(),
),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
TextEditingController _controller = TextEditingController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
RaisedButton(
onPressed: () => showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (BuildContext context) {
return Material(
child: Padding(
padding: MediaQuery.of(context).viewInsets,
child: TextField(
controller: _controller,
),
),
);
}).whenComplete(() => setState(() {})),
child: Text('Click to enter text'),
),
Text(_controller.text == '' ? 'No text entered yet' : _controller.text),
],
),
);
}
}