สารสกัดสีเป็นแพ็คเกจกระพือที่ช่วยให้คุณสามารถแยกและคำนวณสีจากวิดเจ็ตแอปของคุณ คุณสามารถใช้มันเพื่อเปลี่ยนสีของวิดเจ็ตขึ้นอยู่กับสีของพื้นหลัง (วิดเจ็ตอื่น ๆ ด้านหลัง)
(เครื่องเล่นวิดีโออาจไม่แสดงบน pub.dev ตรวจสอบ github.com)
ในการสาธิตนี้ดูว่าคุณจะทำให้วิดเจ็ตของคุณเปลี่ยนสีเหมือนกิ้งก่าได้อย่างไร
เพิ่มสิ่งต่อไปนี้ใน pubspec.yaml ของคุณ:
dependencies :
color_extract : ^1.0.1 จากนั้นเรียก flutter pub get
ColorExtractor เป็นวิดเจ็ตที่เราต้องการดึงสีเฉลี่ยจาก มันทำหน้าที่เป็น wrapper สำหรับ repaintboundary ดังนั้นคุณสามารถใช้ repaintboundary เป็นทางเลือก
ColorExtractor (
boundaryKey : GlobalKey (),
child : Container (
width : 200 ,
height : 200 ,
color : Colors .red,
),
); วิดเจ็ต ColorAverager คำนวณสีเฉลี่ยของส่วนเฉพาะของ ColorExtractor หรือ RepaintBoundary แอปพลิเคชันของมันมีประโยชน์ในการกำหนดสีที่โดดเด่นของพื้นที่บางอย่างเช่นพื้นหลังด้านหลังโลโก้ภาพ
ColorAverager (
boundaryKey : GlobalKey (),
child : SizedBox (
width : 50 ,
height : 50 ,
),
onChanged : (color) {
// Handle the new average color.
},
); นอกจากนี้คุณยังสามารถใช้ ColorAveragerController เพื่อคำนวณสีเฉลี่ยโดยทางโปรแกรม
final controller = ColorAveragerController ();
// ... render the widget ...
final avgColor = await controller. calculateAvgColor (); import 'package:flutter/material.dart' ;
import 'package:color_extract/color_extract.dart' ;
class MyHomePage extends StatelessWidget {
@override
Widget build ( BuildContext context) {
return Scaffold (
body : Stack (
children : [
ColorExtractor (
boundaryKey : boundaryKey,
child : Container (
width : 200 ,
height : 200 ,
color : Colors .blue,
),
),
ColorAverager (
// boundaryKey should be the same one in the above ColorExtractor boundaryKey
boundaryKey : boundaryKey,
// You can use the controller (ColorAveragerController) too.
// controller: controller,
child : const SizedBox (width : 50 , height : 50 ),
onChanged : (color) {
// Do something with the average color.
// color should be = Colors.blue
},
)
],
)
);
}
}itisnajim, [email protected]
Color_extract สามารถใช้ได้ภายใต้ใบอนุญาต MIT ดูไฟล์ใบอนุญาตสำหรับข้อมูลเพิ่มเติม