Виджет Positioned в Flutter
Внутри Stack
потомок в Positioned
привязывается к краям родителя. Параметры
top, left, right и
bottom задают расстояние от
соответствующего края стека в логических
пикселях. Можно указать не все стороны -
виджет займёт место от заданных отступов:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: HomePage()));
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Stack(
children: [
Container(
width: 200,
height: 140,
color: Colors.blueGrey,
),
const Positioned(
top: 8,
right: 8,
child: Icon(Icons.close, color: Colors.white),
),
const Positioned(
left: 12,
bottom: 12,
child: Text(
'Label',
style: TextStyle(color: Colors.white),
),
),
],
),
),
);
}
}
Если задать и left, и right
без width, потомок растянется по
ширине между отступами. То же по вертикали
для пары top и bottom.
Бейдж на углу карточки - типичный сценарий:
фон в стеке, кнопка в Positioned:
Stack(
children: [
const SizedBox(width: 160, height: 160),
Positioned.fill(
child: Container(color: Colors.black12),
),
const Positioned(
top: 0,
left: 0,
right: 0,
child: Center(child: Text('Top center')),
),
],
)
Соберите Stack так:
Stack(
children: [
Container(
width: 240,
height: 160,
color: Colors.indigo,
),
Positioned(
top: 10,
right: 10,
child: Icon(Icons.bookmark),
),
],
)