Flutter Bloc状态成功状态未唤醒

时间:2019-09-12 11:51:37

标签: flutter bloc

当state.success为NotEmpty时,我需要弹出对话框或Card

成功状态为空时,它返回我在代码中设置的文本 我不知道Widget错误还是其他

我需要帮助

import 'package:bloc_salesearch/src/screen/page_settings.dart';
import 'package:bloc_salesearch/src/search_bloc/bloc.dart';
import 'package:bloc_salesearch/src/util/utils.dart';
import 'package:bloc_salesearch/src/widget/utils_widget.dart';
import 'package:flutter/material.dart';
import 'package:bloc_salesearch/src/widget/widgets.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../src.dart';

class SearchPage extends StatefulWidget {
  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  Screen size;

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    size = Screen(MediaQuery.of(context).size);
    return Scaffold(
      backgroundColor: backgroundColor,
      body: AnnotatedRegion(
        value: SystemUiOverlayStyle(
          statusBarColor: backgroundColor,
          statusBarBrightness: Brightness.dark,
          statusBarIconBrightness: Brightness.dark,
          systemNavigationBarColor: backgroundColor,
          systemNavigationBarIconBrightness: Brightness.dark,
        ),
        child: Container(
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                upperPart(),


              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget upperPart() {
    return Stack(
      children: <Widget>[
        ClipPath(
          clipper: UpperClipper(),
          child: Container(
            height: size.getWidthPx(240),
            decoration: BoxDecoration(
              gradient:
                  LinearGradient(colors: [colorCurve, colorCurveSecondary]),
            ),
          ),
        ),
        Column(
          children: <Widget>[
            Container(
              padding: EdgeInsets.only(top: size.getWidthPx(36)),
              child: Column(
                children: <Widget>[
                  titleWidget(),
                  SizedBox(
                    height: size.getWidthPx(10),
                  ),
                  upperBoxCard(),
                  SizedBox(
                    height: size.getWidthPx(10),
                  ),
                  SearchResult(),
                ],
              ),
            ),
          ],
        ),
      ],
    );
  }

  Text titleWidget() {
    return Text("Search Lottery",
        style: TextStyle(
//          fontFamily: 'Exo2',
            fontSize: 24.0,
            fontWeight: FontWeight.w900,
            color: Colors.white));
  }

  Card upperBoxCard() {
    return Card(
        elevation: 4.0,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        margin: EdgeInsets.symmetric(
            horizontal: size.getWidthPx(20), vertical: size.getWidthPx(16)),
        borderOnForeground: true,
        child: Container(
          height: size.getWidthPx(150),
          child: Column(
            children: <Widget>[
              SearchWidget(),
              leftAlignText(
                  text: "Your Number foo win :",
                  leftPadding: size.getWidthPx(16),
                  textColor: textPrimaryColor,
                  fontSize: 16.0),

              rightAlignText(
                  text: '150000',
                  rightPadding: size.getWidthPx(16),
                  textColor: textPrimaryColor,
                  fontSize: 16.0)
            ],
          ),
        ));
  }

//
}

class SearchResult extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<SearchBloc, SearchState>(
      builder: (context, state) {
        if (state is SearchStateLoading) {
          return Container(
            height: 40,
            child: Center(
              child: CircularProgressIndicator(),
            ),
          );
        }
        if (state is SearchStateError) {
          return Text(state.error);
        }
        if (state is SearchStateSuccess) {
          print(state);


          return state.items.isEmpty
              ? Text('No Found!Good Luck Next Month!')
              : Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=> SettingPage()));
        }
        return Text('Please Type your Lottery Number To begin');
      },
    );
  }
}

class _SearchResult extends StatelessWidget {
  final List<Sale> items;

  const _SearchResult({Key key, this.items}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: items.length,
      itemBuilder: (BuildContext context, int index) {

        return _SearchView(item: items[index]);
      },
    );
  }
}

class _SearchView extends StatelessWidget {

  final Sale item;

  const _SearchView({Key key, this.item}) : super(key: key);



  @override
  Widget build(BuildContext context) {
    return Container(child: Text(''
        'You Win'),);
  }
}

class SearchWidget extends StatefulWidget {
  @override
  _SearchWidgetState createState() => _SearchWidgetState();
}

class _SearchWidgetState extends State<SearchWidget> {
  final texEdc = TextEditingController();
  SearchBloc _searchBloc;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _searchBloc = BlocProvider.of<SearchBloc>(context);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    texEdc.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: texEdc,
      autocorrect: false,
      onChanged: (text) {
        _searchBloc.dispatch(
          TextChanged(text: text),
        );
      },
      decoration: InputDecoration(
        prefixIcon: Icon(Icons.search),
        suffixIcon: GestureDetector(
          child: Icon(Icons.clear),
          onTap: _onClearTapped,
        ),
        border: InputBorder.none,
        hintText: 'Enter your Lottery number',
      ),
    );
  }

  void _onClearTapped() {
    texEdc.text = '';
    _searchBloc.dispatch(TextChanged(text: ''));
  }
}

1 个答案:

答案 0 :(得分:0)

您可以使用BlocListener监听Bloc状态,并根据不同的状态执行操作。 您应该将小部件或Bloc构建器包装到BlocListener中,如下所示:

BlocListener(
  bloc: _searchBloc,
  listener: (BuildContext context, SearchState state) {
    if(state is SearchStateSuccess){
      Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=> SettingPage()));
    }
  },
  child: your_blocBuilder
)