diff --git a/ohos/testpicture/lib/PicturePage.dart b/ohos/testpicture/lib/PicturePage.dart index 7532f6b2346332ccc7fadf742e52a967594cb846..9db1e5fb0f351dde641eaa7aaea82a1e605235dd 100644 --- a/ohos/testpicture/lib/PicturePage.dart +++ b/ohos/testpicture/lib/PicturePage.dart @@ -16,72 +16,84 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +class PicBean { + String name; + int id; + PicBean(this.name, this.id); +} + +List pics = [ + PicBean("1.png", 0), + PicBean("2.jpeg", 0), + PicBean("3.jpeg", 0), + PicBean("4.jpg", 0), + PicBean("6.jpg", 0), +]; + class PicturePage extends StatefulWidget { - // PicturePage({required Key key}) : super(key: key); const PicturePage({super.key}); @override - _PicturePageState createState() => _PicturePageState(); + State createState() => _PicturePageState(); } - class _PicturePageState extends State { - final MethodChannel _channel = MethodChannel('PictureChannel'); - - int textureId = -1; + final MethodChannel _channel = const MethodChannel('PictureChannel'); @override void initState() { super.initState(); - - newTexture(); + registerTextures(); } - @override void dispose() { super.dispose(); - if (textureId>=0){ - _channel.invokeMethod('unregisterTexture', {'textureId': textureId}); - - } + unregisterTextures(); } - - void getTextureId() async { - textureId = await _channel.invokeMethod('getTextureId'); + void registerTextures() async { + for (PicBean pic in pics) { + var id = + await _channel.invokeMethod("registerTexture", {'pic': pic.name}); + setState(() { + pic.id = id; + }); + } } - void newTexture() async { - var id = await _channel.invokeMethod('registerTexture', { - //本地图片名 - 'pic': "1.png", - // 'pic':'2.jpeg', - // 'pic':'3.jpeg', - }); - setState(() { - textureId = id; - }); + void unregisterTextures() async { + for (PicBean pic in pics) { + await _channel.invokeMethod('unregisterTexture', {'textureId': pic.id}); + } } - Widget getTextureBody(BuildContext context) { - return Container( - // color: Colors.red, - width: 300, - height: 300, - child: Texture( - textureId: textureId, - ), + Widget getTextureBody(BuildContext context, PicBean picBean) { + return Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + SizedBox( + width: 300, + height: 300, + child: Texture(textureId: picBean.id), + ), + Container( + height: 10, + ), + ], ); } @override Widget build(BuildContext context) { - Widget body = textureId>=0 ? getTextureBody(context) : Text('loading...'); - print('build textureId :$textureId'); - return Scaffold( appBar: AppBar( - title: Text("daex_texture"), + title: const Text("daex_texture"), ), - body: Container( - child: body, + body: ListView.builder( + itemCount: pics.length, + itemBuilder: (context, index) { + PicBean picBean = pics[index]; + return picBean.id >= 0 + ? getTextureBody(context, picBean) + : const Text('loading...'); + }, ), ); } diff --git a/ohos/testpicture/ohos/entry/src/main/resources/rawfile/4.jpg b/ohos/testpicture/ohos/entry/src/main/resources/rawfile/4.jpg new file mode 100644 index 0000000000000000000000000000000000000000..fb8e72489d0b0207ea56897dc7272fb3745e9baa Binary files /dev/null and b/ohos/testpicture/ohos/entry/src/main/resources/rawfile/4.jpg differ diff --git a/ohos/testpicture/ohos/entry/src/main/resources/rawfile/6.jpg b/ohos/testpicture/ohos/entry/src/main/resources/rawfile/6.jpg new file mode 100644 index 0000000000000000000000000000000000000000..ae61e5a2e0af6f4e8590279ddae92f86fb037d00 Binary files /dev/null and b/ohos/testpicture/ohos/entry/src/main/resources/rawfile/6.jpg differ