entronad / flutter_echarts

A Flutter widget to use Apache ECharts in a reactive way.

Home Page:https://pub.dev/packages/flutter_echarts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

最简Demo首次图表可以加载成功,但是之后重新再重新加载就一直不显示图表

KevinSkye1314 opened this issue · comments

你好,我在android运行了一个最简demo例子,首次图表能渲染出来,但是我退出debug再重启项目图标就无法再加载了,目前我使用的版本是2.4.0,

最后自己捣腾了一下,还是将图表给加载了出来,以下是具体的实现代码
void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;

@OverRide
State createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
int _counter = 0;

//申明的一个状态变量
var isReload=true;

void _incrementCounter() {
setState(() {
_counter++;
});
}

var chartDta="";

@OverRide
void initState() {
// TODO: implement initState
super.initState();
chartDta='''
{
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line'
}]
}
''';
}

@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(

      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        const Text(
          'You have pushed the button this many times:',
        ),
        Text(
          '$_counter',
          style: Theme.of(context).textTheme.headline4,
        ),
        Container(
          child: Echarts(
          option: chartDta,
          onLoad: (p0) {//通过状态值每次进行build的时候进行一次重载
            if(isReload){
              p0.reload();
              isReload=false;
            }
          },
          onMessage: (message) {
          },
          ),
          width: 300,
          height: 250,
        )
      ],
    ),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: const Icon(Icons.add),
  ), // This trailing comma makes auto-formatting nicer for build methods.
);

}
}

由于自己是新手,不知道这种写法对性能的是否有影响,也是仅供新入手的小伙伴参考,再次感谢提供控件的大佬