Flutter ListView.builder无法通过setState动态更新

时间:2020-09-24 19:08:05

标签: flutter listview dart

我有应该每1秒更新一次Listview.builder的代码作为测试,但是我没有动态更新,因此我不得不切换选项卡以更新列表。我有一个带状态的小部件,带有setState来更新UI,但似乎无法正常工作。我迷失了自己在做错的事情。任何帮助表示赞赏!

List<Color> colorsLogs = List<Color>();
List<String> titlesLogs = List<String>();
List<String> contentsLogs = List<String>();

class LogsTab extends StatefulWidget {
  static const title = 'Logs';
  static const iosIcon = Icon(Icons.assignment);

  void AddLog(Color color, String title, String contents){
    if(!(!switch1 && title.contains(new RegExp('debug', caseSensitive: false)))){
      colorsLogs.add(color);
      titlesLogs.add(title);
      contentsLogs.add(contents);
    }
  }
  @override
  _LogsTabState createState() => _LogsTabState();
}

class _LogsTabState extends State<LogsTab> {

  void clearLogs(){
    colorsLogs.clear();
    titlesLogs.clear();
    contentsLogs.clear();
  }
  Widget _listBuilder(BuildContext context, int index) {
    if (index >= contentsLogs.length) return null;
    return SafeArea(
      top: false,
      bottom: false,
      child: Card(
        elevation: 1.5,
        margin: EdgeInsets.fromLTRB(6, 12, 6, 0),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(4),
        ),
        child: InkWell(
          onTap: defaultTargetPlatform == TargetPlatform.iOS ? null : () {},
          child: Padding(
            padding: const EdgeInsets.all(12.0),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CircleAvatar(
                  backgroundColor: colorsLogs[index],
                  child: Text(
                    titlesLogs[index].substring(0, 1),
                    style: TextStyle(color: Colors.white),
                  ),
                ),
                Padding(padding: EdgeInsets.only(left: 16)),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        titlesLogs[index],
                        style: TextStyle(
                          fontSize: 15,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      Padding(padding: EdgeInsets.only(top: 8)),
                      Text(
                        contentsLogs[index],
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildIos(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
          trailing: CupertinoButton(
            padding: EdgeInsets.zero,
            child: Text('Clear logs'),
            onPressed: (){  new Timer.periodic(const Duration(seconds: 1) , (Timer t) => logstab.AddLog(Colors.red, 'testing', 'testing ues'));
            setState(() {

            });
            }
          ),
      ),
      child: ListView.builder(
        shrinkWrap: true,
        reverse: true,
        itemBuilder: _listBuilder,
      ),
    );
  }

  @override
  Widget build(context) {
    return PlatformWidget(
      iosBuilder: _buildIos,
    );
  }
}


0 个答案:

没有答案