the current state in mapEventToState in Flutter Bloc is not yield - android-studio

I am trying to make a simple app for testing the BloC pattern but, there's a change in the current version of the BloC Pattern, that the "Current State" now not sending with the mapEventToState method as a parameter but according to the online document you can replace it with only "state", but it not working with me,
and this is my code:
class CounterBloc extends Bloc<CounterEvent, CounterState> {
void onIncrement(){
add(IncrementEvent());
}
void onDecrement() {
add(DecrementEvent());
}
#override
// TODO: implement initialState
CounterState get initialState => CounterState.initial();
#override
Stream<CounterState> mapEventToState(CounterEvent event) async* {
if (event is IncrementEvent) {
yield state..counter += 1;
} else if (event is DecrementEvent) {
yield state..counter -= 1;
}
}
}
and this is the Counter state class
class CounterState {
int counter;
CounterState._();
factory CounterState.initial() {
return CounterState._()..counter = 0;
}
}
and this is my main app dart file
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => CounterBloc(),
child: CounterWidget(widget: widget),
);
}
}
class CounterWidget extends StatelessWidget {
final MyHomePage widget;
const CounterWidget({Key key, #required this.widget}) :super (key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: BlocBuilder(
bloc: BlocProvider.of<CounterBloc>(context),
builder: (context, CounterState state) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'${state.counter}',
style: Theme.of(context).textTheme.display1,
),
],
),
);
},
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed: () => BlocProvider.of<CounterBloc>(context).onIncrement(),
tooltip: 'Increment',
child: Icon(Icons.add),
),
SizedBox(width: 10,),
FloatingActionButton(
onPressed: () => BlocProvider.of<CounterBloc>(context).onDecrement(),
tooltip: 'Decrement',
child: Icon(Icons.remove),
),
],
)
);
}
}
please can anyone help me with this issue?

You can copy paste run full code below
Step 1 : add CounterState({this.counter}); in CounterState
Step 2 : yield state..counter += 1; will not cause Widget build , please change to yield CounterState(counter: state.counter + 1);
working demo
full code
import 'package:flutter/material.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
abstract class CounterEvent {}
class IncrementEvent extends CounterEvent {}
class DecrementEvent extends CounterEvent {}
void main() => runApp(MyApp());
class CounterState {
int counter;
CounterState._();
CounterState({this.counter}); //add this line
factory CounterState.initial() {
return CounterState._()..counter = 0;
}
}
class CounterBloc extends Bloc<CounterEvent, CounterState> {
void onIncrement() {
add(IncrementEvent());
}
void onDecrement() {
add(DecrementEvent());
}
#override
// TODO: implement initialState
CounterState get initialState => CounterState.initial();
#override
Stream<CounterState> mapEventToState(CounterEvent event) async* {
if (event is IncrementEvent) {
//yield state..counter += 1;
yield CounterState(counter: state.counter + 1);
} else if (event is DecrementEvent) {
//yield state..counter -= 1;
yield CounterState(counter: state.counter - 1);
}
}
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => CounterBloc(),
child: CounterWidget(widget: widget),
);
}
}
class CounterWidget extends StatelessWidget {
final MyHomePage widget;
const CounterWidget({Key key, #required this.widget}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: BlocBuilder(
bloc: BlocProvider.of<CounterBloc>(context),
builder: (context, CounterState state) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'${state.counter}',
style: Theme.of(context).textTheme.display1,
),
],
),
);
},
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed: () =>
BlocProvider.of<CounterBloc>(context).onIncrement(),
tooltip: 'Increment',
child: Icon(Icons.add),
),
SizedBox(
width: 10,
),
FloatingActionButton(
onPressed: () =>
BlocProvider.of<CounterBloc>(context).onDecrement(),
tooltip: 'Decrement',
child: Icon(Icons.remove),
),
],
));
}
}

Related

There is no error but favor page does not appear

the favor page does not apper , no errors but it does not work , i do the same for the top app bar and it works , please advice me and thanks . ( i add the 2 pages ,categories ,favorites , sorry i add more words to solve the question issues ........................................................................ )
import 'package:flutter/material.dart';
import 'package:meals/categeory_screens.dart';
import 'favorties.dart';
class TabsScreen extends StatefulWidget {
TabsScreen({Key? key}) : super(key: key);
#override
State<TabsScreen> createState() => _TabsScreenState();
}
class _TabsScreenState extends State<TabsScreen> {
final List _pages=[
CategoreyScreen(),
FavortiesScreen(),
];
int _select =0;
void _selectedPage(int index){
setState((){
index=_select;
});}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title:const Text('meals'),),
body:_pages[_select],
bottomNavigationBar:
BottomNavigationBar(
onTap: _selectedPage,
selectedItemColor: Theme.of(context).bottomAppBarColor,
unselectedItemColor: Colors.white,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.category),
label: 'categories'),
BottomNavigationBarItem( icon: Icon(Icons.star),
label: 'favor'),
],
backgroundColor: Theme.of(context).primaryColor,
),
);
}
}
Try this code.
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> with
WidgetsBindingObserver {
int _selectedTabIndex = 0;
_changeIndex(int index) {
setState(() {
_selectedTabIndex = index;
print("index..." + index.toString());
});
}
List _pages = [
CategoriesScreen(),
FavorScreen(),
];
#override
void initState() {
WidgetsBinding.instance!.addObserver(this);
super.initState();
}
#override
void dispose() {
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(),
bottomNavigationBar: bottomNavigationBar,
),
);
}
Widget get bottomNavigationBar {
return Container(
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),
),
child: BottomNavigationBar(
currentIndex: _selectedTabIndex,
onTap: _changeIndex,
type: BottomNavigationBarType.fixed,
selectedFontSize: 12,
unselectedFontSize: 12,
selectedItemColor: Colors.appColor,
unselectedItemColor: Colors.grey[500],
showUnselectedLabels: true,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: _selectedTabIndex == 0
? Icon(
Icons.local_mall,
size: 22.0,
color: Colors.appColor,
)
: Icon(
Icons.local_mall_outlined,
size: 22.0,
color: Colors.colorBlack100,
),
label: "Categories",
),
BottomNavigationBarItem(
icon: _selectedTabIndex == 1
? Icon(
Icons.dashboard,
size: 22.0,
color: Colors.appColor,
)
: Icon(
Icons.dashboard_outlined,
size: 22.0,
color: Colors.colorBlack100,
),
label: "Favor",
),
],
),
));
}
}

Flutter how to display string in Flutter Application(Android Studio Emulator)

I have a problem with displaying the string output in Flutter App, I wanted to display the string value in column like this:
I
want
to
split
this
but instead I got this output:
[I, want, to, split, this]
I don't know what to do anymore as I am still new to programming, but I think this code below must be the cause:
void _splitWordInColumn(){
setState(() {
sentenceToWord.forEach((e) => print(e));
});
}
This is the image of that wrong output and below is my full code:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<String> sentenceToWord = 'I want to split this'.split(" ");
void _splitWordInColumn(){
setState(() {
sentenceToWord.forEach((e) => print(e));
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo HomePage'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(sentenceToWord.toString()),
],
),
),
);
}
}
I hope someone can help me fix this, Thank you.
Try to use map operator with the column.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: sentenceToWord.map((e) => Text(e)).toList(),
),
Try this :
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String sentenceToWord = 'I \nwant \nto \nsplit \nthis';
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo HomePage'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(sentenceToWord),
],
),
),
);
}
}

How to hide/show a button with another button | Flutter?

New to programming and dart/flutter .
Thank You.
So 2 buttons Me! and You! , I have to hide and show me! button clicking on You! button .
So can any one help me to find solution of my question.
and what if i have more numbers of buttons and show/hide all of them using a single button.
My code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Container(
child: SafeArea(
child: Column(
children: [
MaterialButton(
onPressed: () {},
child: Text('Me!'),
color: Colors.green,
),
MaterialButton(
onPressed: () {},
child: Text('You!'),
color: Colors.red,
)
],
),
),
);
}
}
use a Visibility Widget.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool isVisible = true; //will be visible for the first frame
#override
Widget build(BuildContext context) {
return Container(
child: SafeArea(
child: Column(
children: [
Visibility(
visible: isVisible,
child: MaterialButton(
onPressed: () {},
child: Text('Me!'),
color: Colors.green,
),
),
MaterialButton(
onPressed: () {
setState(() {
isVisible = !isVisible;
});
},
child: Text('You!'),
color: Colors.red,
)
],
),
),
);
}
}
Here are 3 examples.
1
class _MyHomePageState extends State<MyHomePage> {
bool hide = false;
#override
Widget build(BuildContext context) {
return Container(
child: SafeArea(
child: Column(
children: [
if(!hide)MaterialButton(
onPressed: () {},
child: Text('Me!'),
color: Colors.green,
),
MaterialButton(
onPressed: () {
setState((){
hide = !hide;
});
},
child: Text('${hide ? "Show" : "Hide"}'),
color: Colors.red,
)
],
),
),
);
}
}
2
class _MyHomePageState extends State<MyHomePage> {
bool hide = false;
#override
Widget build(BuildContext context) {
return Container(
child: SafeArea(
child: Column(
children: [
Opacity(
opacity: hide ? 0 : 1,
child: MaterialButton(
onPressed: () {},
child: Text('Me!'),
color: Colors.green,
)
),
MaterialButton(
onPressed: () {
setState((){
hide = !hide;
});
},
child: Text('${hide ? "Show" : "Hide"}'),
color: Colors.red,
)
],
),
),
);
}
}
3 (Adding animation)
class _MyHomePageState extends State<MyHomePage> {
bool hide = false;
#override
Widget build(BuildContext context) {
return Container(
child: SafeArea(
child: Column(
children: [
AnimatedOpacity(
opacity: hide ? 0 : 1,
duration: Duration(seconds: 2),
child: MaterialButton(
onPressed: () {},
child: Text('Me!'),
color: Colors.green,
)
),
MaterialButton(
onPressed: () {
setState((){
hide = !hide;
});
},
child: Text('${hide ? "Show" : "Hide"}'),
color: Colors.red,
)
],
),
),
);
}
}
Note: The first example will remove the button from the widget tree. For the second and third, the button will be in the widget tree but won't be visible.
So you can see it as:
First example: The button is GONE.
Second example: The button is INVISIBLE.

Navigator operation requested with a context that does not include a Navigator. (Builder function wont display MainPage())

As soon as I press the button I get the error code "Navigator operation requested with a context that does not include a Navigator." Code im using to navigate is in the RegisterPage and by the builder function should be displaying the MainPage.
void main() => runApp(RegisterPage());
class RegisterPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Register Your Account'),
centerTitle: true,
backgroundColor: Colors.green,
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.of(context)
.push(
MaterialPageRoute(
builder: (context) => MainPage()
),
);
},
child: Text('Sign in as guest'),
)),
)
);
}
}
class MainPage extends StatefulWidget{
String get title => "Cykla i stockholm";
MapPage createState()=> MapPage();
}
That's because the widget which uses the navigator (RegisterPage) is at the same level in the widget tree with the widget which creates the navigator(MaterialApp)
SOLUTION:
make the RegisterPage below MaterialApp in order to be able to use its context:
class RegisterPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Register Your Account'),
centerTitle: true,
backgroundColor: Colors.green,
),
body: Builder(
builder: (ctx)=> Center(//this context here has access to Navigator
child: RaisedButton(
onPressed: () {
Navigator.of(ctx)
.push(
MaterialPageRoute(
builder: (context) => MainPage()
),
);
},
child: Text('Sign in as guest'),
)),
),
)
);
}
}

How to make widget stick to top of keyboard

I'd like to make a widget that sticks to the bottom of the page, and then is pinned to the top of the keyboard (when it appears).
Note how the input textfield is pinned to the keyboard in the image below:
How would I do this? I tried putting it in the bottomNavigationBar, but this (obviously) didn't work. Is there a builtin way to do this?
This is a working example of the thing you want. I think!
Just copy/paste/run
What's important in this example is the Expanded. A really nice widget that expands to as much space as it can get. And in result pushing the chat box down as much as possible
(Bottom of the screen or bottom of the keyboard)
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('49715760 Stackoverflow'),
),
body: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Expanded(
child: new Material(
color: Colors.red,
child: new Text("Filled"),
),
),
new Container(
color: Colors.white,
padding: new EdgeInsets.all(10.0),
child: new TextField(
decoration: new InputDecoration(
hintText: 'Chat message',
),
),
),
],
),
);
}
}
The best way to resolve this is to use a dedicated widget.
MediaQuery.of(context).viewInsets.bottom will give you the value of the height covered by the system UI(in this case the keyboard).
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
var home = MyHomePage(title: 'Flutter Demo Home Page');
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: home,
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text(widget.title),
),
body: _getBody(),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget _getBody() {
return Stack(children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/sample.jpg"), fit: BoxFit.fitWidth)),
// color: Color.fromARGB(50, 200, 50, 20),
child: Column(
children: <Widget>[TextField()],
),
),
Positioned(
bottom: MediaQuery.of(context).viewInsets.bottom,
left: 0,
right: 0,
child: Container(
height: 50,
child: Text("Hiiiii"),
decoration: BoxDecoration(color: Colors.pink),
),
),
]);
}
}
there is a lib for that:
https://pub.dev/packages/keyboard_attachable
Widget build(BuildContext context) => FooterLayout(
footer: MyFooterWidget(),
child: PageMainContent(),
);
use of bottomSheet option from Scaffold.
Scaffold(
bottomSheet: chatBar(),
body: Column(
children: [
Expanded(
child: ListView()
)
]
)
)
the chatBar is top of keyboard, when keyboard is open.
for transparent chatBar: can wrap Scaffold by
Theme(
data: ThemeData.light().copyWith(
bottomSheetTheme: BottomSheetThemeData(backgroundColor: Colors.transparent),
),
This worked for me,
showBottomSheet(
context: context,
builder: (context) => Container(
height: // YOUR WIDGET HEIGHT
child: // YOUR CHILD
)
showBottomSheet is a flutter inbuilt function.

Resources