dart 在Flutter中,我如何使堆栈中的已定位项随背景网格滚动?

dm7nw8vv  于 2023-04-27  发布在  Flutter
关注(0)|答案(1)|浏览(111)

我正在尝试用Flutter做一个类似于每周的日程安排,并想做一个显示时间/大小的框的背景网格,然后在这些框之间的顶部放置项目。(水平滚动很好,我只是还没有添加垂直滚动列表)但是当我向堆栈添加定位项时,它们停留在原地,而不是随着页面滚动。我怎么才能让这些项目滚动呢?我应该为此使用这种策略吗?
以下是我目前为止的代码:

return Expanded(
     child: Stack(
       children: [
         ListView(scrollDirection: Axis.horizontal, children: [
           ...days
               .map((stage) => Container(
                     width: 100,
                     child: Column(
                       children: [
                         Text(day),
                         ...times.map((time) => Container(height: 100, child: Text(time))),
                       ],
                     ),
                   ))
               .toList(),
         ]),
         Container(
           child: Positioned(
             top: 20,
             bottom: 20,
             left: 100,
             right: 200,
             child: Container(
               height: 100,
               width: 100,
               child: Text('waddup'),
               color: Colors.blue,
             ),
           ),
         ),
       ],
     ),
   );

vql8enpb

vql8enpb1#

import 'package:flutter/material.dart';

void fileMain() {
  runApp(
    MaterialApp(
      home: MyApp(
        event: event,
      ),
    ),
  );
}

final now = DateTime.now();
final tomorrow = DateTime(now.year, now.month, now.day + 1);
final days = List.generate(7, (index) {
  final day = now.add(Duration(days: index));
  return '${day.day}/${day.month}?${day.year}';
});

final times = List.generate(24,
    (h) => '${(h % 12).toString().padLeft(2, '0')}:00 ${h < 12 ? 'AM' : 'PM'}');

final event = Event(
  start: tomorrow.add(const Duration(hours: 10)),
  duration: const Duration(hours: 2),
  title: 'My Event',
);

class MyApp extends StatelessWidget {
  const MyApp({
    super.key,
    required this.event,
  });

  final Event event;

  @override
  Widget build(BuildContext context) {
    final eventTop = event.start.hour * 50.0;
    final eventHeight = event.duration.inHours * 50.0;

    final dayDifferent = event.start.difference(now).inDays;

    final eventLeft = dayDifferent * 100.0 + 100;
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: Stack(
                  children: [
                    Row(
                      children: [
                        ...days
                            .map((day) => Container(
                                  width: 100,
                                  child: Column(
                                    children: [
                                      Text(day),
                                      ...times.map(
                                        (time) => Container(
                                          height: 50,
                                          child: Text(time),
                                        ),
                                      ),
                                    ],
                                  ),
                                ))
                            .toList(),
                      ],
                    ),
                    Positioned(
                      top: eventTop,
                      left: eventLeft,
                      child: IgnorePointer(
                        child: Container(
                          height: eventHeight,
                          width: 100,
                          color: Colors.blue,
                          alignment: Alignment.center,
                          child: Text(event.title),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

class Event {
  final DateTime start;
  final Duration duration;
  final String title;

  const Event({
    required this.start,
    required this.duration,
    required this.title,
  });
}

相关问题