Flutter错误:第二次弹出上下文后无法将数据返回到第一个屏幕(第一次起作用)

时间:2019-07-29 15:14:45

标签: flutter flutter-layout flutter-navigation

Initially, this is how it looks like.

我正在尝试使用// HttpPost Create action method of SubCategoroyController await _db.SaveChangesAsync(); TempData["StatusMessage"] = "Success"; // <==== Modified return RedirectToAction(nameof(Index)); (使用ListView()周围的GestureDetector列表)制作一个简单的电话号码/国家/地区代码选择页面,以及何时我尝试从Row返回数据(当我点击国家/地区代码和电话号码时,它会响应),第一次运行良好。

This is the ListView that contains all the GestureDetectors wrapped around rows

但是,第二次声明我是无效的:“试图使用停用的小部件”

This is how it should work, i.e I tapped on 'AL' and that updated the first screen on popping. However, it doesn't work if I try it again in this session

我是Flutter的新手,在发布之前,我在这里阅读了很多答案,但是没有一个帮助。

我尝试在第二个屏幕上添加和删除GestureDetector(),但仍然无法理解我做错了什么。

注意:如果我不使用initState()循环制作小部件,则此方法有效。但是,这不切实际,因为有230个国家/地区。

第一个屏幕的摘要:

for

第二屏(不包括导入):

class SelectionGDetector extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        _navigateAndDisplaySelection(context);
      },
      child: Row(
        children: <Widget>[
          Expanded(
            flex: 2,
            child: Container(
              child: Image.asset(
                'icons/flags/png/${twoLetterCountryCode.toLowerCase()}.png',
                package: 'country_icons',
                height: 20,
                width: 30,
                fit: BoxFit.fill,
              ),
            ),
          ),
          SizedBox(
            width: 7,
          ),
          Expanded(
            child: Icon(
              FontAwesomeIcons.chevronDown,
              size: 8,
              color: Colors.black54,
            ),
          ),
        ],
      ),
    );
  }

  _navigateAndDisplaySelection(BuildContext context) async {
    // Navigator.push returns a Future that completes after calling
    // Navigator.pop on the Selection Screen.
    final result = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => NewCountryList()),
    );
    MobileAuthScreen.updateUI(result);
  }
}

最后,我希望我的第二个屏幕(List<Widget> listViewItems = []; //list of Gesture Detectors that contain Rows class NewCountryList extends StatefulWidget { static const id = 'newCountryList'; @override _NewCountryListState createState() => _NewCountryListState(); } loop(context) { for (String countryCode in country.keys) { //looping through a map which has country codes as keys and phone codes //as values String countryCodeLowerCase = countryCode.toLowerCase(); String phoneCode = country[countryCode]; var item = Padding( padding: EdgeInsets.symmetric(vertical: 18, horizontal: 13), child: GestureDetector( onTap: () { Navigator.pop(context, countryCode); }, child: Row( children: <Widget>[ Container( child: Image.asset( 'icons/flags/png/$countryCodeLowerCase.png', package: 'country_icons', height: 20, width: 30, fit: BoxFit.fill, ), ), SizedBox( width: 8, ), Text( '$countryCode (+$phoneCode)', style: TextStyle( fontFamily: 'Montserrat', fontWeight: FontWeight.w500, ), ), ], ), ), ); listViewItems.add(item); } } class _NewCountryListState extends State<NewCountryList> { ListView listViewLoopMethod(context) { loop(context); return ListView( children: listViewItems, ); } @override void initState() { // TODO: implement initState super.initState(); loop(context); } @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: listViewLoopMethod(context), ), ); } } )始终将值返回到我的第一个屏幕,但这只是第一次。

0 个答案:

没有答案