如何在两个不同的屏幕上同时使用同一个块?

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

我正在尝试使用flutter_bloc将同一Firepaduser实例传递给两个不同的屏幕,主屏幕成功地向我显示了该问题,当我切换到另一个屏幕时,该屏幕一直处于等待状态,无法继续运行从那里。

UserBloc类

class UserBloc extends Bloc {
   final _auth_repository = FirebaseAuthApiRespository();

  Stream<FirebaseUser> streamFirebase = FirebaseAuth.instance.onAuthStateChanged;
  Stream<FirebaseUser> get auhtStatus => streamFirebase;

  Future<FirebaseUser> signIn() => _auth_repository.signInFirebase();
  signOut() {
    _auth_repository.signOut();
  }

  Future<FirebaseUser> signInFacebook() => _auth_repository.signInFacebook();
  signOutFacebook(){
    _auth_repository.signOutFacebook();
  }


   final _cloudFirestoreRepository = CloudFireStoreRepository();
    void updateUserData(User user) => _cloudFirestoreRepository.updateUserDataFirestore(user);



  @override
  // TODO: implement initialState
  get initialState => null;

  @override
  Stream mapEventToState(event) {
    // TODO: implement mapEventToState
    return null;
  }
}

main.dart

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setEnabledSystemUIOverlays([]);
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  ManagmentStorage managmentStorage = ManagmentStorage();
  @override
  void initState() {
    managmentStorage.createStragoe();
    super.initState();
  }

  Widget build(BuildContext context) {
    return MultiBlocProvider(
       providers: [
        BlocProvider<CameraBloc>(create: (BuildContext context) => CameraBloc()),
        BlocProvider<UserBloc>(create: (BuildContext context) => UserBloc()),
      ],
      child: MaterialApp(
        darkTheme: ThemeData.dark(),
        themeMode: ThemeMode.dark,
        debugShowCheckedModeBanner: false,
        home: SignInScreen(), //ProfileUserHome(), //
        routes: <String, WidgetBuilder>{
          '/adddocument': (BuildContext context) => ScreenAddPhoto(),
          '/screenimagedetail': (BuildContext context) => ScreenImageDetail()
        },
      ),

    );
  }
}

屏幕1:

class ProfileUser extends StatelessWidget {
  UserBloc userBloc;

  @override
  Widget build(BuildContext context) {
    userBloc = BlocProvider.of<UserBloc>(context);

    return StreamBuilder(
      stream: userBloc.auhtStatus,
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.none:
            return Center(
              child: SpinKitSquareCircle(color: Colors.white),
            );
            break;

          case ConnectionState.waiting:
            return Center(
              child: SpinKitSquareCircle(color: Colors.white),
            );
            break;

          case ConnectionState.active:
            return showPofileData(snapshot);
            break;

          case ConnectionState.done:
            return showPofileData(snapshot);
            break;

          default:
        }
      },
    );
  }
  Widget showPofileData(AsyncSnapshot snapshot) {
    if (!snapshot.hasData || snapshot.hasError) {
      return Stack(
        children: <Widget>[
          ListView(
            children: <Widget>[Text("Usuario no logeado. Haz login")],
          ),
        ],
      );
    } else {
      var user = User(
        uid: snapshot.data.uid,
        name: snapshot.data.displayName,
        email: snapshot.data.email,
        photoURL: snapshot.data.photoUrl,
      );

      return Stack(
        children: <Widget>[
          UserBody(),
          Header(
            user: user,
          )
        ],
      );
    }
  }
}

screen2:

class ProfileUserHome extends StatelessWidget {
  UserBloc userBloc;

  @override
  Widget build(BuildContext context) {
    userBloc = BlocProvider.of<UserBloc>(context);
    return Scaffold(
      body: StreamBuilder(
        stream: userBloc.auhtStatus,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
              return Center(
                child: SpinKitSquareCircle(color: Colors.white),
              );
              break;

            case ConnectionState.waiting:
              return Center(
                child: SpinKitSquareCircle(color: Colors.white),
              );
              break;

            case ConnectionState.active:
              return profileuserinfo(snapshot);
              break;

            case ConnectionState.done:
              return profileuserinfo(snapshot);
              break;
            default:
          }
        },
      ),
    );
  }

  Widget profileuserinfo(AsyncSnapshot snapshot) {
    if (!snapshot.hasData || snapshot.hasError) {
      return Center(child: Text("Usuario no logeado. Haz login"));
    } else {
      var user = User(
        uid: snapshot.data.uid,
        name: snapshot.data.displayName,
        email: snapshot.data.email,
        photoURL: snapshot.data.photoUrl,
      );
      return ListView(children: <Widget>[
        HeaderProfileUserHome(user: user)
      ]);
    }
  }
}

BottomNavigationBar:

class NavBarUser extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _NavBarUser();
  }
}

class _NavBarUser extends State<NavBarUser> {
  int indexTap = 0;
  @override
  Widget build(BuildContext context) {
    PageController controller = PageController(initialPage: 0, keepPage: true);

    void onTapTapped(int index) {
      setState(() {
        indexTap = index;
        controller.animateToPage(index,
            duration: Duration(milliseconds: 200), curve: Curves.ease);
      });
    }

    void pageChanged(int index) {
      setState(() {
        indexTap = index;
      });
    }

    final pageView = PageView(
      controller: controller,
      onPageChanged: (index) {
        pageChanged(index);
      },
      children: <Widget>[UserHome(), ProfileUserHome()],
    );

    return MultiBlocProvider(
       providers: [
        BlocProvider<CameraBloc>(create: (BuildContext context) => CameraBloc()),
        BlocProvider<UserBloc>(create: ( BuildContext context) => UserBloc()),

      ],
      child: Scaffold(
        body: pageView,
        bottomNavigationBar: Theme(
          data: Theme.of(context).copyWith(
              canvasColor: Colors.black,
              primaryColor: Color.fromARGB(230, 226, 80, 116)),
          child: BottomNavigationBar(
            onTap: (index) {
              onTapTapped(index);
            },
            currentIndex: indexTap,
            items: [
              BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("")),
              BottomNavigationBarItem(
                  icon: Icon(Icons.person), title: Text("")),
            ],
          ),
        ),
      ),

    );
  }
}

登录成功的图像:

enter image description here

图像屏幕1

enter image description here

图像screen2配置文件,这是问题所在:c

enter image description here

在个人资料屏幕上,我什么也没看到,当我回到家都没有enter image description here

enter image description here

enter image description here

enter image description here

flutter flutter-dependencies provider bloc
1个回答
0
投票
您做错了方法,您正在使用flutter_bloc库,因此:
© www.soinside.com 2019 - 2024. All rights reserved.