我正在尝试在register_page上实现BloC模式,但是当我尝试调用上下文时,似乎provider.bloc
返回了null
。在这里尝试了一些建议stackoverflow但没有运气。任何人都想告诉我代码中的问题是什么?见下文:
bloc_provider.dart
class BlocProvider<T extends BlocBase> extends StatefulWidget {
final T bloc;
final Widget child;
static Type _typeOf<T>() => T;
BlocProvider({Key key, @required this.child, this.bloc}) : super(key: key);
@override
_BlocProviderState<T> createState() => _BlocProviderState();
static T of<T extends BlocBase>(BuildContext context) {
final type = _typeOf<BlocProvider<T>>();
BlocProvider<T> provider = context.ancestorWidgetOfExactType(type);
return provider.bloc;
}
}
register_page.dart
Widget _registerButton(BuildContext context) {
final UserBloc userBloc = BlocProvider.of<UserBloc>(context); // having an issue in this line
return Container(
height: 50,
width: MediaQuery.of(context).size.width / 1.1,
child: BlocProvider(
bloc: UserBloc(),
child: RaisedButton(
child: Container(
alignment: Alignment.center,
height: 50,
width: MediaQuery.of(context).size.width,
child: Text(
'register'.toUpperCase(),
style: TextStyle(
color: getColor(ColorList.WhiteCream, 1.0),
fontSize: 20,
),
),
),
onPressed: () {},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(60),
side: BorderSide(
color: getColor(ColorList.WhiteCream, 1.0),
width: 2.0,
),
),
color: getColor(ColorList.DarkGreen, 1.0),
),
),
);
}
其他部分
@override
Widget build(BuildContext context) {
return BlocProvider(
bloc: UserBloc(),
child: Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(50.0),
child: AppBar(
elevation: 0.0,
backgroundColor: getColor(ColorList.DarkGreen, 1.0),
),
),
body: Column(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height / 6,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
getColor(ColorList.DarkGreen, 1.0),
getColor(ColorList.LightGreen, 1.0),
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(90),
),
),
child: Stack(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 40.0, bottom: 15.0),
child: Container(
alignment: Alignment.center,
width: 100,
height: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: getColor(ColorList.WhiteCream, 1.0),
width: 2.0)),
margin: const EdgeInsets.only(top: 32.0),
padding: const EdgeInsets.all(3.0),
child: Container(
height: 70,
width: 70,
child: ClipOval(
child: Image.asset(
'assets/images/log-ayuda-black.png',
color: getColor(ColorList.WhiteCream, 1.0),
),
),
),
),
),
Align(
alignment: FractionalOffset.bottomRight,
child: Padding(
padding: EdgeInsets.only(
right: 32,
bottom: 15.0,
),
child: Text(
'Register'.toUpperCase(),
style: TextStyle(
fontFamily: 'OpenSans',
color: getColor(ColorList.WhiteCream, 1.0),
fontSize: 25,
fontWeight: FontWeight.bold),
),
),
),
],
),
),
SizedBox(
height: 10,
),
Container(
height: MediaQuery.of(context).size.height / 1.41,
width: MediaQuery.of(context).size.width,
child: SingleChildScrollView(
padding: EdgeInsets.only(top: 20.0),
physics: const ClampingScrollPhysics(),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// username
_usernameTextField(context),
_paddingSpace(usernameHasError),
// password
_passwordTextField(context),
_paddingSpace(passwordHasError),
// email address
_emailAddressTextField(context),
_paddingSpace(emailAddressHasError),
// Contact Number
_contactNumberTextField(context),
_paddingSpace(contactNumberHasError),
// First name
_firstNameTextField(context),
_paddingSpace(firstNameHasError),
// middle name
_middleNameTextField(context),
_paddingSpace(false),
// last name
_lastNameTextField(context),
_paddingSpace(lastNameHasError),
// gender
_buildGenderRadioButton(context),
_paddingSpace(selectGenderHasError),
// birthdate
_buildDateTimePicker(context),
selectBirthDateHasError
? Padding(
padding: EdgeInsets.only(top: 10, bottom: 10),
child: Text(
'You must be 18 years old and above',
style: TextStyle(color: Colors.redAccent),
),
)
: _paddingSpace(false),
// register button
_registerButton(widget.context),
_paddingSpace(false),
].where(notNull).toList(),
),
),
),
],
),
),
);
}
这可能是我在MaterialPageRoute
上的Navigator.push
函数中的问题。请参阅下面的代码进行导航:
Container(
height: 60,
width: MediaQuery.of(context).size.width,
padding: const EdgeInsets.only(left: 16.0, right: 16.0),
child: FlatButton(
child: Text(
'Not yet a member? Sign up now!',
style: TextStyle(
fontFamily: 'OpenSans',
fontSize: 18.0,
color: getColor(ColorList.WhiteCream, 1.0),
fontWeight: FontWeight.bold,
),
),
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
color: Colors.transparent,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => RegisterPage(
context: context,
)));
},
),
),
我相信我在将Scaffold
和BlocProvider
包装在 register_page.dart 下的正确方法...我不不知道为什么它仍然返回null
值。