当我使用Transform.Rotate将水平ListView设置为对角线时,如下所示,它变为对角线,但左右边缘被截断.

有没有一种方法可以在不裁剪左右边缘的情况下显示,或者有没有小工具可以用来实现这一点?

我想出了一种方法,使用Stack小部件用渐变条覆盖左侧和右侧边缘,并使它们不可见. 我确实试过了,很好,但我想我应该问问有没有其他方法.

我认为这是因为屏幕宽度是作为约束从父级传递的,但有什么方法可以禁用该约束吗? 谢谢.

import 'package:flutter/material.dart';
import 'dart:math' as math;

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Animated Icons',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.greenAccent,
      body: Column(
        children: [
          Expanded(
            child: Center(
              child: SizedBox(
                width: double.infinity,
                height: 200.0,
                child: Transform.rotate(
                  angle: -math.pi / 20,
                  child: Container(
                    color: Colors.white,
                    height: 200.0,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      itemCount: 6,
                      itemBuilder: (context, innerIndex) => Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Container(
                          //padding: EdgeInsets.all(4.0),
                          height: 50.0,
                          width: 200.0,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20.0),
                            color:
                                Colors.red.withOpacity(1.0 - 0.1 * innerIndex),
                          ),
                          child:Center(child:Text(innerIndex.toString()),),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),

          Container(
            height:100.0,
            color: Colors.white,
            child:Center(
              child:Text('title'),
            ),
          )
        ],
      ),
    );
  }
}

enter image description here

推荐答案

通过使用Overflow Box的以下代码,不再截断左角和右角.

          Expanded(
            child: Center(
              child: OverflowBox(
                maxWidth: MediaQuery.of(context).size.width*1.2,
                child: Transform.rotate(
                  angle: -math.pi / 20,
                  child: Container(
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(20)),
                      color: Colors.white,
                    ),
                    height: 200.0,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      itemCount: 6,
                      itemBuilder: (context, innerIndex) => Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Container(
                          //padding: EdgeInsets.all(4.0),
                          height: 50.0,
                          width: 200.0,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20.0),
                            color:
                                Colors.red.withOpacity(1.0 - 0.1 * innerIndex),
                          ),
                          child:Center(child:Text(innerIndex.toString()),),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),

Flutter相关问答推荐

未处理异常:字符串类型不是值类型子类型的子类型'

如何在扩展其他小部件时访问Ref

UI中的Flutter 下拉列表值未更改

为什么PUT方法没有发出任何响应?

GitHub Actions工作流secret中的特殊字符未被保留

在Flutter 中更改扩展瓷砖的高度

将Riverpods NotifierProvider与State类一起使用

我想创建一个可滚动的堆叠列表项,每个项都朝向屏幕

Flutter守护进程无法启动

我如何在Android 13中通过Flutter应用程序打开文件(不是图像、视频或音频)?

将自定义容器附加到屏幕底部

ScaffoldMessenger 在等待键下方不起作用

升级到 Flutter 3.10 后,Flutter 键盘填充不起作用. Flutter 3.10 如何防止 BottomSheet 被键盘覆盖?

有没有办法在没有收缩包装的情况下在列中使用 ListView.Builder?

Flutter - 展开图标无法正常工作

Container 的 Flutter 背景图片不会出现

如何在 Flutter 中使用简写 IF 禁用单选按钮?

如何在 flutter 中制作这种类型的扩展瓦片

将容器扩展到安全区域 - Flutter

设计响应式卡片的最佳方法,以避免像素溢出错误或 _AssertionError