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();
},
),
);
}
Related
im new to Flutter. I have a mainpage, which is a listview with a drawer. I manage to call a second listview, which is mainly a copy of the mainpage without the drawer. But on the secondpage i see no backbutton on the top left.
Here i call the secondpage inside the drawer:
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(builder: (context) => new ListViewTeam()),
);
},
and my secondpage begins with this code:
#override
_ListViewTeamState createState() => new _ListViewTeamState();
}
class _ListViewTeamState extends State<ListViewTeam> {
List<Team> items = new List();
DatabaseHelper db = new DatabaseHelper();
#override
void initState() {
super.initState();
db.getAllTeams().then((teams) {
setState(() {
teams.forEach((team) {
items.add(Team.fromMap(team));
});
});
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Teams',
home: Scaffold(
appBar: AppBar(
title: Text('Teams'),
centerTitle: true,
backgroundColor: Colors.teal,
),
body: Center(
child: ListView.builder(
itemCount: items.length,
padding: const EdgeInsets.all(1.0),
itemBuilder: (context, position) {
Hope someone can help a noob?
You can try with the Below code replace this code at your second page I hope this will work for you
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Teams'),
centerTitle: true,
backgroundColor: Colors.teal,
),
body: Center(
child: ListView.builder(
itemCount: items.length,
padding: const EdgeInsets.all(1.0),
itemBuilder: (context, position) {
I'm tying to clear all of the previous routes after the user has logged on to my app. However, I'm having an issue with the following code in my Flutter app:
void clearRoutes()
{
//createLinks();
Navigator.of(context).pushNamedAndRemoveUntil('/my_home', (Route<dynamic> route) => false);
}
Widget build(BuildContext context)
{
WidgetsBinding.instance.addPostFrameCallback((_) => clearRoutes());
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("iWhiz Home"),
),
body: ListView.builder(
itemCount: links.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(links[index]),
);
}
),
);
}
The above code results in this issue:
The line of code that is giving me this issue is Navigator.of(context).pushNamedAndRemoveUntil('/my_home', (Route<dynamic> route) => false);
Can this issue be resolved?
I was able to find a solution. This is the code that was giving me a problem:
class Login extends StatelessWidget
{
void homePage(BuildContext context)
{
Navigator.pushNamed(context, "/my_home");
}
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("Login"),
),
body: Center(
child:
Column(
children: [
Text("Welcome to the Login Page"),
ElevatedButton(
onPressed: ()
{
homePage(context);
},
child: Text("Login"),
),
],
)
),
);
}
}
class MyHome extends StatelessWidget
{
void removePreviousRoutes(BuildContext context)
{
//The problem code.
Navigator.of(context).pushNamedAndRemoveUntil('/my_home', (Route<dynamic> route) => false);
}
Widget build(BuildContext context)
{
WidgetsBinding.instance.addPostFrameCallback((_) => removePreviousRoutes(context));
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("My Home"),
),
body: Center(
child:
Text("Welcome to the Home Page"),
)
);
}
}
The initial page that would be displayed is Login. After the user clicks on 'Welcome to the Login Page' they would be directed to the MyHome page. The issue comes from Navigator.of(context).pushNamedAndRemoveUntil('/my_home', (Route<dynamic> route) => false); on the MyHome page.
Solution:
class Login extends StatelessWidget
{
void homePage(BuildContext context)
{
//The solution.
Navigator.pushNamed(context, "/my_home");
Navigator.of(context).pushNamedAndRemoveUntil('/my_home', (Route<dynamic> route) => false);
}
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("Login"),
),
body: Center(
child:
Column(
children: [
Text("Welcome to the Login Page"),
ElevatedButton(
onPressed: ()
{
homePage(context);
},
child: Text("Login"),
),
],
)
),
);
}
}
class MyHome extends StatelessWidget
{
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("My Home"),
),
body: Center(
child:
Text("Welcome to the Home Page"),
)
);
}
}
The solution was to push the new route AND remove all previous routes from Login, INSTEAD OF pushing the new route in Login and removing previous routes from Registration. After doing this, I no longer received the flickering issue.
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
),
),
);
}
}
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('')
],
)
);
}
}
I have a flutter app with a Pageview (and at the bottom a cupertinotabbar). Everything 'works'. But when I switch pages, the streambuilders 'glitch'. (There's one on the first page and on the second page) (see video).
I think the problem is that flutter 'destroys' the streambuilders and then reloads them.
I tried putting keepClientAlive on the statefull widget, but didn't work.
Is there a possibility to keep the streambuilder alive?
This is my code:
#override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async => false,
child: Scaffold(
body: PageView(
children: <Widget>[
Discover(currentUser: currentUser),
Friends(currentUser: currentUser),
Find(),
],
controller: pageController,
onPageChanged: onPageChanged,
physics: NeverScrollableScrollPhysics(),
),
bottomNavigationBar: CupertinoTabBar(
backgroundColor: Colors.black,
currentIndex: pageIndex,
onTap: onTap,
activeColor: Colors.green,
items: [
BottomNavigationBarItem(icon: Icon(FontAwesomeIcons.search)),
BottomNavigationBarItem(icon: Icon(FontAwesomeIcons.users)),
BottomNavigationBarItem(icon: Icon(FontAwesomeIcons.biking)),
],
),
),
);
}
My streambuilder class
class BookList extends StatelessWidget {
#override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('books').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting: return new Text('Loading...');
default:
return new ListView(
children: snapshot.data.documents.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document['title']),
subtitle: new Text(document['author']),
);
}).toList(),
);
}
},
);
}
}
https://www.youtube.com/watch?v=y2yKhqvkT_A&feature=youtu.be
Thanks!