首页 > 解决方案 > SetState 中的条件语句未更新小部件

问题描述

我有一个带有一些小部件的列。里面Column有一些TextFields。当textfields接收并失去焦点时,我想隐藏一个小部件。我认为在调用 SetState 时,它​​不会重绘下面的条件。

自动完成文本字段:

child: AutoCompleteTextView(
  suggestionsApiFetchDelay: 300,
  focusGained: () {
    _pauseStream(
        stream: Streams.dropoff);
    _startStream(
        stream: Streams.pickup);
    setState(() {
      _showCurrentLocalTile = true;
    });
  },
  focusLost: () {
    setState(() {
      _showCurrentLocalTile = false;
    });
    _pauseStream(
        stream: Streams.dropoff);
  }
)


void _showBottomSheet() {
    setState(() {
      _showPersBottomSheetCallBack = null;
    });

// Be sure to set to get and set current physical address
    LocationService().getAddressFromLatLng().then((value) {
      if (value != null) {
        _currentAddress = value;
      }
    });

    _scaffoldKey.currentState
        .showBottomSheet((context) {
          return new Container(
            height: 400.0,
            color: Colors.white,
            padding: const EdgeInsets.only(left: 2, right: 2),
            child: new Center(
              // child: new Text('Persistent Bottom Sheet'),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Container(
                    height: 125,
                    child: Row(
                      children: [
                        Expanded(
                          child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisSize: MainAxisSize.max,
                              children: [
                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisSize: MainAxisSize.min,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    IconButton(
                                      icon: Icon(Icons.arrow_back),
                                      color: Colors.black,
                                      onPressed: () {},
                                    ),
                                  ],
                                ),
                                Expanded(
                                  child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      children: [
                                        Container(
                                          alignment: Alignment.topCenter,
                                          height: 35,
                                          margin: const EdgeInsets.only(
                                              top: 40, left: 0, right: 0),
                                          child: AutoCompleteTextView(
                                            suggestionsApiFetchDelay: 300,
                                            focusGained: () {
                                              _pauseStream(
                                                  stream: Streams.dropoff);
                                              _startStream(
                                                  stream: Streams.pickup);
                                              setState(() {
                                                _showCurrentLocalTile = true;
                                              });
                                            },
                                            onTapCallback: (_) async {
                                              _startStream(
                                                  stream: Streams.dropoff);
                                              // locationSavedStatus = LocationSaveStatus.inProgress;
                                              // saveLocationValuesFromGeoCoding(bloc).then(
                                              //   (_) =>
                                              //       locationSavedStatus = LocationSaveStatus.saved,
                                              // );
                                            },
                                            focusLost: () {
                                              setState(() {
                                                _showCurrentLocalTile = false;
                                              });
                                              _pauseStream(
                                                  stream: Streams.dropoff);
                                              // locationSavedStatus = LocationSaveStatus.saved;
                                              // if (startEditingController.text.isEmpty) {
                                              //   city = '';
                                              //   state = '';
                                              //   country = '';
                                              //   startEditingController.text = '';
                                              // } else {
                                              //   startEditingController.text = getLocationString(
                                              //       country: country, state: state, city: city);
                                              // }
                                            },
                                            onValueChanged: (String text) {
                                              // locationSavedStatus = text.isNotEmpty
                                              //     ? (getLocationString(
                                              //                     city: '', state: '', country: '')
                                              //                 .trim() ==
                                              //             text.trim())
                                              //         ? LocationSaveStatus.saved
                                              //         : LocationSaveStatus.notSaved
                                              //     : LocationSaveStatus.saved;
                                            },
                                            controller: startEditingController,
                                            suggestionStyle: Theme.of(context)
                                                .textTheme
                                                .bodyText2,
                                            getSuggestionsMethod:
                                                getLocationSuggestionsList,
                                            tfTextAlign: TextAlign.left,
                                            tfCursorColor: Colors.black,
                                            tfStyle: TextStyle(
                                              fontSize: 16,
                                              color: Theme.of(context)
                                                  .textTheme
                                                  .bodyText2
                                                  .color,
                                            ),
                                            tfTextDecoration: InputDecoration(
                                              contentPadding: EdgeInsets.only(
                                                  top: 0, left: 8.0),
                                              filled: true,
                                              fillColor: Colors.grey[200],
                                              focusedBorder: OutlineInputBorder(
                                                borderSide: BorderSide(
                                                    color: Colors.white,
                                                    width: 1.0),
                                                borderRadius: BorderRadius.zero,
                                              ),
                                              enabledBorder: OutlineInputBorder(
                                                borderSide: BorderSide(
                                                    color: Colors.white,
                                                    width: 1.0),
                                                borderRadius: BorderRadius.zero,
                                              ),
                                              hintText: "",
                                              labelText: 'Start',
                                              labelStyle:
                                                  kPurpleLabelStyle,
                                            ),
                                          ),
                                        ),
                                        Container(
                                          height: 35,
                                          margin: const EdgeInsets.only(
                                              top: 5,
                                              left: 0,
                                              right: 0,
                                              bottom: 5),
                                          child: AutoCompleteTextView(
                                            suggestionsApiFetchDelay: 300,
                                            focusGained: () {
                                              setState(() {
                                                _showCurrentLocalTile = true;
                                              });
                                              _pauseStream(
                                                  stream: Streams.pickup);
                                              _startStream(
                                                  stream: Streams.dropoff);
                                            },
                                            onTapCallback: (_) async {
                                              _startStream(
                                                  stream: Streams.pickup);
                                              // locationSavedStatus = LocationSaveStatus.inProgress;
                                              // saveLocationValuesFromGeoCoding(bloc).then(
                                              //   (_) =>
                                              //       locationSavedStatus = LocationSaveStatus.saved,
                                              // );
                                            },
                                            focusLost: () {
                                              setState(() {
                                                _showCurrentLocalTile = false;
                                              });
                                              _pauseStream(
                                                  stream: Streams.pickup);
                                              // locationSavedStatus = LocationSaveStatus.saved;
                                              // if (startEditingController.text.isEmpty) {
                                              //   city = '';
                                              //   state = '';
                                              //   country = '';
                                              //   startEditingController.text = '';
                                              // } else {
                                              //   startEditingController.text = getLocationString(
                                              //       country: country, state: state, city: city);
                                              // }
                                            },
                                            onValueChanged: (String text) {
                                              // locationSavedStatus = text.isNotEmpty
                                              //     ? (getLocationString(
                                              //                     city: '', state: '', country: '')
                                              //                 .trim() ==
                                              //             text.trim())
                                              //         ? LocationSaveStatus.saved
                                              //         : LocationSaveStatus.notSaved
                                              //     : LocationSaveStatus.saved;
                                            },
                                            controller:
                                                destinationEditingController,
                                            suggestionStyle: Theme.of(context)
                                                .textTheme
                                                .bodyText2,
                                            getSuggestionsMethod:
                                                getLocationSuggestionsList,
                                            tfTextAlign: TextAlign.left,
                                            tfCursorColor: Colors.black,
                                            tfStyle: TextStyle(
                                              fontSize: 16,
                                              color: Theme.of(context)
                                                  .textTheme
                                                  .bodyText2
                                                  .color,
                                            ),
                                            tfTextDecoration: InputDecoration(
                                              contentPadding: EdgeInsets.only(
                                                  top: 0, left: 8.0),
                                              filled: true,
                                              fillColor: Colors.grey[300],
                                              focusedBorder: OutlineInputBorder(
                                                borderSide: BorderSide(
                                                    color: Colors.white,
                                                    width: 1.0),
                                                borderRadius: BorderRadius.zero,
                                              ),
                                              enabledBorder: OutlineInputBorder(
                                                borderSide: BorderSide(
                                                    color: Colors.white,
                                                    width: 1.0),
                                                borderRadius: BorderRadius.zero,
                                              ),
                                              hintText: "",
                                              labelText: 'Destination',
                                              labelStyle:
                                                  kPurpleLabelStyle,
                                            ),
                                          ),
                                        )
                                      ]),
                                ),
                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  mainAxisSize: MainAxisSize.min,
                                  children: [
                                    Stack(
                                        alignment: Alignment.bottomLeft,
                                        children: [
                                          ButtonTheme(
                                            padding: EdgeInsets.symmetric(
                                                vertical: 4.0,
                                                horizontal:
                                                    8.0), //adds padding inside the button
                                            materialTapTargetSize:
                                                MaterialTapTargetSize
                                                    .shrinkWrap, //limits the touch area to the button area
                                            minWidth: 45, //wraps child's width
                                            height: 45, //wraps child's height
                                            child: FlatButton(
                                              color: Colors.white,
                                              textColor: Colors.black,
                                              disabledColor: Colors.grey[100],
                                              disabledTextColor: Colors.white,
                                              padding: EdgeInsets.only(
                                                  top: 10,
                                                  left: 8,
                                                  bottom: 8,
                                                  right: 12),
                                              splashColor: Colors.grey[100],
                                              onPressed: () {
                                                /*...*/
                                              },
                                              child: Text(
                                                "Done",
                                                style:
                                                    TextStyle(fontSize: 14.0),
                                              ),
                                            ),
                                          ),
                                          // IconButton(
                                          //   icon: Icon(Icons.add),
                                          //   color: Colors.black,
                                          //   onPressed: () {},
                                          // ),
                                        ]),
                                  ],
                                ),
                              ]),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 15.0,
                  ),
                  _showCurrentLocalTile
                      ? GestureDetector(
                          onTap: () {
                            _setPickupAddressToAutoComplete(_currentAddress);
                            print(_currentAddress);
                          },
                          child: Container(
                            height: 48,
                            child: Padding(
                              padding: const EdgeInsets.fromLTRB(
                                  16.0, 8.0, 16.0, 0.0),
                              child: Row(
                                children: <Widget>[
                                  Icon(Icons.my_location),
                                  Padding(
                                    padding: EdgeInsets.only(
                                        left: 8.0, top: 0.0, bottom: 0.0),
                                    child: Text("Current Location"),
                                  )
                                ],
                              ),
                            ),
                          ),
                        )
                      : SizedBox(
                          height: 15.0,
                        ),
                  Container(
                    height: 200,
                    child: Padding(
                      padding: const EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 8.0),
                      child: StreamBuilder<List<GoogleAddress>>(
                        stream: pickupStreamController.stream,
                        builder: (context, snapshot) => snapshot.data != null
                            ? ListView.builder(
                                itemCount: snapshot.data.length,
                                itemBuilder: (context, index) {
                                  print('Render $index');
                                  if (snapshot.data != null) {
                                    print("${snapshot.data}");
                                    return _createListItem(
                                        icon: Icons.location_on,
                                        googleplace: snapshot.data[index],
                                        onTap: () {
                                          _setPickupAddressToAutoComplete(
                                              snapshot.data[index].address);
                                          print("${snapshot.data[index]}");
                                        });
                                  } else {}
                                },
                              )
                            : SizedBox(height: 15.0),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          );
        })
        .closed
        .whenComplete(() {
          if (mounted) {
            setState(() {
              _showPersBottomSheetCallBack = _showBottomSheet;
            });
          }
        });
  }

标签: flutterdart

解决方案


推荐阅读