Flutter - 在页面之间导航

时间:2021-02-04 15:27:28

标签: flutter

我正在制作一个简单的应用程序来了解有关 Flutter 的一些知识,但在我的页面之间导航时遇到问题。

该应用只有三个页面(主页、搜索、设置),我制作了一个简单的导航栏。现在我正在使用:

Navigator.of(context).pushReplacementNamed("/home");

问题是我想在页面之间导航,而不创建“新”页面。例如,如果我在“搜索”页面上有一个文本字段,然后我转到主页并返回“搜索”页面,则文本字段中的输入将保留。

在不推送或弹出“新”页面的情况下在页面之间导航的最佳方式是什么?

3 个答案:

答案 0 :(得分:0)

您可以查看带有底部导航栏的示例,以便在小部件之间切换以显示不同的视图。你可以按照这个例子来创建你需要的东西。

import 'package:flutter/material.dart';

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

/// This is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

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

/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
      TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
    Text(
      'Index 0',
      style: optionStyle,
    ),
    Text(
      'Index 1',
      style: optionStyle,
    ),
    Text(
      'Index 2',
      style: optionStyle,
    ),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Tab 0',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            label: 'Tab 1',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school),
            label: 'Tab 2',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}

答案 1 :(得分:0)

在颤振中 pushReplacementNamed 将替换和处置当前的 UI,因此您需要的只是 push

      Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => SecondRoute()),
      );

因此,我建议您阅读以下部分内容,并推荐使用 Get Plugin。

https://pub.dev/packages/get

https://medium.com/flutter/learning-flutters-new-navigation-and-routing-system-7c9068155ade

https://flutter.dev/docs/cookbook/navigation/navigation-basics

答案 2 :(得分:0)

有一个外部 TextEditingController。

class MyApp extends StatelessWidget {
  final searchController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        ...
        routes: {
          '/search': (_) => SearchPage(searchController),
          ...
        },
    );
  }
}

SearchPage 中,使用传递的 searchController 作为搜索字段。

TextField(
  ...
  controller: searchController,
)

如果您真的不想创建新页面。

SearchPage searchPage;

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        ...
        routes: {
          '/search': (_) => searchPage ??= SearchPage(),
          ...
        },
    );
  }
}
相关问题