Чтобы добавить градиентный фон в контейнер во Flutter, вы можете использовать различные подходы. Вот несколько методов с примерами кода:
Метод 1: использование виджета Containerи BoxDecoration:
import 'package:flutter/material.dart';
class GradientContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.blue, Colors.green],
),
),
);
}
}
Метод 2: использование виджета Containerи BoxDecorationс BoxDecoration.gradient:
import 'package:flutter/material.dart';
class GradientContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration.gradient(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.blue, Colors.green],
),
),
);
}
}
Метод 3: использование виджета Containerи BoxDecorationс пользовательским BoxDecorationPainter:
import 'package:flutter/material.dart';
class GradientContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
painter: BoxDecorationPainter(),
),
);
}
}
class BoxDecorationPainter extends BoxPainter {
@override
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
final Rect rect = offset & configuration.size!;
final Gradient gradient = LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.blue, Colors.green],
);
canvas.drawRect(rect, Paint()..shader = gradient.createShader(rect));
}
}
Это всего лишь несколько методов, которые вы можете использовать для добавления градиентного фона в контейнер во Flutter. Не стесняйтесь выбирать тот, который лучше всего соответствует вашим потребностям.