flutter 禁用滑动到特定选项卡

eh57zj3b  于 2023-05-19  发布在  Flutter
关注(0)|答案(2)|浏览(214)

我正在Flutter中开发一个应用程序,有一个简单的问题-我基本上想禁用滑动到+标签页。ChatGPT是无用的,我不知道如何做到这一点。非常感谢您的评分!下面是我的代码:

import 'package:flutter/material.dart';

class workout_page extends StatefulWidget {
  @override
  _WorkoutPageState createState() => _WorkoutPageState();
}

class _WorkoutPageState extends State<workout_page>
    with TickerProviderStateMixin {
  late TabController _tabController;
  List<Map<String, dynamic>> workoutMap = [
    {
      'name': 'My First Workout',
      'exercises': [
        {
          'name': 'Bicep Curl',
          'musclesworked': ['bicep', 'tricep']
        },
        {
          'name': 'Preacher Curl',
          'musclesworked': ['bicep', 'tricep']
        }
      ]
    },
  ];

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

  TextEditingController _newTabController = TextEditingController();

  void _addTab() {
    setState(() {
      String newTabName = _newTabController.text;
      Map<String, dynamic> newTab = {
        'name': newTabName,
        'exercises': [],
      };
      workoutMap.add(newTab);
      _newTabController.clear();
      _tabController = TabController(
        length: workoutMap.length + (workoutMap.length < 4 ? 1 : 0),
        vsync: this,
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return DefaultTabController(
      length: workoutMap.length + (workoutMap.length < 4 ? 1 : 0),
      child: Scaffold(
        backgroundColor: Color.fromRGBO(79, 79, 79, 1),
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(60),
          child: AppBar(
            backgroundColor: Color.fromARGB(255, 19, 19, 19),
            centerTitle: false,
            flexibleSpace: Column(
              children: [
                SizedBox(height: size.height * .061),
                TabBar(
                  controller: _tabController,
                  tabs: [
                    ...workoutMap
                        .map((tab) => Tab(text: tab['name'] as String)),
                    if (workoutMap.length < 4)
                      Container(
                        width: 40,
                        height: 40,
                        alignment: Alignment.center,
                        child: IconButton(
                          icon: Icon(Icons.add),
                          onPressed: () {
                            showDialog(
                              context: context,
                              builder: (context) => AlertDialog(
                                title: Text('Add Tab'),
                                content: TextField(
                                  controller: _newTabController,
                                  decoration: InputDecoration(
                                    hintText: 'Enter tab name',
                                  ),
                                ),
                                actions: [
                                  TextButton(
                                    child: Text('Cancel'),
                                    onPressed: () {
                                      Navigator.pop(context);
                                    },
                                  ),
                                  TextButton(
                                    child: Text('Add'),
                                    onPressed: () {
                                      _addTab();
                                      Navigator.pop(context);
                                    },
                                  ),
                                ],
                              ),
                            );
                          },
                        ),
                      ),
                  ],
                ),
              ],
            ),
          ),
        ),
        body: TabBarView(
          controller: _tabController,
          children: [
            ...workoutMap.map((tab) => Column(
                  children: [
                    Text(tab['name'] as String),
                  ],
                )),
            if (workoutMap.length < 4) Container(),
          ],
        ),
      ),
    );
  }
}

PS:我必须补充这一点,以便得到这个职位在那里-再次感谢你这么多!

whlutmcx

whlutmcx1#

将“physics”赋予TabBar和TabBarView。

TabBar(
       physics: const NeverScrollableScrollPhysics(),
       controller: _tabController,
       .
       .
       .
),

TabBarView(
           controller: _tabController,
           physics: const NeverScrollableScrollPhysics(),
           children: [
  ]
)
n1bvdmb6

n1bvdmb62#

您需要物理指示到您的小部件。不希望出现滑动功能的位置。添加

physics: NeverScrollableScrollPhysics()

这将解决您的问题。

相关问题