如何调整TabBarView的高度,以适应儿童在Flutter?

zengzsys  于 2023-06-24  发布在  Flutter
关注(0)|答案(2)|浏览(172)
class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        Card(
          child: DefaultTabController(
            length: 2,
            child: Column(
              children: const [
                TabBar(
                  tabs: [
                    Tab(icon: Icon(Icons.cloud_outlined)),
                    Tab(icon: Icon(Icons.beach_access_sharp)),
                  ],
                ),
                SizedBox(
                  height: 300, // delete this.
                  child: TabBarView(
                    children: [
                      //ListView(shrinkWrap: true) ← and adjust the TabBarView to the height of this.
                      Text(''),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}

我正在制作一个卡片,它允许你用TabBar切换内容。我想让TabBarView的高度与孩子们的ListView的高度相匹配,但当我删除SizedBox或使用Expanded时,我得到一个错误。你能建议我使用其他小部件的选项以及?

dojqjjoe

dojqjjoe1#

试试这个

class MTabs extends StatefulWidget {
  @override
  _StackOverState createState() => _StackOverState();
}

class _StackOverState extends State<MTabs>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  @override
  void initState() {
    _tabController = TabController(length: 3, vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: const EdgeInsets.all(8.0),
        color: Theme.of(context).primaryColor,
        child: Column(
          children: [
            // give the tab bar a height [can change height to preferred height]
            Container(
              height: 45,
              decoration: BoxDecoration(
                color: Theme.of(context).backgroundColor,
                borderRadius: BorderRadius.circular(
                  25.0,
                ),
              ),
              child: TabBar(
                controller: _tabController,
                // give the indicator a decoration (color and border radius)
                indicator: BoxDecoration(
                  borderRadius: BorderRadius.circular(
                    25.0,
                  ),
                  color: Theme.of(context).accentColor,
                ),
                labelColor: Theme.of(context).textSelectionColor,
                unselectedLabelColor: Theme.of(context).textSelectionColor,
                tabs: [
                  // first tab [you can add an icon using the icon property]
                  const Tab(
                    text: ViewStrings.txtTab1,
                  ),

                  // second tab [you can add an icon using the icon property]
                  const Tab(
                    text: ViewStrings.txtTab2,
                  ),
                  // second tab [you can add an icon using the icon property]
                  Tab(
                    text: ViewStrings.txtTab3,
                  ),
                ],
              ),
            ),
            // tab bar view here
            Expanded(
              child: TabBarView(
                controller: _tabController,
                children: const [
                  //  tab bar view widget
                  Tab1(),
                  Tab2(),
                  Tab3(),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
w41d8nur

w41d8nur2#

这项工作对我来说,我使用这个Flutter包:https://pub.dev/packages/autoscale_tabbarview

import 'package:autoscale_tabbarview/autoscale_tabbarview.dart';

并在文件中将TabBarView替换为AutoScaleTabBarView

相关问题