簡體   English   中英

更改 Flutter 中的 TabbarView 當從另一個 class 按下按鈕時,還需要使可滑動

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

嘿,我是 flutter 的新手,現在我卡在標簽欄上我有四個文件(類),第一個是父文件,其他三個文件(類)是子文件。

現在,當我單擊子 class 的按鈕時,我想更改 tabbarview。

我還分享了我的示例代碼,請幫助我。

這是我的父母 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],
                    ],
                  ))
                ],
              ),
            ),
          )),
    );
  }
} 

這是我的孩子 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)),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
} 

您需要為此使用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,

並移動 n 索引,這里是 2

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

使用回調方法從不同的小部件調用

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

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

使用此小部件后,請提供

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)),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

和碎片

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

更多關於標簽欄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM