1

i used below code to get alert whenever user clicks on a particular list item


import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
class RamList extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
 return _myListView(context);
 }
}
Widget _myListView(BuildContext context) {
 final titles = [
 'Part-1',
 'part-2',
 'part-3',
 'part-4',
 'part-5',
 ];
 final numbers = [
 '1 ',
 '2 ',
 '3 ',
 '4 ',
 '5 ',
 ];
 functionOne() {
 Navigator.push(
 context, new MaterialPageRoute(builder: (context) => RamOne()));
 }
 functionTwo() {
 Navigator.push(context, MaterialPageRoute(builder: (context) => RamTwo()));
 }
 functionThree() {
 Navigator.push(
 context, MaterialPageRoute(builder: (context) => RamThree()));
 }
 functionFour() {
 Navigator.push(context, MaterialPageRoute(builder: (context) => RamFour()));
 }
 functionFive()=>_onAlertButtonPressed1; 
 final List<Function> onTaps = [
 functionOne,
 functionTwo,
 functionThree,
 functionFour,
 functionFive,
 ];
 return ListView.builder(
 itemCount: titles.length,
 itemBuilder: (context, index) {
 return Card(
 elevation: 50,
 child: InkWell(
 child: Row(
 children: <Widget>[
 Container(
 height: 100.0,
 width:50.0,
 decoration: BoxDecoration(
 gradient:LinearGradientStyle.linearGradient(
 orientation:LinearGradientStyle.ORIENTATION_HORIZONTAL,
 gradientType: LinearGradientStyle.GRADIENT_TYPE_AMIN
 )
 ),),
 Container(
 margin: EdgeInsets.all(10),
 child: Text(
 numbers[index],
 )),
 Flexible(child: Container(
 margin: EdgeInsets.all(10),
 child: GradientText((titles[index]),
 gradient:gradient,
 style:TextStyle(fontSize:20.0,fontWeight:FontWeight.bold, ),
 ),
 ))
 ],
 ),
 onTap: () => onTaps[index](),
 ));
 });
}
_onAlertButtonPressed1(context) {
 Alert(
 context: context,
 type: AlertType.info,
 title: "Coming soon",
 desc: "This link will be available in future updates",
 ).show();
}

I tried using rflutter alert to show alert.Everything is working fine without any error.But no alert appears after clicking on destined listview.. Why this code is not working. Please help me if you have any solution..

Please comment if you need more information.By the way console message is working fine.there is no error message in the console box.

asked Nov 10, 2019 at 10:26
7
  • Where is _onAlertButtonPressed1 function called? Commented Nov 10, 2019 at 10:30
  • See at the bottom part of the code Commented Nov 10, 2019 at 10:40
  • 1
    I see the definition of function, but I don't see where it is called Commented Nov 10, 2019 at 10:48
  • If you have any solution then please answer Commented Nov 10, 2019 at 11:13
  • 1
    It looks like you don't call _onAlertButtonPressed1 function at all. I'm not sure, because maybe you just didn't show all the code. Make sure you do call the function. Commented Nov 10, 2019 at 11:15

2 Answers 2

2

To show an alert dialog in flutter you must do the following:

_onAlertButtonPressed1(context) {
 AlertDialog alert = AlertDialog(
 title: Text("Coming soon"),
 content: Text("This link will be available in future updates"),
 actions: [
 //your actions (I.E. a button)
 ],
 );
 // show the dialog
 showDialog(
 context: context,
 builder: (BuildContext context) {
 return alert;
 },
 );
 }

You can view the documentation here.

And an article explaining how to use an AlertDialog here.

answered Nov 10, 2019 at 10:31
Sign up to request clarification or add additional context in comments.

2 Comments

This isn't the solution to problem. OP is using package to show alerts that has different API.
@ramanraman - there were double quotes in the title text, it now works
1

You have this callback: functionFive()=>_onAlertButtonPressed;

It calls __onAlertButtonPressed_, but your function that shows the dialog is called _onAlertButtonPressed1 (it has 1 in the end of it).

UPD: Your callbacks should accept context and functionFive should call _onAlertButtonPressed1 function:

 functionOne(BuildContext context) {
 Navigator.push(
 context, new MaterialPageRoute(builder: (context) => RamOne()));
 }
 functionTwo(BuildContext context) {
 Navigator.push(context, MaterialPageRoute(builder: (context) => RamTwo()));
 }
 functionThree(BuildContext context) {
 Navigator.push(
 context, MaterialPageRoute(builder: (context) => RamThree()));
 }
 functionFour(BuildContext context) {
 Navigator.push(context, MaterialPageRoute(builder: (context) => RamFour()));
 }
 functionFive(BuildContext context) => _onAlertButtonPressed1(context); 

and pass the context to callbacks:

onTap: () => onTaps[index](context),
answered Nov 10, 2019 at 11:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.