How to bypass a screen using navigator in flutter? - flutter

I am coming from android.
In android ,if i want to bypass an activity i will call intent method in onCreate something like this.
onCreate(){
if(condition satisfied){
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
}
But i cant understand how to write this code in flutter and navigate another screen .
And I also don't know that this code will write in initState() or build().
note: In this case in If condition, I used StreamBuilder,
I need in following steps,
At screen start
Blank screen
if Condition will be true than return Container();
else leave this Screen(Activity) go to another screen.

Use this :
return isSomethingtrue ? HomeScreen() : OtherScreen(),
Then - In your HomeScreen() Build a Container()
class HomeScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container();
}
}

Related

Couldn't find the correct provider above this widget

I have a problem using Flutter Provider...
After Onbording screens I click to Next Button and needs to show me Welcome Screen (where i choose to log in with phone number)
But after clicking to Next Button i get this error - Could not find the correct Provider above this WelcomeScreen Widget
class _WelcomeScreenState extends State<WelcomeScreen> {
#override
Widget build(BuildContext context) {
final ap = Provider.of<AuthProvider>(context, listen: true);
return Scaffold(
body: SafeArea(
child: Center(
Make sure your Provider<> widget is present and at the root(like parent of MaterialApp for instance) to make sure it is in widget tree across rebuilds.
As far as I can see your code, you're calling Provider watch in welcome screen which prolly doesn't have the Provider in its tree. Move the watch to OnboardingScreen.

How can I make a card clickable and navigate to another page inside TabBarView?

I'm trying to make a card clickable and navigate to another page 'forecastWeather.dart'. But I can't seem to figure out how I can fix the error I'm getting. The error message says
Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.
I'm using a TabBarView with two tabs and one contains a card which should be clickable and navigate to another page 'forecastWeather.dart'. Please help!
In order to get use navigator.push, you must pass the context of your widget and have a MaterialApp somewhere in your tree, you do both, so the code should work, but it doesn't.
The reason is simple, the context you are passing is outdated, you are first creating the context when the build method begins, at that point, there is no material app, then you add a material app, but the context already exists, so the context you are passing when calling Navigator.push(context ... has no MaterialApp.
A possible fix is to use the Builder widget to make a new context after you create the MaterialApp widget:
#override
Widget build(BuildContext context) {
// context has no material app.
return MaterialApp(
home: Builder(
builder: (context) {
// this context does have a material app and is safe to use.
return DefaultTabController( ... );
}
),
);
}
Another fix is to move the code into a new widget with it's own context:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MainPage(),
);
}
}
class MainPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
// this context was created after the material app.
return DefaultTabController(...);
}
}
i suggest you to use get: ^4.6.1 package.
with this package you just need to use this for any navigation:
Get.to(ForecastWeather());
this package have so other features read its document here
[1]: https://pub.dev/packages/get

How to close searchbar after returning a new screen ? Flutter

class DataSearch extends SearchDelegate<String> {
....
#override
Widget buildResults(BuildContext context) {
return MyProfile();
}
.....
}
this is the code for search delegate
i tried Close() Method but it close the Profile Screen also
when i use navigate method same as image
where should i use close method or am doing it in a wrong way ?
You may try this. Add this at your on search complete
FocusManager.instance.primaryFocus.unfocus();

Show Snackbar in any screen when task finishes

I have a task that will run in the background (in an isolate) and when it finishes, I want to show a Snackbar. However, the user may navigate to a different screen from the one where the task was initiated. How do I show an 'app-level' Snackbar, not bound to any particular screen?
Edit: I found this: How to show a SnackBar from async executions when no context is available?, has some good information and option 1 (Display errors in page scaffolds) seems to be what I want, but I need to implement all by myself. I was hoping for something built in into Flutter.
I'd give this a try where you go for an abstract base class where you implement the 'listener', which then all your pages extend from instead of e.g. StatelessWidget.
Instead of overriding the normal build method, just override your new special build.
Pseudo code (here with BlocListener):
abstract class MySnackbarShowingPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return BlocListener<SubjectBloc, SubjectState>(
listener: (context, state) {
// TODO: implement snackbar display
},
child: mySpecialBuild(context),
);
}
Widget mySpecialBuild(BuildContext context);
}
class MySpecificPage extends MySnackbarShowingPage {
#override
Widget mySpecialBuild(BuildContext context) {
// TODO: implement mySpecialBuild as your normal page does today
}
}

how to pass data to another screen using bloc in flutter

hello I am new in flutter and bloc, I imagine that I have 2 screens (login and home screen). In login screen I am using bloc that post data and I want to call that data in my home screen. Can someone give me example to do that?
There are many ways to do that, I can name a few.
You navigate to the new Widget (the screen) and pass to that Widget constructor the data you want it to have.
You can use Provider to provide that data and wrap the new screen on it, then navigate to the new screen.
If this is some data that should be accessed across the app, you could provide the entire BLoC to the entire App and get the BLoC's reference on this new screen and then get the data directly from the BLoC.
If you just want to pass a value to home page from login page, you can do like this:
class Home extends StatelessWidget {
final String username;
Home(this.username);
#override
Widget build(BuildContext context) {
return Container();
}
}
class Login extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (ctx) {
return Home('flutter');
}));
}),
);
}
}