刷新或重建Flutter屏幕

q8l4jmvw  于 2022-11-30  发布在  Flutter
关注(0)|答案(1)|浏览(195)

我有3个窗口小部件作为屏幕在我的主页。在主页我有底部导航栏之间导航。当我激活TextFormField在第一页,去屏幕二,并返回到第一页TextFormField仍然是活动的。

int _selectedIndex = 1;
  PageController? _pageController;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: 1);
  }

  @override
  void dispose() {
    _pageController!.dispose();
    super.dispose();
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
      _pageController!.animateToPage(index,
          duration: const Duration(milliseconds: 300), curve: Curves.easeOut);
    });
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthBloc, AuthState>(
      builder: (context, state) {
        return Scaffold(
          body: SizedBox.expand(
            child: PageView(
              controller: _pageController,
              onPageChanged: (index) {
                setState(() {
                  _selectedIndex = index;
                });
              },
              children: <Widget>[
                ProfileScreen(
                  user: state.user,
                ),
                const CreateQuestScreen(),
                const QuestDashboardScreen(),
              ],
            ),
          ),
          bottomNavigationBar: BottomNavigationBar(
            currentIndex: _selectedIndex,
            selectedItemColor: Colors.amber,
            unselectedItemColor: Colors.blue,
            onTap: _onItemTapped,
            iconSize: 40,
            items: const [
              BottomNavigationBarItem(
                  label: '', icon: Icon(Icons.account_circle_outlined)),
              BottomNavigationBarItem(
                  label: '', icon: Icon(Icons.add_circle_outline_rounded)),
              BottomNavigationBarItem(
                  label: '', icon: Icon(Icons.space_dashboard_outlined)),
            ],
          ),
        );
      },
    );
  }
}

这是我的屏幕

bool searchScreen = false;
  String searchQuery = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: PreferredSize(
          preferredSize: const Size.fromHeight(90),
          child: CustomAppBar(onChanged: (value) {
            if (value.isNotEmpty) {
              context.read<SearchUsersCubit>().searchUsers(value, context);
              setState(() {
                searchScreen = true;
                searchQuery = value;
              });
            } else {
              setState(() {
                searchScreen = false;
              });
            }
          }),
        ),
        body: Stack(
          children: [
            Column(
              children: [
                const SizedBox(height: 150),
                Center(child: Text(widget.user.username.toString())),
                const SizedBox(height: 150),
                TextButton(onPressed: () {}, child: const Text('Logout'))
              ],
            ),
            if (searchScreen) SearchContainer(searchQuery: searchQuery),
          ],
        ));
  }
}

当我想在底部导航栏中返回时,如何重建或刷新屏幕?

70gysomp

70gysomp1#

_pageController = PageController(initialPage: 1, keepPage: false);

相关问题