Skip to content

Instantly share code, notes, and snippets.

@arifai
Created September 17, 2019 15:42
Show Gist options
  • Save arifai/8ecf53712566ab61ae3b837f952f5bb2 to your computer and use it in GitHub Desktop.
Save arifai/8ecf53712566ab61ae3b837f952f5bb2 to your computer and use it in GitHub Desktop.
The getter 'state' was called on null.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pdm_mobile/blocs/account/account_bloc.dart';
import 'package:pdm_mobile/blocs/account/account_event.dart';
import 'package:pdm_mobile/blocs/account/account_state.dart';
import 'package:pdm_mobile/blocs/territory/territory.dart';
import 'package:pdm_mobile/models/account.dart';
import 'package:pdm_mobile/util/error_util.dart';
import 'package:pdm_mobile/widgets/exception/exception_info_view.dart';
import 'package:pdm_mobile/widgets/territory/territory_dropdown_view.dart';
class ChangeAddressForm extends StatefulWidget {
final AccountBloc accountBloc;
final Account account;
ChangeAddressForm(this.accountBloc, this.account, {Key key})
: super(key: key);
@override
State<ChangeAddressForm> createState() =>
_ChangeAddressWidgetState(accountBloc, account);
}
class _ChangeAddressWidgetState extends State<ChangeAddressForm> {
final _formKey = GlobalKey<FormState>();
final _addressController = TextEditingController();
final _nameController = TextEditingController();
final _phoneController = TextEditingController();
final _provinceController = TextEditingController();
final _regencyController = TextEditingController();
final _subDistrictController = TextEditingController();
final _villageController = TextEditingController();
final AccountBloc accountBloc;
Account account;
String _errorInfo = "";
bool _isFieldError = false;
bool _inProgress = false;
StreamSubscription subs;
Map<String, String> territory = Map();
_ChangeAddressWidgetState(this.accountBloc, this.account) {
subs = accountBloc.state.listen((AccountState state) {
print("Print state =>> $state");
if (state is AccountUpdated) {
Navigator.pop(context, true);
} else if (state is AddressFieldError) {
print("AddressFieldError");
setState(() {
_errorInfo = state.error;
_isFieldError = true;
});
} else if (state is AddressFailure) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(state.error),
backgroundColor: Colors.red,
),
);
}
});
}
@override
void dispose() {
super.dispose();
subs.cancel();
}
@override
Widget build(BuildContext context) {
disableApiErrorHandler();
_onAddressButtonPressed() {
if (_formKey.currentState.validate()) {
accountBloc.dispatch(UpdateAddress(
name: _nameController.text,
address: _addressController.text,
phone: _phoneController.text,
province: _provinceController.text,
district: _regencyController.text,
subDistrict: _subDistrictController.text,
village: _villageController.text,
));
}
}
return BlocBuilder<TerritoryBloc, TerritoryState>(
builder: (context, state) {
return Center(
child: ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
TerritoryDropdownView(
onSelectTerritory: (value, type) {
switch (type) {
case TerritoryType.province:
_provinceController.text = value.name;
break;
case TerritoryType.district:
_regencyController.text = value.name;
break;
case TerritoryType.subDistrict:
_subDistrictController.text = value.name;
break;
case TerritoryType.village:
_villageController.text = value.name;
}
},
territory: territory ?? Map(),
),
TextFormField(
decoration: InputDecoration(labelText: "Nama Lengkap:"),
autofocus: true,
controller: _nameController,
),
TextFormField(
decoration: InputDecoration(labelText: "Alamat:"),
autofocus: true,
controller: _addressController,
),
TextFormField(
decoration: InputDecoration(labelText: "Nomor Ponsel:"),
autofocus: true,
controller: _phoneController,
),
Container(
margin: EdgeInsets.only(top: 16.0, bottom: 16.0),
child: Visibility(
visible: _isFieldError,
child: ExceptionInfoView(
title: "Terjadi kesalahan",
info: _errorInfo,
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 30.0, bottom: 20.0),
child: ButtonTheme(
// minWidth: MediaQuery.of(context).size.width,
child: RaisedButton(
onPressed: !_inProgress
? _onAddressButtonPressed()
: null,
child: new Text(
"Batalkan",
style: TextStyle(color: Colors.grey),
),
shape: new RoundedRectangleBorder(
side: BorderSide(color: Colors.grey)),
color: Colors.white),
),
),
Padding(
padding: EdgeInsets.only(top: 30.0, bottom: 20.0),
child: ButtonTheme(
// minWidth: MediaQuery.of(context).size.width,
child: RaisedButton(
onPressed: () =>
Navigator.of(context).push(null),
child: new Text(
"Simpan",
style: TextStyle(color: Colors.white),
),
shape: new RoundedRectangleBorder(
side: BorderSide(color: Color(0xff039fc0))),
color: Color(0xff039fc0),
),
),
),
],
),
],
),
),
),
],
),
);
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment