Push navigation transition remove shadow - flutter

I found that Flutter App has slightly hard shadows than the Native App when pushing a new screen.
I searched on the internet and I didn't find much about this. Is there any way to remove that shadow?
Flutter doctor
Full reproducible code
File: main.dart
import 'package:flutter/cupertino.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return CupertinoApp(
initialRoute: '/',
onGenerateRoute: generateRoute,
home: Home(),
);
}
}
class Home extends StatefulWidget {
Home({Key key}) : super(key: key);
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: Center(child: CupertinoButton(child: Text('Navigate'), onPressed: () => Navigator.pushNamed(context, 'details'))),
);
}
}
class Details extends StatefulWidget {
Details({Key key}) : super(key: key);
#override
_DetailsState createState() => _DetailsState();
}
class _DetailsState extends State<Details> {
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: Center(child: Text('Details')),
);
}
}
Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
return CupertinoPageRoute(builder: (context) => Home());
case 'details':
return CupertinoPageRoute(builder: (context) => Details());
default:
return CupertinoPageRoute(builder: (context) => Home());
}
}

Related

how to use polymorphism with provider in flutter

I try to use Polymorphism with the Provider package in Dart/Flutter, but I'm not sure if it is possible or not and if I have made a mistake.
I have two provider class "Provider1" and "Provider2" which extend an abstract class "AbstactProvider", I have a widget "WidgetProvider1Or2"
which need to interact with Provider1 Or Provider2 but flutter throw me a "ProviderNotFoundException".
thanks for yours help!
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_bloc_concept/cubit/counter_cubit.dart';
import 'package:provider/provider.dart';
abstract class AbstactProvider extends ChangeNotifier {
void printName();
}
class Provider1 extends AbstactProvider {
#override
void printName() {
print("Provider1");
}
}
class Provider2 extends AbstactProvider {
#override
void printName() {
print("Provider2");
}
}
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => CounterCubit(),
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
Widget widgetProvider1 = ChangeNotifierProvider(
create: (_) => Provider1(),
child: WidgetProvider1(),
);
Widget widgetProvider2 = ChangeNotifierProvider(
create: (_) => Provider2(),
child: WidgetProvider2(),
);
Widget widgetProvider1Or2 = ChangeNotifierProvider(
create: (_) => Provider1(),
child: WidgetProvider1Or2(),
);
return Scaffold(
body: Column(
children: [widgetProvider1, widgetProvider2, widgetProvider1Or2],
),
);
}
}
class WidgetProvider1 extends StatelessWidget {
const WidgetProvider1({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
context.watch<Provider1>();
return Container();
}
}
class WidgetProvider2 extends StatelessWidget {
const WidgetProvider2({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
context.watch<Provider2>();
return Container();
}
}
class WidgetProvider1Or2 extends StatelessWidget {
const WidgetProvider1Or2({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
context.watch<AbstactProvider>();
return Container();
}
}

Unable to naviagte to another screen in flutter

I'm trying to take value from the method channel and using the value I'm trying to navigate another screen. When I try to navigate from TextButton onclick it's navigating but when I try to navigate from the value received by the method channel it's not navigating to another screen.
Example: I'm receiving openScreen1 from the method channel in the below code from methodCall.method and assigning the method to route variable but the page is not navigating
main_screen.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gg_app/screen1.dart';
import 'package:gg_app/screen2.dart';
class HomeScreen extends StatefulWidget {
static const routeName = "Home-Screen";
const HomeScreen({Key? key}) : super(key: key);
#override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
static const channel = MethodChannel('scribeemr.in/mic');
#override
void initState() {
// TODO: implement initState
channel.setMethodCallHandler(nativeMethodCallHandler);
super.initState();
}
Future<dynamic> nativeMethodCallHandler(MethodCall methodCall) async {
var route = methodCall.method;
await navigateTo(route, context);
}
Future<dynamic> navigateTo(String route, BuildContext context) async {
switch (route) {
case "openScreen1":
await Navigator.of(context).pushNamed(Screen1.routeName);
break;
case "openScreen2":
await Navigator.of(context).pushNamed(Screen2.routeName);
break;
default:
break;
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Home Screen")),
body: Column(
children: [
TextButton(
onPressed: () {
navigateTo("openScreen1", context);
},
child: Text("Screen 1")),
TextButton(
onPressed: () {
navigateTo("openScreen2", context);
},
child: Text("Screen 2")),
],
),
);
}
}
main.dart
import 'package:flutter/material.dart';
import 'package:gg_app/home_screen.dart';
import 'package:gg_app/screen1.dart';
import 'package:gg_app/screen2.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomeScreen(),
routes: {
HomeScreen.routeName: (context) => HomeScreen(),
Screen1.routeName: (context) => Screen1(),
Screen2.routeName: (context) => Screen2(),
},
);
}
}
screen1.dart
import 'package:flutter/material.dart';
class Screen1 extends StatefulWidget {
static const routeName = "Screen1";
const Screen1({ Key? key }) : super(key: key);
#override
State<Screen1> createState() => _Screen1State();
}
class _Screen1State extends State<Screen1> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Screen 1")),
);
}
}

No constructor 'MaterialApp.' with matching arguments declared in class 'MaterialApp'

I just created a new flutter project and started organized my folders, this is what I have for now:
I'm trying to run it on Windows since I'll use Flutter Desktop for this project, and when I hit run it builds and runs just fine, but when I do a hot-reload the application throws this error message:
No constructor 'MaterialApp.' with matching arguments declared in class 'MaterialApp'.
Receiver: MaterialApp
Tried calling: new MaterialApp.()
Found: new MaterialApp.({Key? key, GlobalKey<NavigatorState>? navigatorKey, GlobalKey<ScaffoldMessengerState>? scaffoldMessengerKey, Widget? home, Map<String, (BuildContext) => Widget> routes, String? initialRoute, ((RouteSettings) => Route<dynamic>?)? onGenerateRoute, ((String) => List<Route<dynamic>>)? onGenerateInitialRoutes, ((RouteSettings) => Route<dynamic>?)? onUnknownRoute, List<NavigatorObserver> navigatorObservers, ((BuildContext, Widget?) => Widget)? builder, String title, ((BuildContext) => String)? onGenerateTitle, Color? color, ThemeData? theme, ThemeData? darkTheme, ThemeData? highContrastTheme, ThemeData? highContrastDarkTheme, ThemeMode? themeMode, Locale? locale, Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates, ((List<Locale>?, Iterable<Locale>) => Locale?)? localeListResolutionCallback, ((Locale?, Iterable<Locale>) => Locale?)? localeResolutionCallback, Iterable<Locale> supportedLocales, bool debugShowMaterialGrid, bool showPerformanceOverlay, bool checkerboardRasterCacheImages, bool checkerboardOffscreenLayers, bool showSemanticsDebugger, bool debugShowCheckedModeBanner, Map<LogicalKeySet, Intent>? shortcuts, Map<Type, Action<Intent>>? actions, String? restorationScopeId}) => MaterialApp
This is the content of my main.dart:
import 'package:flutter/material.dart';
import 'package:beryllium/src/screens/HomeScreen.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Beryllium',
initialRoute: '/',
routes: {
'/': (contex) => HomeScreen(), //Home screen widget
},
);
}
}
And this the content of the HomeScreen.dart file:
import 'package:flutter/material.dart';
class HomeScreen extends StatefulWidget {
HomeScreen({Key key}) : super(key: key);
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
#override
Widget build(BuildContext context) {
return new _HomeScreen();
}
}
class _HomeScreen extends StatelessWidget {
const _HomeScreen({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My Book'),
),
body: Center(
child: Container(
child: Text('Home Screen'),
),
));
}
}
If someone has any idea about avoiding this error, please give me a hand.
Thanks in advance.

How can I change data on parent class from child class on flutter

I want to access and change data on parent class from child class. For this when I do it as in the code example below, it works. However, when I import the child class (ChildPage) from outside, not inside the same dart file, I cannot access the _MainPageState class.
It works like this.
main.dart
import 'package:flutter/material.dart';
class MainPage extends StatefulWidget {
#override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int selectedIndex = 0;
#override
Widget build(BuildContext context) {
return Column(
children: [
ChildPage(this),
],
);
}
}
//this is my child page
class ChildPage extends StatefulWidget {
_MainPageState parent;
ChildPage(this.parent);
#override
_ChildPageState createState() => _ChildPageState();
}
class _ChildPageState extends State<ChildPage> {
#override
Widget build(BuildContext context) {
return Container(
child: GestureDetector(
onTap: (){
widget.parent.setState(() {
widget.parent.selectedIndex = 1;
});
},
child: Text('click'),
),
);
}
}
It works when I write the above way. However, I want to separate both classes and write them as different dart files. For this, I tried to do as follows, but was not successful. I want to change the selectedIndex variable in main.dart by accessing it from the child.dart file. How can I do it?
main.dart
import 'package:flutter/material.dart';
class MainPage extends StatefulWidget {
#override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int selectedIndex = 0;
#override
Widget build(BuildContext context) {
return Column(
children: [
ChildPage(this),
],
);
}
}
child.dart
import 'package:flutter/material.dart';
import 'main.dart';
class ChildPage extends StatefulWidget {
_MainPageState parent; // it shows Undefined class '_MainPageState'.
ChildPage(this.parent);
#override
_ChildPageState createState() => _ChildPageState();
}
class _ChildPageState extends State<ChildPage> {
#override
Widget build(BuildContext context) {
return Container(
child: GestureDetector(
onTap: (){
// here i want to access selectedIndex in main.dart like widget.parent.selectedIndex
},
child: Text('click'),
),
);
}
}
As i understand you want to do like tab controller.
Or you can use like below with Navigator.
1- Navigation Helper
class NavigationHelper{
static const String HOME="/home";
static const String DETAIL="/detail";
}
2- Main
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter sadas',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
routes: {
"/home": (context) => MyHomePage(title: 'Flutter Demo asdad Page'),
"/firebase": (context) => FireBaseBook(),
"/pageview": (context) => PageViewM(),
"/html": (context) => HTML(),
"/file": (context) => FileDown(),
"/tab": (context) => MyTabBar(),
"/sliver": (context) => SliverView(),
"/detail": (context) => Detail(),
"/": (context) => DownloadWidget(),
});
}
}
3- Navigator
Navigator.pushNamed(context, NavigationHelper.DETAIL);

How to implement Route Navigation with async_redux

I'm new in Flutter and Dart and found a new "async_redux" package in https://pub.dev/packages/async_redux to develop my project easier way than traditional "redux" package. In readme document there is a short description about implement Route Navigation but I always receive:
"type 'NavigateAction' is not a subtype of type 'ReduxAction' of 'action'"
when i use -dispatch(NavigateAction.pushNamed("MyRoute"))- in "onChangePage".
Here the structure code:
Store<AppState> store;
final navigatorKey = GlobalKey<NavigatorState>();
void main() async{
NavigateAction.setNavigatorKey(navigatorKey);
var state = AppState.initialState();
store = Store<AppState>(initialState: state);
runApp(MyApp());
}
final routes={
'/': (BuildContext context) => First(),
"/myRoute": (BuildContext context) => Two(),
};
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return StoreProvider<AppState>(
store: store,
child: MaterialApp(
routes: routes,
navigatorKey: navigatorKey,
),
);
}
}
class AppState {
AppState(...);
AppState copy(...) =>
AppState(
...
);
static AppState initialState() => AppState(
...
);
#override
bool operator ==(Object other) => ...
#override
int get hashCode => ...;
}
class First extends StatelessWidget {
#override
Widget build(BuildContext context) => MyHomePageConnector();
}
class MyHomePageConnector extends StatelessWidget {
MyHomePageConnector({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return StoreConnector<AppState, ViewModel>(
model: ViewModel(),
builder: (BuildContext context, ViewModel vm) => MyHomePage(
onChangePage: vm.onChangePage
),
);
}
}
class ViewModel extends BaseModel<AppState> {
ViewModel()
VoidCallback onChangePage;
ViewModel.build({
#required this.onChangePage,
}) : super(equals: []);
#override
ViewModel fromStore() => ViewModel.build(
onChangePage: () => dispatch (NavigateAction.pushNamed ("/myRoute"))
);
}
class MyHomePage extends StatefulWidget {
final VoidCallback onChangePage;
MyHomePage({
Key key,
this.onChangePage
}) : super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return RaisedButton(
child: Icon(Icons.add_circle),
onPressed: widget.onChangePage
),
);
}
}
How and where implement "dispatch(NavigateAction.pushNamed ("/myRoute"))"?
Try this:
dispatch(NavigateAction<AppState>.pushNamed("/myRoute"))"
Update:
With recent async_redux: ^1.2.0 you don't need the <AppState> anymore, and can dispatch it like this:
dispatch(NavigateAction.pushNamed("/myRoute"))"