Skip to content

Commit

Permalink
fix(*): can't refresh echartview
Browse files Browse the repository at this point in the history
  • Loading branch information
furui committed Dec 24, 2020
1 parent c9fcf44 commit 0a6ce1a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 61 deletions.
2 changes: 1 addition & 1 deletion example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_echart","path":"/Users/furui/workspace/flutter_echart/","dependencies":[]}],"android":[{"name":"flutter_echart","path":"/Users/furui/workspace/flutter_echart/","dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"flutter_echart","dependencies":[]}],"date_created":"2020-12-24 16:23:28.308829","version":"1.25.0-8.1.pre"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_echart","path":"/Users/furui/workspace/flutter_echart/","dependencies":[]}],"android":[{"name":"flutter_echart","path":"/Users/furui/workspace/flutter_echart/","dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"flutter_echart","dependencies":[]}],"date_created":"2020-12-24 18:05:07.451216","version":"1.25.0-8.1.pre"}
119 changes: 77 additions & 42 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_echart/flutter_echart.dart';
import 'package:flutter_echart/echart_provider.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyApp());

Expand All @@ -12,62 +14,95 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
CounterProvider _counterProvider = new CounterProvider();

var data1;
var option = {
"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
void initState() {
// TODO: implement initState
super.initState();
_counterProvider.refresh(UniqueKey(), option);
}

@override
Widget build(BuildContext context) {
var option = {
"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"
}
]
};
var option2 = {
"xAxis": {
"type": "category",
"data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
"tooltip": {"trigger": 'item', "formatter": '{a} <br/>{b}: {c} ({d}%)'},
"legend": {
"orient": 'vertical',
"left": 10,
"data": ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
},
"yAxis": {"type": "value"},
"series": [
{
"data": [820, 932, 901, 934, 1290, 1330, 1320],
"type": "line"
"name": '访问来源',
"type": 'pie',
"radius": ['50%', '70%'],
"avoidLabelOverlap": false,
"label": {"show": false, "position": 'center'},
"emphasis": {
"label": {"show": true, "fontSize": '30', "fontWeight": 'bold'}
},
"labelLine": {"show": false},
"data": [
{"value": 335, "name": '直接访问'},
{"value": 310, "name": '邮件营销'},
{"value": 234, "name": '联盟广告'},
{"value": 135, 'name': '视频广告'},
{"value": 1548, "name": '搜索引擎'}
]
}
]
};
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Native WebView as Widget'),
),
body: new SingleChildScrollView(
child: new Column(
children: <Widget>[
new Text('Native WebView as Widget\n\n'),
new Container(
child: EchartView(height: 300, data: option),
height: 300.0,
width: 500.0,
),
new Text('Native WebView as Widget 2\n\n'),
new Container(
child: EchartView(height: 300, data: option2),
height: 300.0,
width: 500.0,

return ChangeNotifierProvider(
builder: (context) => _counterProvider,
child: new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Native WebView as Widget'),
),
body: new SingleChildScrollView(
child: new Column(
children: <Widget>[
new Text('Native WebView as Widget\n\n'),
Consumer(builder: (BuildContext context,
CounterProvider counterProvider, Widget child) {
print('EchartView。。。。。。');
return new Container(
child: EchartView(
key: _counterProvider.keyCount,
height: 300,
data: counterProvider.value),
height: 300.0,
width: 500.0,
);
}),
Builder(builder: (context) {
return RaisedButton(
child: Text("更改数据"),
onPressed: () {
_counterProvider.refresh(UniqueKey(), option2);
},
);
}),
],
),
],
),
)),
);
)),
));
}
}
16 changes: 16 additions & 0 deletions lib/echart_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:flutter/material.dart';

class CounterProvider with ChangeNotifier {
Key key;
Map data;

Map get value => data;

Key get keyCount => key;

void refresh(Key key, Map data) {
this.key = key;
this.data = data;
notifyListeners();
}
}
36 changes: 18 additions & 18 deletions lib/flutter_echart.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import 'dart:async';

import 'package:flutter/services.dart';

import 'dart:convert';
import 'package:flutter/material.dart';
import 'flutter_native_web.dart';

//Echarts
class EchartView extends StatelessWidget {
class EchartView extends StatefulWidget {
EchartView({Key key, this.height, this.data, this.child}) : super(key: key);
final Map data;
final double height;
final Widget child;

@override
_EchartViewState createState() => _EchartViewState();
}

class _EchartViewState extends State<EchartView> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Container(
height: height,
height: widget.height,
color: Colors.white,
child: Echarts(data: data, child: child),
child: Echarts(data: widget.data, child: widget.child),
);
}
}

class Echarts extends StatefulWidget {
Echarts({this.data, this.child});
Echarts({Key key, this.data, this.child}) : super(key: key);

final Widget child;

Expand All @@ -34,7 +39,6 @@ class Echarts extends StatefulWidget {
}

class _EchartsState extends State<Echarts> {

WebController webController;

bool finished = false;
Expand Down Expand Up @@ -67,16 +71,12 @@ class _EchartsState extends State<Echarts> {

@override
Widget build(BuildContext context) {
return Stack(
overflow: Overflow.clip,
children: <Widget>[
widget.child ??
const Center(child: const CircularProgressIndicator()),
AnimatedOpacity(
duration: Duration(milliseconds: 300),
opacity: finished ? 1.0 : 0.0,
child: FlutterNativeWeb(
onWebCreated: onWebCreated))
]);
return Stack(overflow: Overflow.clip, children: <Widget>[
widget.child ?? const Center(child: const CircularProgressIndicator()),
AnimatedOpacity(
duration: Duration(milliseconds: 300),
opacity: finished ? 1.0 : 0.0,
child: FlutterNativeWeb(onWebCreated: onWebCreated))
]);
}
}
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependencies:
flutter:
sdk: flutter

provider: ^2.0.1+1

# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

Expand Down

0 comments on commit 0a6ce1a

Please sign in to comment.