flutter 处理带模态底层的BlocProvider

mjqavswn  于 2023-03-19  发布在  Flutter
关注(0)|答案(1)|浏览(102)

我在Bloc Provider中有一个带有手势检测器的状态控件。

@override
  Widget build(BuildContext context) {
    return RepositoryProvider.value(
      value: _authRepository,
      child: BlocProvider(
        create: (context) => AuthBloc(authRepository: AuthRepository()),
        child: Scaffold(
          body: GestureDetector(
            onTap: () {
              showSignInBottomSheet(context);
            },
          ),
        ),
      ),
    );
  }
}

在点击我想显示一个模态bottom表,我希望可以访问我以前创建的集团。
有人告诉我,应该用BlocProvider.value Package 模态构建器,以达到这个目的。

showSignInBottomSheet(BuildContext context) => showModalBottomSheet(
    context: context,
    isScrollControlled: true,
    backgroundColor: Colors.white,
    shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.vertical(top: Radius.circular(0.0)),
    ),
    builder: (context) {
      return BlocProvider.value(
          value: BlocProvider.of<AuthBloc>(context, listen: false),
          child: Container(
              padding: const EdgeInsets.only(top: 40),
              height: MediaQuery.of(context).size.height,
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 15.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        springWidget(() {
                          Navigator.of(context).pop();
                        },
                            const Icon(
                              CupertinoIcons.xmark,
                              size: 20,
                            )),
                      ],
                    ),
                  ),
                  Expanded(
                      child: Container(
                    color: Colors.red,
                  )),
                ],
              )));
    });

但我仍然得到这个错误。

BlocProvider.of() called with a context that does not contain a AuthBloc.

        No ancestor could be found starting from the context that was passed to BlocProvider.of<AuthBloc>().

我对集团国家管理这件事还不熟悉,所以我真的不知道出了什么问题.
请在我被淹没之前帮帮我。

6jygbczu

6jygbczu1#

重命名showModalBottomSheet中的构建器上下文,以便您的BlocProvider使用正确的上下文。

builder: (context) {
      return BlocProvider.value(
          value: BlocProvider.of<AuthBloc>(context, listen: false),

收件人:

builder: (innerContext) {
      return BlocProvider.value(
          value: BlocProvider.of<AuthBloc>(context, listen: false),

下面是一个完整的工作示例:

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo',
      home: BlocProvider(
        create: (context) => DemoCubit(),
        child: const FirstPage(),
      ),
    );
  }
}

class FirstPage extends StatelessWidget {
  const FirstPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('SO question 75774451'),
      ),
      body: Center(
        child: ElevatedButton(
            onPressed: () => showModalBottomSheet(
                  context: context,
                  builder: (innerContext) => BlocProvider.value(
                    value: context.read<DemoCubit>(),
                    child: Column(
                      children: [
                        BlocBuilder<DemoCubit, int>(
                          builder: (context, state) {
                            return Text('Current state: $state');
                          },
                        ),
                        ElevatedButton(
                          onPressed: () => context.read<DemoCubit>().inc(),
                          child: const Text('Press me'),
                        )
                      ],
                    ),
                  ),
                ),
            child: Text('Show modal - Current state: ${context.watch<DemoCubit>().state}')),
      ),
    );
  }
}

class DemoCubit extends Cubit<int> {
  DemoCubit() : super(0);
  void inc() => emit(state + 1);
}

相关问题