I have created an app in flutter, but whenever i press the back button it redirects me back to the pages i had open instead of previous pages - flutter

I am very new to flutter. Learning the basics. But the back button is not working as it should in the app.
This my main.dart file:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.orange
),
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}

Related

How can I make sure that my main.dart is correct?

This is my main.dar and I got problems:
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hive_flutter/adapters.dart';
import 'package:pomodoroTimer/1.app_bar_pomodoro/profile/colors.dart';
import 'apilar_codigo/stacked_all.dart';
void main() async {
// initialize hive
await Hive.initFlutter();
// open a box
await Hive.openBox("Habit_Database");
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
textTheme: GoogleFonts.nunitoTextTheme(Theme.of(context).textTheme),
useMaterial3: true,
colorScheme:
ColorScheme.light(primary: ColorsToAPP.selectText)),
home: const StackePages(),
);
}
}
Is there a piece of code that I'm missing out? because I can't run the app
Thanks for any help you can provide
How can I run main.dart without problems?
You are missing the code that will actually run the app:
void main() async {
...
runApp(MyApp());
}

How can i use Provider in flutter_native_splash package?

I want to get user's data from server and set State, when app is loading. so I use flutter_native_splash package and provider for app. the provider doesn't work.
stock_notifier.dart
class StockCodeNotifier extends ChangeNotifier {
final List<StockModel> _stocks = [];
String _stockCode = '';
String get stockCode => _stockCode;
void getStocks() {
List<StockModel> data = allStocks;
_stocks.clear();
_stocks.addAll(data);
_stockCode = _stocks[0].code;
notifyListeners();
}
main.dart
void main() {
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
runApp(MyApp());
Provider.of<StockCodeNotifier>(context).getStocks();
FlutterNativeSplash.remove();
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider<StockCodeNotifier>(
create: (_)=>StockCodeNotifier(),
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Home(),
),
);
}
}

my flutter app isnt running the homepage i want it to

I don't know what's happening but the app is running on my root page and not on the login page
import 'package:flutter/material.dart';
import 'package:loyality/Application/Pages/Login/login_page.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Libra',
home: LoginPage(),
);
}
}

Enforce Light or Dark theme in Flutter WebView

I'm using webview_flutter to display a site. The problem is, the site is opening in dark mode. I tried setting MaterialApp theme and darkTheme properties to ThemeData.light(), but the webview is not respecting it and taking Device's default theme. Switching device theme reflects in the webview.
How to enforce light/dark theme to the WebView widget explicitly?
Here a main.dart snippet
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
//please explain this one as well
// SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
// statusBarBrightness: Brightness.dark) // Or Brightness.dark
// );
return MaterialApp(
themeMode: ThemeMode.light,
theme: ThemeData.light(),
darkTheme: ThemeData.light(),
debugShowCheckedModeBanner: false,
title: 'Flutter site',
home: SafeArea(
child: WebViewExample(),
),
);
}
}
class WebViewExample extends StatefulWidget {
#override
WebViewExampleState createState() => WebViewExampleState();
}
class WebViewExampleState extends State<WebViewExample> {
#override
void initState() {
super.initState();
// Enable hybrid composition.
if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
// if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) { }
}
#override
Widget build(BuildContext context) {
return WebView(
javascriptMode: JavascriptMode.unrestricted,
initialUrl: 'https://flutter.dev.',
);
}
}

Debug mode banner is not removed

I am working on a Flutter app and want to hide the debug mode banner.
This is my code for main.dart:
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;
//FirebaseCrashlytics.instance.crash();
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: new MyApp(),
));
}
The debug mode banner is not removed, it is shown as always.
What do I need to change to get it removed?
Debug banner does not remove from main method, you just follow the example and you bind your MyApp() with the MaterialApp()
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: NextPage(),
);
}
}