简体   繁体   English

滚动时小部件闪烁并消失

[英]Widget flickers and disappears when scrolling

I'm already losing sleep over this.我已经为此失眠了。

I'm trying to display a chart inside a ListView (for scrolling).我正在尝试在ListView中显示图表(用于滚动)。 For some reason the contents of the Card flickers when scrolling and randomly completely disappears (the Card itself stays visible though).由于某种原因, Card的内容在滚动时会闪烁并随机完全消失(但Card本身仍然可见)。

Any idea why would that happen?知道为什么会这样吗?

(...) ListView (...)
children: [Row ( children: [buildChartBox()] )] (...)


Expanded buildChartBox() {
   return Expanded(
     child: Card(
       child: Padding(
         padding: const EdgeInsets.all(20.0),
         child: Column(
           children: [
             Column(
               mainAxisSize: MainAxisSize.min,
               children: [
                 chartTitles(
                     title: 'Items',
                     subtitle: 'by value'),
                 SizedBox(
                     height: 300,
                     child: ValuesChart(data: calculateValues(items)))
               ],
             ),
           ],
         ),
       ),
     ),
   );
 }




Row chartTitles({String title = '', String subtitle = ''}) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(title, style: text_charttitle),
            Text(subtitle, style: text_chartsubtitle),
          ],
        )
      ],
    );
  }
  

Things tried:尝试的事情:

  • Both of these were originally Stateless Widgets;这两个最初都是无状态的小部件; I changed to simple methods to simplify but it didn't change the weird behaviour.我改用简单的方法来简化,但它并没有改变奇怪的行为。
  • Replacing the chartTitles return with an empty Container (ie removing the titles) does mitigate the issue.chartTitles return替换为空Container (即删除标题)确实可以缓解问题。 The chart then stays displayed but also flickers slightly .然后图表会保持显示状态,但也会轻微闪烁。
  • Replacing the ListView with a SingleChildScrollView doesn't change anything.SingleChildScrollView替换ListView不会改变任何东西。

EDIT: Code for the ValuesChart:编辑:ValuesChart 的代码:

import 'package:fl_chart/fl_chart.dart';

class ValuesChart extends StatelessWidget {
  final Map<String, int> data;

  const ValuesChart({required this.data});

  @override
  Widget build(BuildContext context) {
    return Container(
        child: PieChart(
      _theData(data),
    ));
  }
}

Note I'm using a package called 'fl_chart'.请注意,我使用的是名为“fl_chart”的 package。 _theData just returns various parameters for the chart, I don't think it's relevant. _theData只是返回图表的各种参数,我认为这无关紧要。

Try to replace ListView with SingleChildScrollView尝试用SingleChildScrollView替换ListView

ListView s in flutter by default using what it is called in Android RecyclerView to efficiently use render resources. flutter 中的ListView默认使用它在 Android RecyclerView中调用的内容来有效地使用渲染资源。 If you are interested here an article https://medium.com/1mgofficial/how-recyclerview-works-internally-71290de5d2c4如果您对这里感兴趣的文章https://medium.com/1mgofficial/how-recyclerview-works-internally-71290de5d2c4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM