You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中为BookingOrdersScreen标签页添加带阴影Container及报错排查

解决方案

1. 先修复TabController长度不匹配问题

你的代码里TabController初始化时设了length:3,但实际只有2个标签页,这是导致添加组件时出现红线报错的核心原因之一。先修正这个参数:

@override
void initState() {
  tabController = TabController(length: 2, vsync: this); // 改为2,和实际标签数量一致
  super.initState();
  currentUserID = FirebaseAuth.instance.currentUser!.uid;
}

2. 封装可复用的带阴影Container组件

不需要每次都写重复的阴影样式,直接封装一个自定义Widget,方便在两个标签页里复用:

class ShadowContainer extends StatelessWidget {
  final Widget child;
  const ShadowContainer({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.3),
            spreadRadius: 2,
            blurRadius: 5,
            offset: const Offset(0, 3), // 控制阴影的偏移方向和距离
          ),
        ],
      ),
      child: child,
    );
  }
}

3. 在两个标签页中使用该组件

替换TabBarView里的占位组件,用自定义的ShadowContainer实现目标效果:

Expanded(
  child: TabBarView(
    controller: tabController,
    children: [
      // Products标签页内容
      SingleChildScrollView(
        child: Column(
          children: [
            ShadowContainer(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: const [
                    Text("我的产品", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                    SizedBox(height: 12),
                    Text("这里可以添加产品列表、详情等内容"),
                    // 根据需求扩展更多组件
                  ],
                ),
              ),
            ),
            // 可以添加多个带阴影的容器
          ],
        ),
      ),
      // Orders标签页内容
      SingleChildScrollView(
        child: Column(
          children: [
            ShadowContainer(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: const [
                    Text("我的订单", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                    SizedBox(height: 12),
                    Text("这里可以添加订单列表、详情等内容"),
                    // 根据需求扩展更多组件
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    ],
  ),
)

完整修改后的代码

class BookingOrdersScreen extends StatefulWidget {
  const BookingOrdersScreen({super.key});

  @override
  State<BookingOrdersScreen> createState() => _BookingOrdersScreenState();
}

class _BookingOrdersScreenState extends State<BookingOrdersScreen>
    with SingleTickerProviderStateMixin {
  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
  Stream<QuerySnapshot<Map>>? stream;
  late String currentUserID;
  TabController? tabController;

  @override
  void initState() {
    tabController = TabController(length: 2, vsync: this);
    super.initState();
    currentUserID = FirebaseAuth.instance.currentUser!.uid;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
          backgroundColor: const Color.fromARGB(255, 255, 193, 59),
          title: const SizedBox(
            width: 140,
            height: 140,
            // 可在此添加AppBar标题内容,比如图片或文字
          ),
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.vertical(
              bottom: Radius.circular(30),
            ),
          ),
          automaticallyImplyLeading: false),
      body: SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(
              height: MediaQuery.of(context).size.height / 1.5,
              child: DefaultTabController(
                length: 2,
                child: Column(
                  children: [
                    const SizedBox(height: 10),
                    TabBar(
                      tabs: [
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Icon(
                            Icons.calendar_month,
                            color: const Color.fromARGB(255, 3, 59, 161),
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Icon(
                            Icons.shopping_basket,
                            color: const Color.fromARGB(255, 3, 59, 161),
                          ),
                        ),
                      ],
                    ),
                    Expanded(
                      child: TabBarView(
                        controller: tabController,
                        children: [
                          // Products标签页
                          SingleChildScrollView(
                            child: Column(
                              children: [
                                ShadowContainer(
                                  child: Padding(
                                    padding: const EdgeInsets.all(16.0),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: const [
                                        Text("我的产品", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                                        SizedBox(height: 12),
                                        Text("这里展示你的产品列表或详情内容"),
                                      ],
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                          // Orders标签页
                          SingleChildScrollView(
                            child: Column(
                              children: [
                                ShadowContainer(
                                  child: Padding(
                                    padding: const EdgeInsets.all(16.0),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: const [
                                        Text("我的订单", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                                        SizedBox(height: 12),
                                        Text("这里展示你的订单列表或详情内容"),
                                      ],
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

// 自定义带阴影的Container组件
class ShadowContainer extends StatelessWidget {
  final Widget child;
  const ShadowContainer({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.3),
            spreadRadius: 2,
            blurRadius: 5,
            offset: const Offset(0, 3),
          ),
        ],
      ),
      child: child,
    );
  }
}

说明

  • 自定义ShadowContainer组件统一了阴影样式,避免重复代码,后续要调整阴影效果只需修改这一处即可。
  • 修复TabController长度不匹配后,之前添加组件的红线报错会消失。
  • 两个标签页都使用了相同的组件,实现了一致的视觉效果。
  • 可根据需求调整阴影的颜色、模糊度、偏移量,以及容器的圆角、内边距等参数。

内容的提问来源于stack exchange,提问作者sanda17

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 16:34:51