合并命名为Routes和PageView

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

我对开发和构建我的第一个真实应用程序还很陌生。我实现了一个路由器类,并从图标按钮生成用于导航的命名路由。下一步,我也想通过滑动在3个屏幕之间切换。

我的结构是:

main.dart (returns MaterialApp)  
FirstScreen.dart (returns Scaffold)  
SecondScreen.dart (returns Scaffold)  
ThirdScreen.dart (returns Scaffold)

我试图通过在main.dart中添加Pageview小部件来实现滑动功能,但是当我切换到使用IconButtons导航并切换到另一个屏幕时,滑动功能将不再起作用。我想问题很明显,因为我不再使用main.dart了,它不会加载PageView。但是我缺乏整合这两个功能的简洁方法。

非常感谢我能获得的所有帮助,预先致谢!

flutter routes swipe named generated
2个回答
0
投票
我当前的代码:

main.dart

void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'WeinCheckerApp', home: PageViewScreen(), ); } } class PageViewScreen extends StatefulWidget { @override _PageViewScreenState createState() => _PageViewScreenState(); } class _PageViewScreenState extends State<PageViewScreen> { PageController _pageController = PageController( initialPage: 1, ); @override void dispose() { _pageController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.grey[800], appBar: AppBar( backgroundColor: Colors.red[900], title: Center(child: Text("WeinCheckerApp")), ), body: PageView( controller: _pageController, children: [ SearchScreen(), ScanScreen(), FavScreen(), ], ), ); } }

ScanScreen.dart(三个屏幕之一目前都相同):

class ScanScreen extends StatefulWidget {
  @override
  _ScanScreenState createState() => _ScanScreenState();
}

class _ScanScreenState extends State<ScanScreen> {
  @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    return SafeArea(
      minimum: EdgeInsets.only(
        left: SizeConfig.safeBlockVertical,
        right: SizeConfig.safeBlockVertical,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              Expanded(
                child: IconButton(
                  icon: Icon(Icons.search),
                  color: Colors.white,
                  onPressed: () => {},
                  iconSize: SizeConfig.safeBlockVertical * 5,
                ),
              ),
              Ink(
                decoration:
                    ShapeDecoration(shape: CircleBorder(), color: Colors.white),
                child: IconButton(
                  icon: Icon(Icons.camera_alt),
                  color: Colors.grey[800],
                  onPressed: () => print("ScanButtonPressed"),
                  iconSize: SizeConfig.safeBlockVertical * 6,
                ),
              ),
              Expanded(
                child: IconButton(
                  icon: Icon(Icons.star),
                  color: Colors.white,
                  onPressed: () => {},
                  iconSize: SizeConfig.safeBlockVertical * 5,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

IconButtons应该引导到其他屏幕。

© www.soinside.com 2019 - 2024. All rights reserved.