Flutter导航器问题!如何通过Button(在页面中)转到另一个导航页面?

问题描述 投票:0回答:1

what i want to do

我想使用顶部的搜索按钮,转到另一个导航页面,并保留navigativeBar,我尝试使用navigator.push(),但是它将转到没有navigativeBar的新页面,我该怎么办?

这是我的一些代码:

 return Scaffold(
      drawer: _buildDrawer(context),
      body: IndexedStack(
        index: pageIx,
        children: <Widget>[
          _buildBody(), Guanzhu(),
          Tansuo(),
          //Disan(list),
          Setting(), Dier()
        ],
      ),
      bottomNavigationBar: AnimatedBottomBar(
        onBarTap: (index) => setState(() => pageIx = index),
        barItems: widget.barItems,
        animationDuration: const Duration(milliseconds: 150),
        barStyle: BarStyle(fontSize: 20.0, iconSize: 30.0),
      ),
    );
flutter navigation navigationbar
1个回答
0
投票

您可以更改pageIx以索引所需的索引在演示中,第二页索引为1,颜色为绿色您可以在下面复制粘贴运行完整代码

代码段

RaisedButton(
              onPressed: () {
                setState(() {
                  pageIx = 1;
                });
              },
              child: Text('Show second Widget'),
            )

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int pageIx = 2;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "IndexedStack Example",
      home: Scaffold(
        appBar: AppBar(title: Text("IndexedStack Example")),
        body: Column(
          children: <Widget>[
            Expanded(
              child: IndexedStack(
                index: pageIx,
                children: <Widget>[
                  Container(color: Colors.red,),
                  Container(color: Colors.green),
                  Container(color: Colors.blue),
                ],
              ),
            ),
            RaisedButton(
              onPressed: () {
                setState(() {
                  if(pageIx < 2){
                    pageIx++;
                  } else{
                    pageIx = 0;
                  }
                });
              },
              child: Text('Show Next Widget'),
            ),
            RaisedButton(
              onPressed: () {
                setState(() {
                  pageIx = 1;
                });
              },
              child: Text('Show second Widget'),
            )
          ],
        ),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.