Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

android studio - How to modify the value of another TextFieldBlocBuilder programmatically?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...