How to navigate to respective page by tapping RichText in flutter - flutter

I am implementing RichText, in which while tapping the text i need to navigate to the respective page , i have did it using recognizer, but unable to navigate, any suggestion which could really helpful, pls suggest
RichText(
text: TextSpan(children: [
TextSpan(
text: ' ALREADY HAVE AN ACCOUNT ? ',
style: TextStyle(color: Colors.grey)),
TextSpan(
text: 'LOG IN',
recognizer: new TapGestureRecognizer()..onTap = () => {
Navigator.push(context, MaterialPageRoute(
builder: (context) => LoginPage()),
)
},
style: TextStyle(color: Colors.deepPurpleAccent)),
]))

try this:
RichText(text: TextSpan(children: [
TextSpan(
text: ' ALREADY HAVE AN ACCOUNT ? ',
style: TextStyle(color: Colors.grey)),
WidgetSpan(child: GestureDetector(child:Text('LOG IN', style: TextStyle(color: Colors.deepPurpleAccent),), onTap: (){
Navigator.push(context, MaterialPageRoute(
builder: (context) => LoginPage()),
);
},)
]),)

How about wrapping RichText in an InkWell:
import 'package:flutter/material.dart';
class TextInkwellpage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Text InkWell'),
),
body: Center(
child: InkWell( // child tapped will fire onTap
child: RichText(
text: TextSpan(text: 'Inside RichText....', style: DefaultTextStyle.of(context).style),
),
onTap: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => SomePage())),
// ↑ Navigate to new page here
),
)
);
}
}

Related

I've been trying to create the button to navigate to the second page but somehow the RaisedButton function did't work out

I have been following the yt video on creating the navigation map button but I don't understand what did I do wrong on RaisedButton line (I put in stars infront and at the back)
import 'package:flutter/material.dart';
import 'package:ionicons/ionicons.dart';
import 'package:avenique/pages/PlanningPaymentScreen.dart';
class RootApp extends StatelessWidget {
RootApp({super.key});
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: () {
print("menu pressed");
},
icon: Icon(Icons.menu)),
title: Text("PlanningPage"),
actions: <Widget>[
IconButton(
onPressed: () {
print("search pressed");
},
icon: Icon(Icons.search)),
],
),
body: Center(
** child:RaisedButton( **
child: Text(
'Next Screen',
style: TextStyle(
color: Color.fromARGB(255, 255, 255, 255),
),
),
Color: Color.fromARGB(255, 0, 0, 0),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PlanningPaymentScreen()),
);
}),
),
);
}
}
I expected the code to run smoothly. Thankyou for the help!
Since RaisedButton is deprecated. try using " ElevatedButton " and you will find a function inside then you can push.
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
}

A value of type 'Object' can't be returned from the method '_onBackPressed' because it has a return type of 'Future<bool>'. after updating the SDK

I have a pop-up that asks whether to exit the application or not, it worked correctly. But after updating the SDK version, an error occurred:
"A value of type 'Object' can't be returned from the method '_onBackPressed' because it has a return type of 'Future'"
I will be grateful for your help
My code:
Future<bool> _onBackPressed() {
return showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('You want to exit the application?'),
content: new Text('You want to exit the application',
style: TextStyle(
fontSize: 20,
),
),
actions: <Widget>[
new GestureDetector(
onTap: () => Navigator.of(context).pop(false),
child:
Text("No",
style: TextStyle(
fontSize: 25,
),
),
),
SizedBox(height: 40),
new GestureDetector(
onTap: () => Navigator.of(context).pop(true),
child: Text("Yes",
style: TextStyle(
fontSize: 25,
),
),
),
],
),
) ??
false;
}
Image with errore:
Try this, and showDialog returns Future.
Future<bool> _onBackPressed() async {
return await showDialog(
context: context,
builder: (context) =>

Flutter - ShowDialog not showing the dialog

Here is my _showDialog function:
Future<void> _showDialog({BuildContext context, String msg, String title}) async {
showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
title: Text(title),
content: Text(msg),
actions: [
FlatButton(
child: Text('Ok'),
onPressed: () => Navigator.of(context).pop(true),
),
],
);
});
}
Here is my code:
if (result.contains("Error")) {
// Error!!!
// Show ERROR dialog box.
_showDialog(context: context, title: "Error", msg: result);
} else {
// Success!!!
// Show SUCCESS dialog box, then return to the previous screen.
_showDialog(context: context, title: "Success", msg: "Success!");
Navigator.pop(context); // return to the previous screen
}
If error occurs the ERROR dialog box it shows.
But if there is no error the SUCCESS dialog box it not show.
If the line
Navigator.pop(context);
is commented out the SUCCESS dialog box it show.
I need to show the SUCCESS dialog box before return to the previous screen.
You should then await the close button response on your dialog, like:
Future _showDialog({BuildContext context, String msg, String title}) async {
return await showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
title: Text(title),
content: Text(msg),
actions: [
FlatButton(
child: Text('Ok'),
onPressed: () => Navigator.of(context).pop(true),
),
],
);
});
}
and then
if (result.contains("Error")) {
// Error!!!
// Show ERROR dialog box.
_showDialog(context: context, title: "Error", msg: result);
} else {
// Success!!!
// Show SUCCESS dialog box, then return to the previous screen.
await _showDialog(context: context, title: "Success", msg: "Success!");
Navigator.pop(context); // return to the previous screen
}
I was also having the same issue but adding await in front of showDialog fixed it for me
you should await the showDialog like this
Future<void> authSuccessHandle(
{required String title,
required String subtitle,
required BuildContext context}) async {
await showDialog(
context: context,
builder: (BuildContext ctx) {
return AlertDialog(
backgroundColor: Colors.green.shade50,
title: Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 6.0),
child: CircleAvatar(
backgroundColor: ColorsConsts.AppMainColor,
child: const Icon(
Icons.done,
color: Colors.white,
size: 25,
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
title,
style: const TextStyle(
fontSize: 16,
),
),
),
],
),
content: Text(
subtitle,
style: const TextStyle(
fontSize: 14,
),
),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(
'Ok',
style: TextStyle(
color: ColorsConsts.AppMainColor,
fontSize: 16,
),
),
)
],
);
});
}

How to navigate to a different page using MenuItem in Flutter?

I'm new to Flutter, I have a list of MenuItems in Drawer like below.
MenuItem selectedMenuItem;
List<MenuItem> menuItems = [
MenuItem(title: 'Contact', items: [
MenuItem(title: 'Contact Us'),
MenuItem(title: 'Call center'),
MenuItem(title: 'WhatsApp'),
]),
MenuItem(title: 'language'),
MenuItem(title: 'Customer'),
];
I want to open a different page when each item is clicked. What should be my next step? Any ideas?
subItems(BuildContext context) {
return selectedMenuItem.items.map(
(item) => MaterialButton(
onPressed: () {
redirectItem(item);
},
child: Row(
children: [
Text(
item.title,
),
],
),
),
);
} style: TextStyle(fontSize: 14),
);
}
Welcome to the Flutter Community! I don't see your complete code so I guess you're already able to perform something when onPressed() is triggered.
You can navigate to another widget using this code:
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => YourWidget(),
),
);
},

Add a link to navigator in a text widget

I want to show a link to seperate view inside a text widget
I tried adding Two Text Widgets and a flat button in between but it doesnt show as expected
How I want it to look like
<p>This is a sample text</p>
This is what i tried
Column(
children: [
Text('This is a '),
FlatButton(
onPress: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>View()))
}
child: Text('sample')
),
Text(' text')
]
)
TapGestureRecognizer _recognizer;
#override
void initState() {
super.initState();
_recognizer = TapGestureRecognizer()..onTap = (){
print("tapped");
};
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: 'This is a '),
TextSpan(text: 'sample', style: TextStyle(fontWeight: FontWeight.bold), recognizer: _recognizer),
TextSpan(text: ' text'),
],
),
),
),
);
}