Skip to content

Commit 9760bb0

Browse files
Add an example showing how to use textures (#122779)
There didn't seem to be any examples on how to do this. I've only shown the Linux implementation, others may want to follow this PR up with support for other platforms.
1 parent 63a8298 commit 9760bb0

File tree

11 files changed

+724
-0
lines changed

11 files changed

+724
-0
lines changed

examples/texture/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Flutter Texture
2+
3+
An example to show how to use custom Flutter textures.

examples/texture/lib/main.dart

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter/services.dart';
7+
8+
class TexturePage extends StatefulWidget {
9+
const TexturePage({super.key});
10+
11+
@override
12+
State<TexturePage> createState() => _TexturePageState();
13+
}
14+
15+
class _TexturePageState extends State<TexturePage> {
16+
static const int textureWidth = 300;
17+
static const int textureHeight = 300;
18+
static const MethodChannel channel =
19+
MethodChannel('samples.flutter.io/texture');
20+
final Future<int?> textureId =
21+
channel.invokeMethod('create', <int>[textureWidth, textureHeight]);
22+
23+
// Set the color of the texture.
24+
Future<void> setColor(int r, int g, int b) async {
25+
await channel.invokeMethod('setColor', <int>[r, g, b]);
26+
}
27+
28+
@override
29+
Widget build(BuildContext context) {
30+
return Scaffold(
31+
appBar: AppBar(
32+
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
33+
title: const Text('Texture Example'),
34+
),
35+
body: Center(
36+
child: Column(
37+
mainAxisAlignment: MainAxisAlignment.center,
38+
children: <Widget>[
39+
FutureBuilder<int?>(
40+
future: textureId,
41+
builder: (BuildContext context, AsyncSnapshot<int?> snapshot) {
42+
if (snapshot.hasData) {
43+
if (snapshot.data != null) {
44+
return SizedBox(
45+
width: textureWidth.toDouble(),
46+
height: textureHeight.toDouble(),
47+
child: Texture(textureId: snapshot.data!),
48+
);
49+
} else {
50+
return const Text('Error creating texture');
51+
}
52+
} else {
53+
return const Text('Creating texture...');
54+
}
55+
},
56+
),
57+
const SizedBox(height: 10),
58+
OutlinedButton(
59+
child: const Text('Flutter Navy'),
60+
onPressed: () => setColor(0x04, 0x2b, 0x59)),
61+
const SizedBox(height: 10),
62+
OutlinedButton(
63+
child: const Text('Flutter Blue'),
64+
onPressed: () => setColor(0x05, 0x53, 0xb1)),
65+
const SizedBox(height: 10),
66+
OutlinedButton(
67+
child: const Text('Flutter Sky'),
68+
onPressed: () => setColor(0x02, 0x7d, 0xfd)),
69+
const SizedBox(height: 10),
70+
OutlinedButton(
71+
child: const Text('Red'),
72+
onPressed: () => setColor(0xf2, 0x5d, 0x50)),
73+
const SizedBox(height: 10),
74+
OutlinedButton(
75+
child: const Text('Yellow'),
76+
onPressed: () => setColor(0xff, 0xf2, 0x75)),
77+
const SizedBox(height: 10),
78+
OutlinedButton(
79+
child: const Text('Purple'),
80+
onPressed: () => setColor(0x62, 0x00, 0xee)),
81+
const SizedBox(height: 10),
82+
OutlinedButton(
83+
child: const Text('Green'),
84+
onPressed: () => setColor(0x1c, 0xda, 0xc5)),
85+
],
86+
),
87+
),
88+
);
89+
}
90+
}
91+
92+
void main() {
93+
runApp(const MaterialApp(home: TexturePage()));
94+
}

examples/texture/linux/CMakeLists.txt

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Project-level configuration.
2+
cmake_minimum_required(VERSION 3.10)
3+
project(runner LANGUAGES CXX)
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
# The name of the executable created for the application. Change this to change
7+
# the on-disk name of your application.
8+
set(BINARY_NAME "texture")
9+
# The unique GTK application identifier for this application. See:
10+
# https://wiki.gnome.org/HowDoI/ChooseApplicationID
11+
set(APPLICATION_ID "io.flutter.examples.texture")
12+
13+
# Explicitly opt in to modern CMake behaviors to avoid warnings with recent
14+
# versions of CMake.
15+
cmake_policy(SET CMP0063 NEW)
16+
17+
# Load bundled libraries from the lib/ directory relative to the binary.
18+
set(CMAKE_INSTALL_RPATH "$ORIGIN/lib")
19+
20+
# Root filesystem for cross-building.
21+
if(FLUTTER_TARGET_PLATFORM_SYSROOT)
22+
set(CMAKE_SYSROOT ${FLUTTER_TARGET_PLATFORM_SYSROOT})
23+
set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT})
24+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
25+
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
26+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
27+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
28+
endif()
29+
30+
# Define build configuration options.
31+
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
32+
set(CMAKE_BUILD_TYPE "Debug" CACHE
33+
STRING "Flutter build mode" FORCE)
34+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
35+
"Debug" "Profile" "Release")
36+
endif()
37+
38+
# Compilation settings that should be applied to most targets.
39+
#
40+
# Be cautious about adding new options here, as plugins use this function by
41+
# default. In most cases, you should add new options to specific targets instead
42+
# of modifying this function.
43+
function(APPLY_STANDARD_SETTINGS TARGET)
44+
target_compile_features(${TARGET} PUBLIC cxx_std_14)
45+
target_compile_options(${TARGET} PRIVATE -Wall -Werror)
46+
target_compile_options(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:-O3>")
47+
target_compile_definitions(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:NDEBUG>")
48+
endfunction()
49+
50+
# Flutter library and tool build rules.
51+
set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter")
52+
add_subdirectory(${FLUTTER_MANAGED_DIR})
53+
54+
# System-level dependencies.
55+
find_package(PkgConfig REQUIRED)
56+
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
57+
58+
add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}")
59+
60+
# Define the application target. To change its name, change BINARY_NAME above,
61+
# not the value here, or `flutter run` will no longer work.
62+
#
63+
# Any new source files that you add to the application should be added here.
64+
add_executable(${BINARY_NAME}
65+
"main.cc"
66+
"my_application.cc"
67+
"my_texture.cc"
68+
"${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
69+
)
70+
71+
# Apply the standard set of build settings. This can be removed for applications
72+
# that need different build settings.
73+
apply_standard_settings(${BINARY_NAME})
74+
75+
# Add dependency libraries. Add any application-specific dependencies here.
76+
target_link_libraries(${BINARY_NAME} PRIVATE flutter)
77+
target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK)
78+
79+
# Run the Flutter tool portions of the build. This must not be removed.
80+
add_dependencies(${BINARY_NAME} flutter_assemble)
81+
82+
# Only the install-generated bundle's copy of the executable will launch
83+
# correctly, since the resources must in the right relative locations. To avoid
84+
# people trying to run the unbundled copy, put it in a subdirectory instead of
85+
# the default top-level location.
86+
set_target_properties(${BINARY_NAME}
87+
PROPERTIES
88+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run"
89+
)
90+
91+
92+
# Generated plugin build rules, which manage building the plugins and adding
93+
# them to the application.
94+
include(flutter/generated_plugins.cmake)
95+
96+
97+
# === Installation ===
98+
# By default, "installing" just makes a relocatable bundle in the build
99+
# directory.
100+
set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle")
101+
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
102+
set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE)
103+
endif()
104+
105+
# Start with a clean build bundle directory every time.
106+
install(CODE "
107+
file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\")
108+
" COMPONENT Runtime)
109+
110+
set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data")
111+
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")
112+
113+
install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}"
114+
COMPONENT Runtime)
115+
116+
install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}"
117+
COMPONENT Runtime)
118+
119+
install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
120+
COMPONENT Runtime)
121+
122+
foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES})
123+
install(FILES "${bundled_library}"
124+
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
125+
COMPONENT Runtime)
126+
endforeach(bundled_library)
127+
128+
# Fully re-copy the assets directory on each build to avoid having stale files
129+
# from a previous install.
130+
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
131+
install(CODE "
132+
file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\")
133+
" COMPONENT Runtime)
134+
install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}"
135+
DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime)
136+
137+
# Install the AOT library on non-Debug builds only.
138+
if(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
139+
install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
140+
COMPONENT Runtime)
141+
endif()
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# This file controls Flutter-level build steps. It should not be edited.
2+
cmake_minimum_required(VERSION 3.10)
3+
4+
set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral")
5+
6+
# Configuration provided via flutter tool.
7+
include(${EPHEMERAL_DIR}/generated_config.cmake)
8+
9+
# TODO: Move the rest of this into files in ephemeral. See
10+
# https://github.com/flutter/flutter/issues/57146.
11+
12+
# Serves the same purpose as list(TRANSFORM ... PREPEND ...),
13+
# which isn't available in 3.10.
14+
function(list_prepend LIST_NAME PREFIX)
15+
set(NEW_LIST "")
16+
foreach(element ${${LIST_NAME}})
17+
list(APPEND NEW_LIST "${PREFIX}${element}")
18+
endforeach(element)
19+
set(${LIST_NAME} "${NEW_LIST}" PARENT_SCOPE)
20+
endfunction()
21+
22+
# === Flutter Library ===
23+
# System-level dependencies.
24+
find_package(PkgConfig REQUIRED)
25+
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
26+
pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0)
27+
pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0)
28+
29+
set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/libflutter_linux_gtk.so")
30+
31+
# Published to parent scope for install step.
32+
set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE)
33+
set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE)
34+
set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE)
35+
set(AOT_LIBRARY "${PROJECT_DIR}/build/lib/libapp.so" PARENT_SCOPE)
36+
37+
list(APPEND FLUTTER_LIBRARY_HEADERS
38+
"fl_basic_message_channel.h"
39+
"fl_binary_codec.h"
40+
"fl_binary_messenger.h"
41+
"fl_dart_project.h"
42+
"fl_engine.h"
43+
"fl_json_message_codec.h"
44+
"fl_json_method_codec.h"
45+
"fl_message_codec.h"
46+
"fl_method_call.h"
47+
"fl_method_channel.h"
48+
"fl_method_codec.h"
49+
"fl_method_response.h"
50+
"fl_plugin_registrar.h"
51+
"fl_plugin_registry.h"
52+
"fl_standard_message_codec.h"
53+
"fl_standard_method_codec.h"
54+
"fl_string_codec.h"
55+
"fl_value.h"
56+
"fl_view.h"
57+
"flutter_linux.h"
58+
)
59+
list_prepend(FLUTTER_LIBRARY_HEADERS "${EPHEMERAL_DIR}/flutter_linux/")
60+
add_library(flutter INTERFACE)
61+
target_include_directories(flutter INTERFACE
62+
"${EPHEMERAL_DIR}"
63+
)
64+
target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}")
65+
target_link_libraries(flutter INTERFACE
66+
PkgConfig::GTK
67+
PkgConfig::GLIB
68+
PkgConfig::GIO
69+
)
70+
add_dependencies(flutter flutter_assemble)
71+
72+
# === Flutter tool backend ===
73+
# _phony_ is a non-existent file to force this command to run every time,
74+
# since currently there's no way to get a full input/output list from the
75+
# flutter tool.
76+
add_custom_command(
77+
OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS}
78+
${CMAKE_CURRENT_BINARY_DIR}/_phony_
79+
COMMAND ${CMAKE_COMMAND} -E env
80+
${FLUTTER_TOOL_ENVIRONMENT}
81+
"${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh"
82+
${FLUTTER_TARGET_PLATFORM} ${CMAKE_BUILD_TYPE}
83+
VERBATIM
84+
)
85+
add_custom_target(flutter_assemble DEPENDS
86+
"${FLUTTER_LIBRARY}"
87+
${FLUTTER_LIBRARY_HEADERS}
88+
)

examples/texture/linux/main.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "my_application.h"
6+
7+
int main(int argc, char** argv) {
8+
g_autoptr(MyApplication) app = my_application_new();
9+
return g_application_run(G_APPLICATION(app), argc, argv);
10+
}

0 commit comments

Comments
 (0)