I have the following classes:
AddFormBloc
class AddFormBloc extends FormBloc<String,String>{
// ignore: close_sinks
final trackField = TextFieldBloc(
asyncValidatorDebounceTime: Duration(milliseconds: 500),
);
final latField = TextFieldBloc();
final longField = TextFieldBloc();
AddFormBloc(){
trackField.addAsyncValidators([_isValidTrack]);
addFieldBlocs(fieldBlocs: [
trackField,
latField,
longField
]);
}
...
@override
Stream<FormBlocState<String, String>> onSubmitting() async* {
try{
print(trackField.value);
await Future<void>.delayed(Duration(seconds:2));
yield state.toSuccess();
}catch(e){
yield state.toFailure(
failureResponse: 'Fake error, please continue testing the async validation.'
);
}
}
}
FormAddSong
class FormAddSong extends StatefulWidget {
@override
_FormAddSongState createState() => _FormAddSongState();
}
class _FormAddSongState extends State<FormAddSong> {
AddFormBloc _addFormBloc;
Completer<GoogleMapController> _controller = Completer();
double _lat; //latitud
double _lng; //longitud
Set<Marker> _markers = Set();
@override
void initState() {
super.initState();
_addFormBloc = AddFormBloc();
}
@override
void dispose() {
_addFormBloc.delete();
super.dispose();
}
@override
Widget build(BuildContext context) {
String pattern = "https://open.spotify.com/track/";
String patternIdTrack="4QupQwSViJ7sdZWZOkzZV5";
return Container(
child: FormBlocListener(
formBloc: _addFormBloc,
onSubmitting: (context, state){
showDialog(
context: context,
barrierDismissible: false,
builder: (_) => WillPopScope(
onWillPop: () async => false,
child: Center(
child: Card(
child: Container(
width: 80,
height: 80,
padding: EdgeInsets.all(12.0),
child: CircularProgressIndicator(),
),
),
),
),
);
},
onSuccess: (context, state){
Navigator.of(context).pop();
showDialog(context: context, builder: (_) => AlertDialog(
title: Text("se ha agregado correctamente"),
));
},
onFailure: (context, state){
// Hide the progress dialog
Navigator.of(context).pop();
// Show snackbar with the error
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(state.failureResponse),
backgroundColor: Colors.red[300],
),
);
},
child: Column(
children: [
TextFieldBlocBuilder(
maxLength: patternIdTrack.length+pattern.length,
textFieldBloc: _addFormBloc.trackField,
decoration: InputDecoration(
labelText: 'Enlace canción en Spotify',
hintText: 'Enlace canción en Spotify',
prefixIcon: Icon(Icons.music_note),
suffixIcon: IconButton(
icon: Icon(Icons.info_outline),
onPressed: (){
Navigator.push(context,
MaterialPageRoute(builder: (context) => OnBoardingGuideSpotifyLinkPage()));
},
),
),
),
TextFieldBlocBuilder(
isEnabled: false,
textFieldBloc: _addFormBloc.latField,
decoration: InputDecoration(
labelText: 'Latitud (Clic mapa)',
hintText: 'Latitud (Clic mapa)',
prefixIcon: Icon(Icons.edit_location)
),
),
TextFieldBlocBuilder(
readOnly: true,
textFieldBloc: _addFormBloc.longField,
decoration: InputDecoration(
labelText: 'Longitud (Clic mapa)',
hintText: 'Longitud (Clic mapa)',
prefixIcon: Icon(Icons.edit_location)
),
),
SizedBox(height: MediaQuery.of(context).devicePixelRatio*10.0),
FutureBuilder(
future: _initialCameraP(),
builder: (BuildContext context, AsyncSnapshot<Tuple2<Position,Uint8List>>snapshot){
if(snapshot.hasData){
Position ps = snapshot.data.item1;
return GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(ps.latitude,ps.longitude),
zoom: (ps.latitude==0.0 && ps.longitude == 0.0)?2.0:10.0
),
markers: _markers,
onMapCreated: (GoogleMapController controller){
_lat = ps.latitude;
_lng = ps.longitude;
_controller.complete(controller);
},
onTap: (LatLng latLng){
setState(() {
_markers.clear(); //limpiar lista para que solo aparezca sobre donde hemos clicado
//a?adir donde hemos clicado
_markers.add(Marker(
markerId: MarkerId(latLng.toString()),
icon: BitmapDescriptor.fromBytes(snapshot.data.item2),
position: latLng,
));
});
},
);
}else{
return CircularProgressIndicator();
}
},
),
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: _addFormBloc.submit,
child: Center(child: Text('Enviar')),
),
),
],
),
),
);
}
What I am trying to do is that when a user tap on the GoogleMap, I automatically save the latitude and longitude in some variables and insert those values in the corresponding latitude and longitude textfieldblocbuilder.
Without Bloc, I know how to do it in normal forms, but with Bloc I can't find the way.
question from:
https://stackoverflow.com/questions/65650199/how-to-modify-the-value-of-another-textfieldblocbuilder-programmatically