I have a todo app which has a listview with title and short summary showing when clicked on the listtile it should go to the particular todo details page which shows all the details regarding the todo
This is my listview
#override
Widget build(BuildContext context) {
return Scaffold(
drawer: newdrawer(),
appBar: newappbar(),
body: _TaskList(),
floatingActionButton: FloatingActionButton(
foregroundColor: Colors.black54,
backgroundColor: Colors.blue,
elevation: 0,
child: Icon(Icons.add),
onPressed: () {
addDialog(context);
},
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
);
}
// ignore: non_constant_identifier_names
Widget _TaskList() {
if (Task != null) {
return FlatButton(
child: ListView.builder(
itemCount: Task.documents.length,
padding: EdgeInsets.all(5.0),
itemBuilder: (context, i) {
return new ListTile(
title: Text(Task.documents[i].data['Title']),
subtitle: Text(Task.documents[i].data['Summary']),
);
},
),
onPressed: () {},
);
} else {
return Text('Loading, Please wait..');
}
}
}
the on pressesd button of flat button which is empty in widget tasklist when clicked should open the detailtask page showing only that particular task details
class _taskdetailState extends State<taskdetail> {
//taskdetail([this.id]);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: newappbar(),
drawer: newdrawer(),
body: Column(
children: <Widget>[
Text('')
],
)
);
}
}
Please help me in my guess in my list view the on pressesd when tapped should take the document id and then it should pass on to the detail screen. But i am out of thoughts on how to do this.
in onPressed method, you need to pass the detail of Task
like this
onPressed: (){
navigator.push(context,MaterialPageRoute(builder:(context)=>_taskdetailState(details: Task.documents[i].data)));
}
now you can access this data like
class _taskdetailState extends State<taskdetail> {
final dynamic Taks;
_taskdetailState({this.Task});
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title:Text(Task.Title)),
drawer: newdrawer(),
body: Column(
children: <Widget>[
Text('')
],
)
);
}
}
Related
I tried using the following function inside build body.But it throws error saying
The argument type 'Future<void> Function(BuildContext)' can't be assigned to the parameter type 'void Function()'
Future<void> confirmation(BuildContext context) async {
return await showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: Colors.black54,
content: Center(
child: Text(
"Please Wait....",
style: TextStyle(color: Colors.blueAccent),
)));
});
}
class Trial extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(title: Text('Request Processed')),
body: Center(
child: Flatbutton(onPressed: confirmation,child: Text('Click me')), //this onpressed shows error
),
),
);
}
}
I have tried calling the same function from appBar action widget icon and it didn't throw any error.On using in build function only it throws error. Why is that so?
Try to call it like this
class Trial extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(title: Text('Request Processed')),
body: Center(
child: Flatbutton(onPressed: () {confirmation(context);},child: Text('Click me')), //this onpressed shows error
),
),
);
}
}
It seems the same but it saved me a lot of time. Let me know:)
Pass the context while calling it. Like this ()=>confirmation(context)
class Trial extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(title: Text('Request Processed')),
body: Center(
child: Flatbutton(onPressed: ()=>confirmation(context),child: Text('Click me')), //this onpressed shows error
),
),
);
}
}
This question already has answers here:
Scaffold.of() called with a context that does not contain a Scaffold
(15 answers)
Closed 2 years ago.
I can't seem to figure out why this isn't working. I'm supposed to get a snackbar when I tap the button. Any help? The code snippet on flutter api works fine tho
void main() => runApp(SnackBarDemo());
class SnackBarDemo extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: Center(
child: InkWell(
// When the user taps the button, show a snackbar.
onTap: () {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Tap'),
));
},
child: Container(
padding: EdgeInsets.all(12.0),
child: Text('Flat Button'),
),
),
)
)
)
);
}
}
You are calling showSnackbar in a widget on the same level of the Scaffold, so the context you are sending to this method does not contain a Scaffold,
Soltuion:
make this widget one level below the Scaffold:
void main() => runApp(SnackBarDemo());
class SnackBarDemo extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: Center(
child: Builder(
builder: (context)=>InkWell(
// When the user taps the button, show a snackbar.
onTap: () {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Tap'),
));
},
child: Container(
padding: EdgeInsets.all(12.0),
child: Text('Flat Button'),
),
),
),
)
)
)
);
}
}
I am working on a Flutter widget but I can't seem to get it to return multiple widgets. The Widget is called Widg and it should return the listView.builder widget and the floatingActionButton widget. Here is my code:
#override
Widget build(BuildContext context) {
return <Widget>[
//children: <Widget> [
ListView.builder(
itemCount: list.length,
itemBuilder: (context, i) {
return listRow();
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
setState(() {
list.add(list.length);
});
}
)
]
];
}
I am unable to figure out how to do this. I tried listing them as children as per the comment, but it didn't work. This is where I call my widget:
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Next Page"),
),
body: Widg()
);
}
Can someone please help me out? Thanks!
This should work.
FloatingActionButton documentation for your reference.
import 'package:flutter/material.dart';
Widget build(BuildContext context) {
List list;
return new Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
setState(() {
list.add(list.length);
});
}),
appBar: new AppBar(
title: new Text("Next Page"),
),
body: ListView.builder(
itemCount: list.length,
itemBuilder: (context, i) {
return listRow();
},
),
);
}
I have 2 pages, in the first page I have a button which is on click will open second page, in second page I have variable number = 999; so when I back to the first page I want to show the number print(number); or display on Text(number) How to do it with dispose() ?
#override
void dispose() {
super.dispose();
// send data to the first page
}
thanks for your answer
You can simply do this with the help of a navigator.
Navigator.push returns a Future that completes after calling
Navigator.pop on the Second Screen with the value passed.
e.x code:
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Returning Data',
home: HomeScreen(),
));
}
class HomeScreen extends StatelessWidget {
String _resultNumber = '';
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Returning Data Demo'),
),
body: Center(child: SelectionButton()),
);
}
}
class SelectionButton extends StatefulWidget {
#override
_SelectionButtonState createState() => _SelectionButtonState();
}
class _SelectionButtonState extends State<SelectionButton> {
String _resultNumber;
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RaisedButton(
onPressed: () => _navigateAndDisplaySelection(context),
child: Text('Pick an option, any number!'),
),
Text(_resultNumber ?? ''),
]);
}
_navigateAndDisplaySelection(BuildContext context) async {
// Navigator.push returns a Future that completes after calling
// Navigator.pop on the Selection Screen.
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => SelectionScreen()),
);
_resultNumber = result;
}
}
class SelectionScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Pick a number'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: () {
// Close the screen and return "Yep!" as the result.
Navigator.pop(context, '999');
},
child: Text('999 Number'),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: () {
// Close the screen and return "Nope!" as the result.
Navigator.pop(context, '500');
},
child: Text('550 Number'),
),
)
],
),
),
);
}
}
With dispose() you need override the back pressed, to do this wrap the Scaffold in WillPopScope widget.
return WillPopScope(
onWillPop: () {
_backPressed();
return Future.value(false);
},
child: Scaffold(
appBar: AppBar(
title: Text('your text'),
),
body: Center(),
),
);
void _backPressed() {
Navigator.pop(context, '999');
}
I defined the ontap method to navigate to SecondRoute class but it leads to the black screen.to resolve this issue I also replace the materialApp with Scaffold but didn't find luck
class FirstMain extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body:TabviewWidget(),
drawer: Drawer(
child: ListView(
children: <Widget>[
ListTile(
title: Text('Item 2'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
),
],
),
),
);
}
}
class SecondRoute extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Route"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go back!'),
),
),
);
}
}
I expected to open SecondRoute but appears black screen