First of all, to conveniently use the value of TextField
, you should use TextEditingController
. To get the value after selecting a datetime, you can return a value from the Dialog
method, then send that value to Stream
Working code (I'm creating a simple UI with simple StreamController
to demonstrate):
// TaskData class
class TaskData {
final StreamController _controller =
StreamController<Map<String, dynamic>>();
Stream<Map<String, dynamic>> get taskStream => _controller.stream;
addTask(String title, String date) {
_controller.add({'title': title, 'date': date});
}
}
// AddTask screen
class AddTaskScreen extends StatefulWidget {
@override
_AddTaskScreenState createState() => _AddTaskScreenState();
}
class _AddTaskScreenState extends State<AddTaskScreen> {
final _textController = TextEditingController();
DateFormat dateFormat = DateFormat("yyyy-MM-dd HH:mm:ss");
String chosenDate;
Future<String> _showDatePicker(ctx) async {
var formatedDate = DateTime.now();
return showCupertinoModalPopup(
context: ctx,
builder: (_) => Container(
height: 500,
color: Color.fromARGB(255, 255, 255, 255),
child: Column(
children: [
Container(
height: 400,
child: CupertinoDatePicker(
initialDateTime: DateTime.now(),
onDateTimeChanged: (val) {
formatedDate = val;
}),
),
// Close the modal
CupertinoButton(
child: Text('OK'),
// Return the formatted date here
onPressed: () => Navigator.of(ctx)
.pop(dateFormat.format(formatedDate)),
)
],
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
// ... other lines
child: Column(
// ... other lines
TextField(
// Use the _textController here
controller: _textController,
decoration: InputDecoration(
border: InputBorder.none,
),
style: TextStyle(color: Colors.white),
autofocus: true,
textAlign: TextAlign.center,
),
Container(
child: CupertinoButton(
padding: EdgeInsetsDirectional.zero,
child: Text('Select a date'),
onPressed: () async {
// Wait for the return value
chosenDate = await _showDatePicker(context);
},
),
),
FlatButton(
child: Text(
'Add',
style: TextStyle(
color: Colors.white,
),
),
color: Colors.purple.shade400,
onPressed: () {
Provider.of<TaskData>(context, listen: false)
.addTask(_textController.text, chosenDate);
Navigator.pop(context);
},
),
],
),
),
),
);
}
}
// Main UI Screen
class SomeScreen extends StatefulWidget {
@override
_SomeScreenState createState() => _SomeScreenState();
}
class _SomeScreenState extends State<SomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlatButton(
onPressed: () async {
await showDialog(
context: context,
builder: (context) => AddTaskScreen());
setState(() {});
},
child: Text('Add Task')),
Container(
child: StreamBuilder<Map<String, dynamic>>(
initialData: {},
stream: Provider.of<TaskData>(context).taskStream,
builder: (context, snapshot) {
if (!snapshot.hasData) return Container();
return Column(
children: [
Text(
'${snapshot.data['title']} | ${snapshot.data['date']}'),
],
);
},
)),
],
),
),
),
);
}
}