Color Extract - это упаковка Flutter, которая позволяет извлекать и вычислять цвета из виджетов вашего приложения. Вы можете использовать его, чтобы изменить цвет виджета в зависимости от цвета фона (другой виджет позади).
(Видеаплеер не может показывать на Pub.DEV, проверьте github.com)
В этой демонстрации посмотрите, как вы можете заставить свой виджет менять цвета, как хамелеон?
Добавьте следующее в свой pubspec.yaml :
dependencies :
color_extract : ^1.0.1 Затем запустите flutter pub get .
ColorExtractor Это виджет, из которого мы хотим извлечь средний цвет. Он служит оберткой для 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. Смотрите файл лицензии для получения дополнительной информации.