Created
April 11, 2020 09:11
-
-
Save vijayinyoutube/3f7721e1fe404782d16a4f7a8fa4a37e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:bloc/bloc.dart'; | |
import 'package:equatable/equatable.dart'; | |
part 'mybloc_event.dart'; | |
part 'mybloc_state.dart'; | |
class MyblocBloc extends Bloc<MyblocEvent, MyblocState> { | |
@override | |
MyblocState get initialState => MyblocInitial(); | |
@override | |
Stream<MyblocState> mapEventToState( | |
MyblocEvent event, | |
) async* { | |
if (event is SubmitForm) { | |
yield FormLoading(); | |
yield FormLoaded(); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
part of 'mybloc_bloc.dart'; | |
abstract class MyblocEvent extends Equatable { | |
const MyblocEvent(); | |
} | |
class SubmitForm extends MyblocEvent { | |
@override | |
// TODO: implement props | |
List<Object> get props => null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'MyPage2.dart'; | |
import 'bloc/mybloc_bloc.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: MyPage(), | |
); | |
} | |
} | |
class MyPage extends StatefulWidget { | |
@override | |
_MyPageState createState() => _MyPageState(); | |
} | |
class _MyPageState extends State<MyPage> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Container( | |
child: BlocProvider<MyblocBloc>( | |
child: BlocConsumer<MyblocBloc, MyblocState>( | |
listener: (context, state) { | |
Navigator.push( | |
context, MaterialPageRoute(builder: (context) => MyPage2())); | |
}, | |
builder: (context, state) { | |
if (state is MyblocInitial) { | |
return buildInitialInput(context); | |
} else if (state is FormLoading) { | |
return loadingwidget(); | |
} | |
return loadingwidget(); | |
}, | |
), | |
create: (BuildContext context) { | |
return MyblocBloc(); | |
}, | |
), | |
), | |
); | |
} | |
Widget loadingwidget() { | |
print("=================inside loading State=================="); | |
return Center( | |
child: Padding( | |
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 100), | |
child: CircularProgressIndicator()), | |
); | |
} | |
} | |
Widget buildInitialInput(BuildContext context) { | |
return Column( | |
children: <Widget>[ | |
SizedBox(height: 50), | |
TextFormField( | |
decoration: InputDecoration( | |
labelText: "Name", | |
hintStyle: TextStyle( | |
color: Colors.grey, | |
fontSize: 16.0, | |
), | |
), | |
), | |
TextFormField( | |
decoration: InputDecoration( | |
labelText: "City", | |
hintStyle: TextStyle( | |
color: Colors.grey, | |
fontSize: 16.0, | |
), | |
), | |
), | |
SizedBox(height: 25), | |
FlatButton( | |
color: Colors.blueAccent, | |
onPressed: () { | |
BlocProvider.of<MyblocBloc>(context).add(SubmitForm()); | |
}, | |
child: const Text( | |
'Submit', | |
style: TextStyle(fontSize: 20, color: Colors.white), | |
), | |
shape: new RoundedRectangleBorder( | |
borderRadius: new BorderRadius.circular(7.5)), | |
), | |
], | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class MyPage2 extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: Text("\n\n\n\t\t\tWelcome"), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
part of 'mybloc_bloc.dart'; | |
abstract class MyblocState extends Equatable { | |
const MyblocState(); | |
} | |
class MyblocInitial extends MyblocState { | |
List<Object> get props => []; | |
} | |
class FormLoading extends MyblocState { | |
@override | |
// TODO: implement props | |
List<Object> get props => null; | |
} | |
class FormLoaded extends MyblocState { | |
@override | |
// TODO: implement props | |
List<Object> get props => null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment