I am using textDirection: TextDirection.rtl on the MaterialApp:
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: appTitle,
home: Directionality(
textDirection: TextDirection.rtl,
child: MyHomePage(title: appTitle),
),
);
Everything is aligned to right and works perfectly, except for the back button of the AppBar.
When you navigate to other pages (specifically I'm using drawer) the back button is aligned to the left and not to the right.
Navigation code:
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => const SettingsView(),
),
);
"Settings" page:
return Scaffold(
appBar: AppBar(title: Text("Settings"))
);
As you can see I didn't touch the leading property, I thought it should be automatically...
I am using builder to handle this.
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
builder: (context, child) => Directionality(
textDirection: TextDirection.rtl,
child: child ?? const SizedBox.shrink(),
),
);
}
}
class SettingPage extends StatelessWidget {
const SettingPage({super.key});
#override
Widget build(BuildContext context) {
print(Directionality.of(context).toString());
return Scaffold(
appBar: AppBar(
title: Text("Settings"),
),
);
}
}
Related
Please help me. I can't do basic navigation operations
I can't go to my shared page. I don't know why. When I want to route to another page with Navigator.push accrue to an error and I capture my error and add that at bottom of my question.
this is my main page
import 'package:flutter/material.dart';
import 'package:like/shared.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: homeScreen(context),
);
}
Scaffold homeScreen(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("shared"),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Shared()),
);
},
child: Text("go")),
),
);
}
}
this is my shared page
import 'package:flutter/material.dart';
import 'package:like/main.dart';
void main() => runApp(Shared());
class Shared extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
appBar: AppBar(
title: Text('Material App Bar'),
),
body: Center(
child: Container(
child: Text('Hello World'),
),
),
),
);
}
}
Both of them do not work.
For more information about the error, you can see the photo of the error
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Shared()),
);
Shared() is not a widget
remove this line from the top of your page:
import 'package:like/shared.dart';
Shared() is a package
I'm fairly new to flutter and dart, I set up my app like so:
class MyExpensesApp extends StatefulWidget {
const MyExpensesApp({Key? key}) : super(key: key);
#override
State<MyExpensesApp> createState() => _MyExpensesAppState();
}
class _MyExpensesAppState extends State<MyExpensesApp> {
#override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'title',
home: Directionality(
textDirection: TextDirection.rtl,
child: HomePage(),
),
);
}
}
and i have a FloatingAction button inside the HomePage which navigates to a different page when clicked
#override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
....
),
floatingActionButton: FloatingActionButton(
onPressed: () => {
Navigator.push(
context, MaterialPageRoute(builder: (context) => ExpensePage()))
},
tooltip: "add expense",
child: Icon(Icons.add),
),
);
}
}
but when navigation to the ExpensePage screen, it is not RTL, which means it is not a child of Directionality widget?
Am i just replacing the whole MaterialApp with ExpensePage?
How can i only replace the Directionality Child when navigating?
I don't need any intricate routing, as i only have those 2 pages.
To put a Directionality widget above the Navigator (and hence above every page), you can use the builder property in MaterialApp.
return MaterialApp(
title: 'title',
home: HomePage(),
builder: (context, child) => Directionality(
textDirection: TextDirection.rtl,
child: child!,
),
);
For the child page, you can use Scaffold as the root parent.
Then try with the same routing.
I have an application that I'm building and in it, I have an AppBar. My text is in Arabic and I want to make the text place change from LTR (left-to-right) to RTL (right-to-left)
Here is a screenshot of the AppBar
And this is the code for my AppBar
class MyAppBar extends StatelessWidget with PreferredSizeWidget {
#override
Size get preferredSize => Size.fromHeight(kToolBarHeight);
#override
Widget build(BuildContext context) {
return AppBar(
title: Text(
kAppBarTitleFirst,
),
);
}
}
So the question is:- How can I get the text عنوان التطبيق to be in the place of the red that I marked (see screenshot above)
if you want Text direction right to left in your entire app and not just on the App Bar you can provide a Directionality to MaterialApp
MaterialApp(
builder: (context, child) {
return Directionality(
textDirection: TextDirection.rtl,
child: child,
);
},
);
force the whole app to be from left to right.
import 'package:flutter/material.dart';
void main(List<String> args) {
return runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
builder: (context, child) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
appBar: AppBar(
title: const Text(
'عنوان التطبيق',
),
backgroundColor: Colors.blue,
),
backgroundColor: Colors.blue,
),
);
},
),
);
}
also here is the changes on the orignal demo flutter
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: const Directionality( // <-- Add this Directionality
textDirection: TextDirection.rtl,
child: MyHomePage(title: 'الصفحة الرئيسية لعرض Flutter Demo')),
);
}
}
Use textDirection property of Text widget.
class MyAppBar extends StatelessWidget with PreferredSizeWidget {
#override
Size get preferredSize => Size.fromHeight(kToolBarHeight);
#override
Widget build(BuildContext context) {
return AppBar(
title: Text(
kAppBarTitleFirst,
textDirection:TextDirection.rtl // ← add this line
),
);
}
}
I have a MaterialApp in which it has an IndexedStack as its homePage, to use it for BottomBarNavigation. Now, in one of the "Tab"s, I want page transitions to be done like it is done in iOS.
Part of the trick can be done using CupertinoPageRoute in Navigator.push as follows:
Navigator.of(context, rootNavigator: true).push(CupertinoPageRoute<bool>(
//fullscreenDialog: true,
builder: (BuildContext context) => new DescriptionPage(),
));
this results the new page to slide from right as an iOS app. But, I also want the first page to be shifted to right with parallax, as it says in the CupertinoPageRoute's documentation:
The page also shifts to the left in parallax when another page enters to cover it.
this will be done if the first page itself is created via "Navigator.push(CupertinoPageRoute ...", but as I mentioned, my first page is one of the main pages of the application's home.
current transition style:
the new page slides in from right, but the current page does not shift to left
as you can see, the current page does not shift to left. There might be a way to make the current page's widget to act as a widget built by a CupertinoPageRoute, so that the current page itself slides to left as the new page comes in.
In the theme: parameter of MaterialApp, try this:
MaterialApp(
theme: ThemeData( //or replace with your custom ThemeData.copywith()
pageTransitionsTheme: PageTransitionsTheme(
builders: {
TargetPlatform.android: CupertinoPageTransitionsBuilder(),
TargetPlatform.iOS: CupertinoPageTransitionsBuilder()
}
),
),
)
If you run the code below, you will see that First Tab is doing what you are seeing and second Tab is what you are expecting.
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: CupertinoStoreHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class CupertinoStoreHomePage extends StatelessWidget {
const CupertinoStoreHomePage({title});
#override
Widget build(BuildContext context) {
return CupertinoTabScaffold(
tabBar: CupertinoTabBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.bell),
title: Text('TAB1'),
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.bell_solid),
title: Text('TAB2'),
),
],
),
tabBuilder: (context, index) {
switch (index) {
case 0:
return CupertinoTabView(builder: (context) {
return CupertinoPageScaffold(
child: TAB1(),
);
});
case 1:
return CupertinoTabView(
builder: (context) {
return CupertinoPageScaffold(
child: TAB2(),
);
},
routes: {
'/screen': (ctx) => NextScreen(),
},
);
default:
return Container();
}
},
);
}
}
class TAB1 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("TAB1"),
),
body: Container(
child: FlatButton(
child: Text("Go To Next Screen"),
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (_) => NextScreen())),
),
),
);
}
}
class TAB2 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("TAB2"),
),
body: Container(
child: FlatButton(
child: Text("Go To Next Screen"),
onPressed: () => Navigator.of(context).pushNamed("/screen"),
)),
);
}
}
class NextScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: Scaffold(
appBar: AppBar(
title: Text("NextScreen"),
),
body: Container(
child: Text("NextScreen"),
),
),
);
}
}
When using Navigator.of(ctx).push the app tries to push new screen on root navigator, which in your case is tab bar but it fails to replace the tab bar, hence you see incomplete animation. But with second approach where you have defined route in relevant tab and use Push Named, the app uses navigator assigned to that tab and hence expected result.
Happy Coding!
when returning MaterialApp flutter shows error as follow "error: 1 required argument(s) expected, but 0 found. (not_enough_required_arguments at [demo] lib/main.dart:9)"Here is the screenshot
const _categoryName = "Cake";
const _categoryIcon = Icons.cake;
const _categoryColor = Colors.green;
import 'package:solution_02_category_widget/category.dart';
void main() {
runApp(UnitConverterApp());
}
class UnitConverterApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Unit Converter',
home: Scaffold(
backgroundColor: Colors.green[100],
body: Center(
child: Category(
name: _categoryName,
color: _categoryColor,
iconLocation: _categoryIcon,
),
),
),
);
}
}
You should wrap the title argument under AppBar Widget. You can in turn wrap the appBar argument inside the Scaffold. The scaffold is like an empty page that gives your app the white background. Below is a sample code implementation:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
// debugShowMaterialGrid: true,
theme: ThemeData(
brightness: Brightness .light,
accentColor: Colors.deepPurple,
primarySwatch: Colors.deepOrange),
home: Scaffold(
appBar: AppBar(
title: Text('Some text'),
),
),
);
}
}
Your code is working fine without error if i remove the Category() widget. The below code is working fine:
import 'package:flutter/material.dart';
const _categoryName = "Cake";
const _categoryIcon = Icons.cake;
const _categoryColor = Colors.green;
void main() {
runApp(UnitConverterApp());
}
class UnitConverterApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Unit Converter',
home: Scaffold(
backgroundColor: Colors.green[100],
body: Center(
child: Container(),
),
),
);
}
}
Maybe the problem is in the Category() widget so if you post it i can check it for you.