【问题标题】:Flutter - setState on a List of Widget (inner) not working or updating the main Widget statusFlutter - 小部件列表(内部)上的 setState 不工作或更新主小部件状态
【发布时间】:2021-12-14 13:13:44
【问题描述】:

我有一个 Widget 列表,其中包含 2 个按钮的列表和一个文本 ..

并且有一个包含每个计数器的列表,因此当我按下按钮时,它将向计数器添加 +1,并在 onPressed 设置状态后显示新值!

问题在于它没有改变 Widget 的状态!

例子:

更清楚:

位于 Widget 列表中的 setState 不起作用!

我正在使用这个 void 来构建小部件列表的每个项目:

  void constractor(int targetindex) async {
_bonestoiteams.add(0);
_iteamswigitcall.add(ListTile(
  trailing: Row(
    mainAxisSize: MainAxisSize.min,
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    crossAxisAlignment: CrossAxisAlignment.center,
    children: [
      IconButton(
        onPressed: () {
          return setState(() {
            _bonestoiteams[targetindex] = _bonestoiteams[targetindex] + 1;
          });
        },
        icon: Icon(Icons.add),
        color: Colors.green,
      ),
      Text(
        _bonestoiteams[targetindex].toString(),
        textAlign: TextAlign.start,
        textDirection: TextDirection.rtl,
        style: TextStyle(
            fontSize: 14, color: Colors.black, fontWeight: FontWeight.bold),
      ),
      IconButton(
        onPressed: () => setState(() {
          _bonestoiteams[targetindex] = _bonestoiteams[targetindex] + 1;
          print(_bonestoiteams[targetindex]);
        }),
        icon: Icon(Icons.remove),
        color: Colors.red,
      ),
    ],
  ),
  leading: LocalData.iteams_img_1[LocalData.iteams_trade_name
              .indexOf(_silectediteams[targetindex])] ==
          ''
      ? Image(
          fit: BoxFit.cover,
          image: AssetImage('assets/img/medication.png'),
          height: 60,
          width: 60,
        )
      : Image(
          fit: BoxFit.cover,
          image: FileImage(File(LocalData.iteams_img_1[LocalData
              .iteams_trade_name
              .indexOf(_silectediteams[targetindex])])),
          height: 60,
          width: 60,
        ),
  title: Text(
    _silectediteams[targetindex].toString(),
    textAlign: TextAlign.start,
        style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
      ),
    ));
  }

这行不行:

          IconButton(
        onPressed: () {
          return setState(() {
            _bonestoiteams[targetindex] = _bonestoiteams[targetindex] + 1;
          });
        },
        icon: Icon(Icons.add),
        color: Colors.green,
      ),

确实加了+1!但它没有更新它的目录我应该重新 Bulde 或做一些事情来显示新的价值!

鬃毛类是:

      @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onWillPop,
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.blue[500],
          title: Text("أضافة زيارة جديدة"),
          centerTitle: true,
        ),
        body: SingleChildScrollView(
            child: Container(
          margin: EdgeInsets.all(24),
          child: SingleChildScrollView(
            child: Form(
                key: formKey,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    ductorlistnewserch(),
                    const SizedBox(
                      height: 13,
                    ),
                    ductorlistpharmacy(),
                    const SizedBox(
                      height: 13,
                    ),
                    iteamsselectiedlist(),
                    const SizedBox(
                      height: 13,
                    ),
                    _buldenamefield1(),
                    const SizedBox(
                      height: 13,
                    ),
                    _buldenamefield2(),
                    const SizedBox(
                      height: 13,
                    ),
                    Column(
                      children: _iteamswigitcall,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Padding(
                          child: buildSubmit(),
                          padding: const EdgeInsets.all(8.0),
                        ),
                        buildCancil(),
                        const SizedBox(
                          height: 10,
                        ),
                      ],
                    )
                  ],
                )),
          ),
        )),
      ),
    );
  }

完整代码:

import 'dart:io';

import 'package:dropdown_search/dropdown_search.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:smart_bureau/components/LangData.dart';
import 'package:smart_bureau/components/LocalData.dart';

class AddVisist extends StatefulWidget {
  @override
  _AdVisit createState() => _AdVisit();
}

class _AdVisit extends State<AddVisist> {
  @override
  void initState() {
    super.initState();
  }

  late String _ductorname;
  late String _ductorstatus;
  late String _clenicstatus;
  late String _relatedpharmacy;

  late List<String> _ductorrelatedpharmacy = [];
  late List<String> _ductorlist = LocalData.doctor_name;
  late List<String> _silectediteams = [];
  late List<Widget> _iteamswigitcall = [];
  late List<int> _bonestoiteams = [];

  final controler1 = TextEditingController();
  final controler2 = TextEditingController();
  final controler3 = TextEditingController();

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

.....

无法将所有鬃毛类 do 添加到 Max(30034)

如果有什么不清楚的地方,我可以在表扬中解释更多...

编辑:

尝试使用 ListView.builder 构建 itteams 但同样的问题!!

      `ListView.builder(
physics: NeverScrollableScrollPhysics(),  
shrinkWrap: true, 
primary: false, 
itemCount: _silectediteams.length, itemBuilder: (context, index) { return _iteamswigitcall[index]; 
}),`

【问题讨论】:

  • 请编辑问题本身,而不是在 cmets 中编写编辑
  • 完成✔@Anan Saadi

标签: flutter dart


【解决方案1】:

请尝试下面的代码,它应该可以工作

IconButton(
    onPressed: () {
      _bonestoiteams[targetindex] = _bonestoiteams[targetindex]++;
      setState(() {});
    },
    icon: Icon(Icons.add),
    color: Colors.green)

【讨论】:

  • 同样的问题! .. 我还添加了 print(_bonestoiteams[targetindex] = _bonestoiteams[targetindex]++); ..它正在打印但没有改变状态!同样的问题
猜你喜欢
  • 2018-07-28
  • 2020-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 2020-10-20
  • 1970-01-01
相关资源
最近更新 更多