Snakbar 在一个事件中出现两次

问题描述

我正在练习颤动肘。通过全局声明cubit实例的路由访问方法。 当我单击主屏幕中的增量或减量时,snakbar 会出现一次,但是当我导航到第二屏时,它会在一个事件中出现两次。这是我的主文件

import 'package:bloc_counter_app/presentation/screens/home_screen.dart';

import 'business_logic/cubit/counter_cubit.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 BlocProvider<CounterCubit>(
      lazy: false,create: (context) => CounterCubit(),child: MaterialApp(
        title: 'bloc counter app',theme: ThemeData(primarySwatch: Colors.red),home: HomePage(),),);
  }
}

这是主屏幕

import 'package:bloc_counter_app/business_logic/cubit/counter_cubit.dart';
import 'package:bloc_counter_app/presentation/screens/second_screen.dart';
import 'package:Flutter/material.dart';
import 'package:Flutter_bloc/Flutter_bloc.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Bloc Counter App'),body: Column(
        mainAxisAlignment: MainAxisAlignment.center,children: [
          BlocConsumer<CounterCubit,CounterState>(listener: (context,state) {
            if (state.wasIncremented == false) {
              ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                content: Text('Incremented'),duration: Duration(milliseconds: 1),));
            } else if (state.wasIncremented == true) {
              ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                content: Text('Decremented'),));
            }
          },builder: (context,state) {
            return Text(
              'The value is ${state.counterValue}',style: TextStyle(
                fontSize: 20.0,);
          }),SizedBox(
            height: 50.0,Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,children: [
              FloatingActionButton(
                  tooltip: 'Increment',child: Icon(Icons.add),onpressed: () {
                    BlocProvider.of<CounterCubit>(context).increment();
                  }),FloatingActionButton(
                  tooltip: 'decrement',child: Icon(Icons.remove),onpressed: () =>
                      BlocProvider.of<CounterCubit>(context).decrement())
            ],SizedBox(
            height: 24.0,MaterialButton(
              color: Colors.blue,child: Text('Second Screen'),onpressed: () => Navigator.of(context)
                  .push(MaterialPageRoute(builder: (_) => SecondScreen()))),],);
  }
}

这是第二个屏幕。

import 'package:bloc_counter_app/business_logic/cubit/counter_cubit.dart';
import 'package:Flutter/material.dart';
import 'package:Flutter_bloc/Flutter_bloc.dart';

class SecondScreen extends StatefulWidget {
  @override
  _SecondScreenState createState() => _SecondScreenState();
}

class _SecondScreenState extends State<SecondScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Bloc Counter App'),CounterState>(
              listener: (context,state) {
                if (state.wasIncremented == false) {
                  ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                    content: Text('Incremented'),));
                } else if (state.wasIncremented == true) {
                  ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                    content: Text('Decremented'),));
                }
              },)
        ],);
  }
}


 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)