android 键盘在setState()上自动打开

0mkxixxg  于 2023-01-03  发布在  Android
关注(0)|答案(1)|浏览(126)

我正在使用flutter构建一个移动的应用程序。该应用程序有一个带有AnimatedIndexedStack的主页。要切换到另一个屏幕,我只需更改索引。如果我运行该应用程序,我可以切换到任何屏幕,没有任何问题。然而,如果我切换到一个带有文本字段的屏幕,并点击文本字段进行编辑。现在取消键盘后,如果我切换到任何其他屏幕,当屏幕上没有文本域,我也没有点击任何文本域时,键盘会自动弹出。无论是开始屏幕还是结束屏幕,键盘都会在更改索引后自动弹出。
动画索引堆栈如下所示:

import 'package:flutter/material.dart';

class AnimatedIndexedStack extends StatefulWidget {
  final int index;
  final List<Widget> children;

  const AnimatedIndexedStack({
    Key? key,
    required this.index,
    required this.children,
  }) : super(key: key);

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

class _AnimatedIndexedStackState extends State<AnimatedIndexedStack>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;
  int _index = 0;

  @override
  void initState() {
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 250),
    );
    _animation = Tween(begin: 0.0, end: 1.0).animate(

      CurvedAnimation(
        parent: _controller,
        curve: Curves.ease,
      ),
    );

    _index = widget.index;
    _controller.forward();
    super.initState();
  }

  @override
  void didUpdateWidget(AnimatedIndexedStack oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.index != _index) {
      _controller.reverse().then((_) {
        setState(() => _index = widget.index);
        _controller.forward();
      });
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animation,
      builder: (context, child) {
        return Opacity(
          opacity: _controller.value,
          child: child,
        );
      },
      child: IndexedStack(
        index: _index,
        children: widget.children,
      ),
    );
  }
}

首页看起来像这样:

class Homepage extends StatefulWidget {

  const Homepage({Key? key}) : super(key: key);

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

class _HomepageState extends State<Homepage> {

  
  var _currentIndex = 0;
  final _homeIndex = 0;
  final _titles = ['Home', 'New Booking', 'Trips', 'Payment History', 'Support', 'Profile'];
  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
  var _bookingStep = 0;

  @override
  void initState() {
    _currentIndex = _homeIndex;

    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    return WillPopScope(
      onWillPop: () async => _onWillPop(),
      child: Scaffold(
        key: scaffoldKey,
        extendBody: true,
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,

        drawer: AppDrawer(selectedIndex: _currentIndex, onItemTap: (index) {
          setState(() {
            _currentIndex = index;
          });
          scaffoldKey.currentState!.closeDrawer();
        },),
   

        body: SafeArea(
          bottom: false,
          child: AnimatedIndexedStack(
            index: _currentIndex,
            children: [
              const BookingsScreen(),
              NewBookingScreen(onExit: () {
                setState(() {
                  _currentIndex = 0;
                });
              }, bookingStep: _bookingStep, nextStep: () {
                setState(() {
                  _bookingStep = _bookingStep + 1;
                });
              },),
              const Trips(),
              const PaymentHistoryScreen(),
              SupportScreen(),
              const ProfileScreen()
            ],
          )
        ),
      ),
    );
  }
}
2ic8powd

2ic8powd1#

尝试将此添加到您遇到问题的所有屏幕,

FocusManager.instance.primaryFocus?.unfocus()

相关问题