如何在两个屏幕之间使用集团模式

时间:2020-01-07 17:38:06

标签: flutter bloc

我的main.dart文件看起来像这样。

home: MultiBlocProvider(
      providers: [
        BlocProvider<UserBloc>(
          create: (BuildContext context) =>   UserBloc()..add(GetUser(userId:"5e0b62023b75dd60e22edaad")),
        ),
         BlocProvider<TodoBloc>(
           create: (BuildContext context)=> TodoBloc()..add(AddTodoEvent()),)
      ],
      child: FirstScreen(),
    ),

我要导航到FirstScreen中按下按钮的第二个屏幕。

var router = new MaterialPageRoute(
      builder: (BuildContext context){
        return BlocProvider<UserBloc>(
          create: (BuildContext context) =>   UserBloc(),
          child: SecondScreen(),
        );
      });
  Navigator.of(context).push(router);

它可以正常工作,但是会创建新的UserBloc初始状态。 我想要的是在UserBlog上获得当前状态。

我尝试了this

UserBloc userBloc = BlocProvider.of<UserBloc>(context);
BlocProvider<CounterBloc>(
  bloc: userBloc,
  child: SecondScreen()
 )

bloc提供了一个错误。 有什么方法可以导航到SecondScreen并具有相同的状态。

2 个答案:

答案 0 :(得分:4)

每次调用BlocProvider时,都会创建bloc类的新实例。

要使用相同的bloc实例,您需要先创建它,然后再进行路由,然后再通过上下文对其进行处理。

import 'package:bloc/bloc.dart';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<BlocSample>(
          create: (BuildContext context) => BlocSample(),
        ),
      ],
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: FirstScreen(),
      ),
    );
  }
}

class BlocSample extends Bloc<EventSample, String> {
  @override
  String get initialState => 'initial state';

  @override
  Stream<String> mapEventToState(
    EventSample event,
  ) async* {
    yield 'changed state';
  }
}

class EventSample {}

class FirstScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<BlocSample, String>(
      builder: (context, state) {
        return Scaffold(
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(state),
                RaisedButton(
                  child: Text('change status'),
                  onPressed: () => BlocProvider.of<BlocSample>(context).add(
                    EventSample(),
                  ),
                ),
                RaisedButton(
                  child: Text('change route'),
                  onPressed: () => Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (_) => SecondScreen(),
                    ),
                  ),
                )
              ],
            ),
          ),
        );
      },
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<BlocSample, String>(
      builder: (context, state) {
        return Scaffold(
          body: Center(child: Text(state)),
        );
      },
    );
  }
}

enter image description here

答案 1 :(得分:0)

请检查一次此博文https://blog.geekyants.com/state-management-in-flutter-7df833e6f3bd。它解释了使用bloc模式的清晰过程,而没有任何插件支持。如果您需要更多帮助,请告诉我。