This repository has been archived by the owner on May 19, 2019. It is now read-only.
Getting error with date picker fields value not set. #1
Labels
question
Further information is requested
Hi,
Please check the code. I'm getting an error with the date picker. After setting the date in text field it's not validating or getting the value after submit the form.
bloc.dart file
`
import 'package:frideos_core/frideos_core.dart';
class DynamicFieldsBloc {
DynamicFieldsBloc() {
print('-------DynamicFields BLOC--------');
}
// A StreamedList holds a list of StreamedValue of type String so
// it is possibile to add more items.
final nameFields = StreamedList<StreamedValue>(initialData: []);
final dateFields = StreamedList<StreamedValue>(initialData: []);
// This StreamedValue is used to handle the current validation state
// of the form.
final isFormValid = StreamedValue();
// Every time the user clicks on the "New fields" button, this method
// adds two new fields and sets the checkForm method to be called
// every time these new fields change.
void newFields() {
nameFields.addElement(StreamedValue());
dateFields.addElement(StreamedValue());
}
void checkForm(String _) {
// These two boolean flags will be used to determine whether each group of fields
// (name or age) is valid or not.
bool isValidFieldsTypeName = true;
bool isValidFieldsTypeDate = true;
}
void submit() {
for(var i=0; i<dateFields.length;i++){
print('dates: '+dateFields.value[i].value.toString());
}
}
void removeFields(int index) {
nameFields.removeAt(index);
dateFields.removeAt(index);
}
void dispose() {
print('-------DynamicFields BLOC DISPOSE--------');
}
}
final bloc = DynamicFieldsBloc();
`
dynamicFields.dart file
`
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:frideos_core/frideos_core.dart';
import 'package:frideos/frideos.dart';
import 'package:date_format/date_format.dart';
import '../bloc/bloc.dart';
const TextStyle buttonText = TextStyle(color: Colors.white);
class DynamicFieldsPage extends StatefulWidget {
@OverRide
_DynamicFieldsPageState createState() => _DynamicFieldsPageState();
}
class _DynamicFieldsPageState extends State {
@OverRide
void dispose() {
bloc.dispose();
super.dispose();
}
@OverRide
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: const Text('Dynamic fields validation'),
),
body: DynamicFieldsWidget(),
),
);
}
}
class DynamicFieldsWidget extends StatefulWidget {
@OverRide
_DynamicFieldsWidgetState createState() => _DynamicFieldsWidgetState();
}
class _DynamicFieldsWidgetState extends State {
final nameFieldsController = List();
var dateFieldsController = List();
@OverRide
Widget build(BuildContext context) {
List _buildFields(int length) {
// Clear the TextEditingControllers lists
nameFieldsController.clear();
dateFieldsController.clear();
}
}
class FieldsWidget extends StatefulWidget {
const FieldsWidget({
this.index,
this.nameController,
this.dateController,
});
final int index;
final TextEditingController nameController;
final TextEditingController dateController;
@OverRide
_FieldsWidgetState createState() => _FieldsWidgetState();
}
class _FieldsWidgetState extends State {
dynamic selectedDate = '';
_selectDate(BuildContext context) async {
final DateTime pickedStartDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2015, 8),
lastDate: DateTime(2022));
if (pickedStartDate != null)
setState(() {
selectedDate = formatDate(pickedStartDate, [dd, ' ', M, ' ', yyyy]).toString();
widget.dateController.text = selectedDate;
});
}
@OverRide
void initState() {
super.initState();
}
@OverRide
Widget build(BuildContext context) {
return Stack(
children: [
_buildFormFields(context),
],
);
}
Widget _buildFormFields(context){
return Row(
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: Text('${widget.index + 1}:')),
Expanded(
child: Column(
children: [
StreamBuilder(
initialData: ' ',
stream: bloc.nameFields.value[widget.index].outStream,
builder: (context, snapshot) {
return Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 10,
),
child: TextField(
controller: widget.nameController,
style: const TextStyle(
fontSize: 14,
color: Colors.black,
),
decoration: InputDecoration(
labelText: 'Name:',
hintText: 'Insert a name...',
errorText: snapshot.error,
),
onChanged: bloc.nameFields.value[widget.index].inStream,
),
),
],
);
}),
StreamBuilder(
initialData: ' ',
stream: bloc.dateFields.value[widget.index].outStream,
builder: (context, snapshot) {
return Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 10,
),
child: TextField(
onTap: ()=> _selectDate(context),
controller: widget.dateController,
style: const TextStyle(
fontSize: 14,
color: Colors.black,
),
decoration: InputDecoration(
labelText: 'Date:',
hintText: 'Add Date',
errorText: snapshot.error,
),
keyboardType: TextInputType.datetime,
onChanged: bloc.dateFields.value[widget.index].inStream,
),
),
],
);
}),
const SizedBox(
height: 22.0,
),
],
),
),
IconButton(
icon: const Icon(Icons.delete),
color: Colors.red,
onPressed: () => bloc.removeFields(widget.index),
),
],
);
}
}
`
main.dart file
`
import 'package:flutter/material.dart';
import '../ui/dynamic_fields.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
final platform = Theme.of(context).platform;
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DynamicFieldsPage(),
);
}
}
`
The text was updated successfully, but these errors were encountered: