当我在底部导航中的屏幕之间导航时,我的Flutter应用会不断重建自身

时间:2020-10-02 20:21:06

标签: android flutter dart

当我尝试在底部导航上的页面之间导航时,我的应用程序不断崩溃,当我尝试在用户打开应用程序时仅运行一次功能时出现问题,但是我的应用程序仍在重建主屏幕,有三个屏幕,第三个屏幕不重建,只有主屏幕,我尝试删除任何初始化状态或处置函数以查看是否是原因,但没有运气,我需要它停止在应用程序中重建很多次,因为我拥有的功能应该只运行一次

    import 'package:flutter/material.dart';
    import 'package:hotel_search/styles/texts.dart';
    import 'package:hotel_search/styles/colors.dart';
    import 'package:hotel_search/widgets/result.dart';
    import 'package:flutter_spinkit/flutter_spinkit.dart';
    import 'package:date_range_picker/date_range_picker.dart' as DateRagePicker;
    import 'package:intl/intl.dart';
    import 'package:numberpicker/numberpicker.dart';
    import 'package:flutter_picker/flutter_picker.dart';
    import 'dart:convert';
    import 'package:hotel_search/detail1.dart';
    
    import 'package:hotel_search/common/icons.dart';
    import 'package:hotel_search/common/stub_data.dart';
    import 'package:hotel_search/common/theme.dart';
    import 'package:hotel_search/parallax_page_view.dart';
    import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
    import 'package:hotel_search/splash.dart';
    import 'package:splashscreen/splashscreen.dart';
    import 'package:flutter/services.dart';
    import 'package:custom_navigation_bar/custom_navigation_bar.dart';
    import 'package:hotel_search/start.dart';
    import 'package:provider/provider.dart';
    import 'package:hotel_search/model.dart';
    import 'package:show_up_animation/show_up_animation.dart';
    import 'package:ripple_backdrop_animate_route/ripple_backdrop_animate_route.dart';
    import 'package:geolocator/geolocator.dart';
    import 'package:url_launcher/url_launcher.dart';
    import 'package:overlay_screen/overlay_screen.dart';
    import 'package:fluttertoast/fluttertoast.dart';
    
    import 'package:hotel_search/FadeAnimation.dart';
    import 'package:page_transition/page_transition.dart';
    import 'package:animated_text_kit/animated_text_kit.dart';
    import 'package:flutter_svg/flutter_svg.dart';
    
class HomePage extends StatefulWidget {
  Home createState() => Home();
}

class Home extends State<HomePage> with TickerProviderStateMixin {
  Home createState() => Home();
  static bool ready = true;

  final List<String> hotelCategories = StubData().hotelCategories;
  final List<capitalCard> capital = StubData().capital;
  final List<livingstoniaCard> livingstonia = StubData().livingstonia;
  final List<mountsocheCard> mountsoche = StubData().mountsoche;
  final List<mzuzuCard> mzuzu = StubData().mzuzu;
  final List<lilongweCard> lilongwe = StubData().lilongwe;
  final List<kuchaweCard> kuchawe = StubData().kuchawe;
  final List<nkopolaCard> nkopola = StubData().nkopola;

  int checkedItem = 0;

  static const PickerData2 = '''
[
    [
        1 ,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10
    ],
    [
       1 ,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10
    ],
    [
        "Adults"
    ]
]
    ''';

  static var datePicked = "Click here";
  static var roomPicked = "Click here";
  bool start = false;
  GlobalKey<ScaffoldState> _drawerKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    final themeData = HotelConceptThemeProvider.get();

    return Scaffold(
        key: _drawerKey,
        drawerEdgeDragWidth: 0,
        drawer: Drawer(
          child: ListView(
            children: <Widget>[ListTile()],
          ),
        ),
        backgroundColor: Colors.white,
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(60.0),
          child: AppBar(
            bottomOpacity: 0.0,
            elevation: 0.0,
            centerTitle: true,
            backgroundColor: Colors.white,
            title: Text(
              "Sunbird Hotels",
              style: textStyles['appBar'],
            ),
            leading: IconButton(
              onPressed: () {
                _drawerKey.currentState.openDrawer();
              },
              icon: Icon(
                Icons.menu,
                color: Colors.grey[800],
              ),
            ),
            actions: <Widget>[
              CircleAvatar(
                radius: 25,
                backgroundColor: Colors.white,
                child: ClipOval(
                  child: Image.asset(
                    'img/logo.jpg',
                    width: 34,
                    height: 34,
                  ),
                ),
              ),
            ],
          ),
        ),
        body:

1 个答案:

答案 0 :(得分:0)

您可以执行2种解决方案

1-根据官方文档,使用KeepAlive类(将孩子标记为需要存活,即使该孩子处于懒惰列表中也可以将其删除)。

在代码中:

class YourStatefulWidget extends StatefulWidget {

//add with AutomaticKeepAliveClientMixin to the state class

class _YourStatefulWidgetState extends State<YourStatefulWidget > with AutomaticKeepAliveClientMixin {

  //add  super.build(context) under the build method
  Widget build(BuildContext context) {
  super.build(context);

  return //your widget

   @override
   bool get wantKeepAlive => true;
  }
 }
}

2-将另一个屏幕用作具有异步功能的第一个屏幕,使其等待仅运行一次的功能完成,然后使用Navigator.pushReplacement导航到主屏幕,这样您就可以从家庭中删除功能屏幕。

这是一个正常行为,可以节省内存。