简体   繁体   English

更改 Flutter 中的 TabbarView 当从另一个 class 按下按钮时,还需要使可滑动

[英]Change TabbarView in Flutter When pressed button from another class and also need to make swipe-able

Hey I m new in flutter now m stuck with the tab bar I have four files (Class), the first one is the parent file and the other three files(Class) are the child.嘿,我是 flutter 的新手,现在我卡在标签栏上我有四个文件(类),第一个是父文件,其他三个文件(类)是子文件。

Now I want to change tabbarview when I clicked the button from the child class.现在,当我单击子 class 的按钮时,我想更改 tabbarview。

I also shared my sample code please help me.我还分享了我的示例代码,请帮助我。

This is My Parent Class这是我的父母 Class

class AddItemTab extends StatefulWidget {
  const AddItemTab({Key? key}) : super(key: key);

  @override
  _AddItemTabState createState() => _AddItemTabState();
}

class _AddItemTabState extends State<AddItemTab> {

  final List<Widget> _fragments = [
    const ProductPurchase(),
    const ProtectionProduct(),
    const RoomProduct()
  ];
  int _page = 0;

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
          backgroundColor: MyColor.backgroundColor,
          body: Padding(
            padding: const EdgeInsets.only(
                top: 50.0, right: 20.0, left: 20.0, bottom: 20.0),
            child: Container(
              child: Column(
                children: [
                  Row(
                    children: [
                      Align(
                        alignment: Alignment.centerLeft,
                        child: IconButton(
                          padding: EdgeInsets.zero,
                          constraints: BoxConstraints(),
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                          icon: const Icon(Icons.arrow_back_ios),
                        ),
                      ),
                      Text("Back"),
                    ],
                  ),
                  SizedBox(
                    height: 15,
                  ),
                  const Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        'Add an item',
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 34,
                          fontFamily: 'Inter',
                          fontWeight: FontWeight.w700,
                        ),
                      )),
                  const SizedBox(
                    height: 15,
                  ),
                  Container(
                    height: 55,
                    width: double.infinity,
                    child: const TabBar(
                      indicator: BoxDecoration(
                        color: MyColor.buttonColor,
                        borderRadius: BorderRadius.all(
                          Radius.circular(5),
                        ),
                      ),

                      indicatorWeight: 5,
                      indicatorPadding: EdgeInsets.only(top:50),
                      // controller: _tabController,
                      labelColor: Colors.black,
                      tabs: [
                        Tab(
                          child: Text(
                            "Purchase",
                            textAlign: TextAlign.center,
                          ),
                        ),
                        Tab(
                          text: 'Protection',
                        ),
                        Tab(
                          text: 'Room',
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(height: 20),
                  Expanded(
                      child: TabBarView(
                    children: [
                      _fragments[0],
                      _fragments[1],
                      _fragments[2],
                    ],
                  ))
                ],
              ),
            ),
          )),
    );
  }
} 

This is My Child Class这是我的孩子 Class

class ProductPurchase extends StatefulWidget {
  const ProductPurchase({Key? key}) : super(key: key);

  @override
  _ProductPurchaseState createState() => _ProductPurchaseState();
}

class _ProductPurchaseState extends State<ProductPurchase> {
  final List<Widget> _fragments = [
    const ProtectionProduct(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColor.backgroundColor,
      body: Stack(
        children: [
          Padding(
            padding: EdgeInsets.only(bottom: 50),
            child: Align(
              alignment: Alignment.bottomCenter,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  padding: EdgeInsets.zero,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)),
                  elevation: 5,
                ),
                onPressed: () {
                  // Navigator.of(context).push(MaterialPageRoute(
                  //     builder: (context) => ProductView()));
                  // _fragments[0];
                },
                child: Ink(
                  decoration: BoxDecoration(
                      color: MyColor.buttonColor,
                      borderRadius: BorderRadius.circular(10)),
                  child: Container(
                    width: 250,
                    padding: const EdgeInsets.all(15),
                    constraints: const BoxConstraints(minWidth: 88.0),
                    child: const Text('Go To Next Tabbar View',
                        textAlign: TextAlign.center,`enter code here`
                        style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                            color: Colors.white)),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
} 

You need to use TabController for this, while you already have tab Bar and tab Bar view, you can do it like您需要为此使用TabController ,虽然您已经有标签栏和标签栏视图,但您可以这样做

class _AddItemTabState extends State<AddItemTab>
    with SingleTickerProviderStateMixin {
  final List<Widget> _fragments = [
  .....
  ];

  late final TabController controller = TabController(length: 3, vsync: this);
  @override
  Widget build(BuildContext context) {
 

........

child: TabBar(
  controller: controller,

......
Expanded(
      child: TabBarView(
    controller: controller,

And to move n index, here 2并移动 n 索引,这里是 2

onPressed: () {
  controller.animateTo(2);
},

To call from different widget using callback method使用回调方法从不同的小部件调用

class ProductPurchase extends StatefulWidget {
  final VoidCallback callback;
  const ProductPurchase({Key? key, required this.callback}) : super(key: key);

.....
onPressed:  (){
  widget.callback();
},

Once you used this widget, provide使用此小部件后,请提供

ProductPurchase(callback: (){
  controller.animateTo(2);
},);
class ProductPurchase extends StatefulWidget {
  final VoidCallback callback;
  const ProductPurchase({Key? key, required this.callback}) : super(key: key);

  @override
  _ProductPurchaseState createState() => _ProductPurchaseState();
}

class _ProductPurchaseState extends State<ProductPurchase> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // backgroundColor: MyColor.backgroundColor,
      body: Stack(
        children: [
          Padding(
            padding: EdgeInsets.only(bottom: 50),
            child: Align(
              alignment: Alignment.bottomCenter,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  padding: EdgeInsets.zero,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)),
                  elevation: 5,
                ),
                onPressed: () {
                  widget.callback(); //this
                },
                child: Ink(
                  decoration: BoxDecoration(
                      color: MyColor.buttonColor,
                      borderRadius: BorderRadius.circular(10)),
                  child: Container(
                    width: 250,
                    padding: const EdgeInsets.all(15),
                    constraints: const BoxConstraints(minWidth: 88.0),
                    child: const Text('Go To Next Tabbar View',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                            color: Colors.white)),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

And fragments和碎片

  late final List<Widget> _fragments = [
    ProductPurchase(
      callback: () {
        controller.animateTo(3);
      },
    ),
    Container(color: Colors.cyanAccent, child: Stack(children: [Text("fsA")])),
    Text("2A")
  ];

More about TabBar更多关于标签栏

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Flutter 中的 TabBarView 中从最后一个选项卡滑动到第一个选项卡 - How to swipe from last to first tab in TabBarView in Flutter 在 flutter 中创建五个按钮 单击按钮时,应立即按下一个按钮,其颜色应更改,同时重构小部件 - create five button in flutter only one button should be pressed at once when clicked the button its color should change also refactor the widget flutter:收藏夹按钮在按下时不会改变颜色 - flutter : favorite button not change color when pressed 按下按钮时颤动更改文本 - Flutter change text when button pressed 在 flutter 中按下时更改按钮的颜色 - Change color of button when pressed in flutter Flutter - 改变 TabBarView 的动画 - Flutter - Change the animation of TabBarView 我需要在按下动态凸起按钮时更改特定凸起按钮的背景颜色吗? - I need to change background color of a particular raised button from dynamic raised button when it is pressed? 颤动 TabBarView 不会通过 TabController 通过点击选项卡/选项卡而改变,滑动时不会改变,但 tabbarview 会 - flutter TabBarView doesn't change by TabController by tapping on tabs/tabs doesn't change when swiping but tabbarview does 颤动 TabBarView 有时不会在手势滑动时触发 - flutter TabBarView sometimes not triggering on gesture swipe flutter - 是否可以从另一个 class 制作提交按钮? - flutter - Is it possible to make submit button from another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM