浏览代码

streambuilder, futurebuilder demo

/main
siyao 3 年前
当前提交
ea4bc085
共有 2 个文件被更改,包括 70 次插入34 次删除
  1. 102
      Samples/UIWidgetsSamples_2019_4/Assets/Script/StreamTest.cs
  2. 2
      com.unity.uiwidgets/Runtime/widgets/async.cs

102
Samples/UIWidgetsSamples_2019_4/Assets/Script/StreamTest.cs


using System;
using System.Collections;
using System.Timers;
using Unity.UIWidgets.animation;
using uiwidgets;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.ui;
using FontStyle = Unity.UIWidgets.ui.FontStyle;
using Image = Unity.UIWidgets.widgets.Image;
using TextStyle = Unity.UIWidgets.painting.TextStyle;
using UnityEngine;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.ui;
using UnityEngine.Networking;
using Image = Unity.UIWidgets.widgets.Image;
using Timer = Unity.UIWidgets.async.Timer;
namespace UIWidgetsSample

class ExampleState : State<ExampleApp>
{
int counter;
IEnumerator _loadCoroutine(string key, Completer completer, Isolate isolate) {
var url = new Uri(key);
using (var www = UnityWebRequest.Get(url)) {
yield return www.SendWebRequest();
using (Isolate.getScope(isolate)) {
if (www.isNetworkError || www.isHttpError) {
completer.completeError(new Exception($"Failed to load from url \"{url}\": {www.error}"));
yield break;
}
var data = www.downloadHandler.data;
completer.complete(data);
}
}
}
myStream.listen(d => { Debug.Log($"count: {d}"); });
Future<byte[]> f = null;
var completer = Completer.create();
var isolate = Isolate.current;
var panel = UIWidgetsPanelWrapper.current.window;
if (panel.isActive()) {
panel.startCoroutine(_loadCoroutine("https://buljan.rcsdk8.org/sites/main/files/main-images/camera_lense_0.jpeg", completer, isolate));
f = completer.future.to<byte[]>().then_<byte[]>(data => {
if (data != null && data.Length > 0) {
return data;
}
throw new Exception("not loaded");
});
}
var futureBuilder = new FutureBuilder<byte[]>(
future: f,
builder: (ctx, snapshot) =>
{
int width = 200;
int height = 200;
Color color = Colors.blue;
if (snapshot.connectionState == ConnectionState.done)
{
return new Container(alignment: Alignment.center, width: width, height:height, color: color, child: Image.memory(snapshot.data) );
} else if (snapshot.connectionState == ConnectionState.waiting)
{
return new Container(alignment: Alignment.center, width: width, height:height, color: color, child: new Text("waiting") );
}
else
{
return new Container(alignment: Alignment.center, width: width, height:height, color: color, child: new Text("else") );
}
}
);
var streamBuilder = new StreamBuilder<int>(
stream: myStream,
initialData: 1,
builder: (ctx, snapshot) =>
{
var data = snapshot.data;
return new Container(child: new Text($"stream data: {data}"));
}
);
color: Color.fromARGB(255, 255, 0, 0),
color: Colors.blueGrey,
new Text("Counter: " + counter,
style: new TextStyle(fontSize: 18, fontWeight: FontWeight.w100)),
new GestureDetector(
onTap: () =>
{
setState(() =>
{
counter++;
});
},
child: new Container(
padding: EdgeInsets.symmetric(20, 20),
color: counter % 2 == 0 ? Color.fromARGB(255, 0, 255, 0) : Color.fromARGB(255, 0, 0, 255),
child: new Text("Click Me",
style: new TextStyle(fontFamily: "racher", fontWeight: FontWeight.w100))
)
)
streamBuilder,
futureBuilder
}
)
);

2
com.unity.uiwidgets/Runtime/widgets/async.cs


}
// TODO(ianh): remove unreachable code above once https://github.com/dart-lang/linter/issues/1141 is fixed
class FutureBuilder<T> : StatefulWidget {
public class FutureBuilder<T> : StatefulWidget {
public FutureBuilder(
_async.AsyncWidgetBuilder<T> builder,
Key key = null,

正在加载...
取消
保存