How to create user history page similar to 'my activity' on google - flutter - flutter

I am trying to make a history page in flutter. When I press 'a','b' or 'c' in my homepage, I want it to show what I pressed and the date I pressed the text on my history page similar to 'my activity' on google. This is what I came up with so far, and I don't even know if it is the best way to make it. It also has an error
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int count = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
Tile(text: Text("a")),
Tile(text: Text("b")),
Tile(text: Text("c")),
],
));
}
}
int count = 0;
class Tile extends StatefulWidget {
final Text text;
Tile({this.text});
#override
TileState createState() => TileState();
}
class TileState extends State<Tile> {
#override
Widget build(BuildContext context) {
return ListTile(
title: widget.text,
onTap: () {
count++;
print(count);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HistoryPage()),
);
},
);
}
}
class HistoryPage extends StatefulWidget {
#override
HistoryPageState createState() => HistoryPageState();
}
class HistoryPageState extends State<HistoryPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
})),
body: ListView.builder(
itemCount: count,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(text),
);
},
),
);
}
}
How should I make my user history page?

You can copy paste run full code below
You can put your click event in a History List and use ListView to show this History List
working demo
full code
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int count = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
Tile(text: Text("a")),
Tile(text: Text("b")),
Tile(text: Text("c")),
],
));
}
}
int count = 0;
List<History> historyList = [];
class History {
String data;
DateTime dateTime;
History({this.data, this.dateTime});
}
class Tile extends StatefulWidget {
final Text text;
Tile({this.text});
#override
TileState createState() => TileState();
}
class TileState extends State<Tile> {
#override
Widget build(BuildContext context) {
return ListTile(
title: widget.text,
onTap: () {
count++;
print(count);
historyList
.add(History(data: widget.text.data, dateTime: DateTime.now()));
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HistoryPage(),
));
},
);
}
}
class HistoryPage extends StatefulWidget {
#override
HistoryPageState createState() => HistoryPageState();
}
class HistoryPageState extends State<HistoryPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
})),
body: ListView.builder(
itemCount: historyList.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(
' ${historyList[index].data} ${historyList[index].dateTime.toString()}'),
);
},
),
);
}
}

Related

Flutter Navigation: how to make a routename as a funciton of an instance?

I want to make a new page which depends on a text input that a user typed in, so I want to make a routeName as a function of an instance, the following code doesn't work..
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: 'main',
routes: {
'main': (context) => MainPage(),
NodeInsideChat().routeName(): (context) => NodeInsideChat(),
},
);
}
}
Here You can see I'm trying to make routeName be newly genereated as an each page is created. But I have no idea what to pass inside NodeInsideChat()..
class MainPage extends StatefulWidget {
#override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
String wordInput;
#override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
TextField(
onChanged: (value) {
wordInput = value;
},
),
RawMaterialButton(
onPressed: () {
Navigator.pushNamed(context, NodeInsideChat(wordInput).routeName(),
arguments: NodeInsideScreenArguments(wordInput));
},
fillColor: Colors.red,
child: Text('Go to the new Page'),
),
],
);
}
}
class NodeInsideChat extends StatelessWidget {
NodeInsideChat(this.wordInput);
final String wordInput;
String routeName() {
return wordInput;
}
#override
Widget build(BuildContext context) {
final NodeInsideScreenArguments args =
ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFFFF8A80),
title: Text(
args.wordindex,
style: TextStyle(
fontSize: 20.0,
),
),
),
);
}
}
class NodeInsideScreenArguments {
final String wordindex;
NodeInsideScreenArguments(this.wordindex);
}
By ModalRoute or onGenerateRoute, I could not set the routeName as a function..

How do I add floatingactionbutton in my ListView in Flutter dart

I want to add a floatingactionbutton in my ListPage on the bottom right corner.
I tried adding it but I am getting error or it is becoming a dead code.
An on press will be implemented on that floatingactionbutton to create a user and that will be reflected in the listview page.
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
void main() => runApp(new AdminPage());
class AdminPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Admin Dashboard',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Admin Dashboard'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context){
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: ListPage(),
);
}
}
class ListPage extends StatefulWidget {
#override
_ListPageState createState() => _ListPageState();
}
class _ListPageState extends State<ListPage> {
Future _data;
Future getPosts() async {
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection("admins").getDocuments();
return qn.documents;
}
#override
Widget build(BuildContext context) {
Future getPosts() async {
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection("admins").getDocuments();
return qn.documents;
}
navigateToDetail(DocumentSnapshot post){
Navigator.push(context, MaterialPageRoute(builder: (context) => DetailPage(post: post,)));
}
#override
void initState(){
super.initState();
_data = getPosts();
}
return Container(
child: FutureBuilder(
future: _data,
builder: (_, snapshot){
if(snapshot.connectionState == ConnectionState.waiting){
return Center(
child: Text("Loading..."),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (_, index){
return ListTile(
title: Text(snapshot.data[index].data["email"]),
onTap: () => navigateToDetail(snapshot.data[index]),
);
});
}
}),
);
}
}
class DetailPage extends StatefulWidget {
final DocumentSnapshot post;
DetailPage({this.post});
#override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title : Text(widget.post.data["name"]),
),
body: Container(
child:Card(
child: ListTile(
title:Text(widget.post.data["email"]),
subtitle: Text(widget.post.data["name"]),
),
),
),
);
}
}
Image of the screen can be found below
You can add floatingActionButton argument on Scaffold
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: ListPage(),
floatingActionButton: FloatingActionButton(
onPressed: () =>{},
child: const Icon(Icons.add),
),
);
You can add FAB in listview by wrapping FloatingActionButton inside of Transform.translate:
floatingActionButton:Transform.translate(
offset: const Offset(-10, -70),
child: FloatingActionButton(
onPressed: () =>{},
child: const Icon(Icons.add),
),
),

How to translate an array with data into a widget

How to translate an array with data into a widget
array
[{id: 1, section_name: Name1, route: Gorod(), icon: Icons.location_city}, {id: 2, section_name: Name2, route: Gorod(), icon: Icons.chat}]
SearchData
void SearchData() {
info = new List.from(data);
for (int i = 0; i < info.length; i++) {
Widget routed = info[i]['route'];
Navigator.push(context, MaterialPageRoute(builder: (context) => routed));
// Widget test = Gorod();
// Navigator.push(context, MaterialPageRoute(builder: (context) => test));
}
}
an error comes out
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: type 'String' is not a subtype of type 'Widget'
file Gorod();
import 'package:flutter/material.dart';
class Gorod extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return GorodState();
}
}
class GorodState extends State<Gorod> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData (
color: Colors.white,
),
title: Text('Title Gorod', style: TextStyle(color: Colors.white)),
),
body: Container (
child: Text('Text fdsf fds fdsf'),
)
);
}
}
page code where I want to go
I want to take the path from the array and then substitute it and go to the page.
As can be seen from your error you are receiving string from your List.
There is no method to convert string to widget directly, so you have to manually check what you are getting from string by comparing and then you can create widget from it.
I hope Following minimal example will clear your idea.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var info = [
{
'id': 1,
'section_name': 'Name1',
'route': 'Gorod()',
'icon': 'Icons.location_city'
},
{
'id': '2',
'section_name': 'Name2',
'route': 'Gorod()',
'icon': 'Icons.chat'
}
];
List<Widget> searchData() {
List<Widget> _list = [];
for (int i = 0; i < info.length; i++) {
print(info[i]['route']);
if (info[i]['route'] == "Gorod()") {
_list.add(RaisedButton(
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Gorod()));
},
child: Text("text"),
));
}
}
return _list;
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Column(
children: searchData(),
),
),
);
}
}
class Gorod extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return GorodState();
}
}
class GorodState extends State<Gorod> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(
color: Colors.white,
),
title: Text('Title Gorod', style: TextStyle(color: Colors.white)),
),
body: Container(
child: Text('Text fdsf fds fdsf'),
));
}
}

Flutter navigator.push() object issue

While passing an object from one class to another class by using Navigator.push(), the object does not get modifying even its declared as not final.
Main Screen : Created an object(userBean) and passing to First screen
First Screen : displaying the same object(userBean) values, and passing again the same object(userBean) to second screen.
Second screen : trying to get modify the same object (userBean) in second screen, and printing the same object(userBean) in first screen by using refreshData.then method.
Main.dart
import 'package:flutter/material.dart';
import 'package:flutter_app_poc1/firstSceeen.dart';
import 'package:flutter_app_poc1/secondScreen.dart';
import 'package:flutter_app_poc1/userbean.dart';
void main() => runApp(MyApp());
typedef void refreshCallBack(int index);
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: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
UserBean user = new UserBean();
final List<String> hhList = ["General", "edu"];
int _counter = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new FlatButton(
child: Text("Next Screen"),
onPressed: () {
user.id = 1;
user.name = "Ramesh";
Future<dynamic> refreshData =
Navigator.of(context).push(new MaterialPageRoute<dynamic>(
builder: (BuildContext context) {
return new FirstScreen(userbean: user);
},
));
refreshData.then((_) {
});
}),
],
),
),
);
}
}
Firstscreen.dart
import 'package:flutter/material.dart';
import 'package:flutter_app_poc1/secondScreen.dart';
import 'package:flutter_app_poc1/userbean.dart';
typedef void refreshCallBack(int index);
class FirstScreen extends StatefulWidget {
UserBean userbean;
FirstScreen({Key key, this.userbean}) : super(key: key);
#override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
String userName;
final List<String> hhList = ["General", "edu"];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("first"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(widget.userbean.name),
new RaisedButton(onPressed: (){
Future<dynamic> refreshData =
Navigator.of(context).push(new MaterialPageRoute<dynamic>(
builder: (BuildContext context) {
return new SecondScreen(userbean: widget.userbean);
},
));
refreshData.then((_) {
print(widget.userbean.name);
});
}),
],
),
),
);
}
}
secondscreen.dart
import 'package:flutter/material.dart';
import 'package:flutter_app_poc1/userbean.dart';
class SecondScreen extends StatefulWidget {
UserBean userbean;
SecondScreen({Key key, this.userbean}) : super(key: key);
#override
_SecondScreenState createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
UserBean bean = UserBean();
#override
Widget build(BuildContext context) {
bean.name = "suresh";
return Scaffold(
appBar: AppBar(
title: Text("Previous Screen"),
),
body: Center(
child: new FlatButton(
child: Text(bean.name),
onPressed: () {
print(bean.name);
widget.userbean = bean;
Navigator.pop(context, true);
}),
));
}
}
#Murali
If you want to follow the same procedure pass object, then follow the below procedure.
From Navigator.pop push again new Object
onPressed: () {
print("TEST second screen :"+bean.name);
/// here modifying with new object.
widget.userbean = bean;
Navigator.pop(context, widget.userbean);
}),
In second screen Get new Object from Feature Method as below
Future<UserBean> refreshData =
Navigator.of(context).push(new MaterialPageRoute<UserBean>(
builder: (BuildContext context) {
return new SecondScreen(userbean: widget.userbean);
},
));
refreshData.then((res) {
print("TEST First screen : ${res.name}");
});
Then Object will change with new values.

Flutter -How to Pass variable from one dart class to another dart class

I just want to pass my int and bool values into another class in another dart file.
I am trying to pass values the method.
Try this.
import 'package:flutter/material.dart';
void main() => runApp(new MaterialApp(
home: new MainPage(),
));
class MainPage extends StatefulWidget {
#override
_MainPageState createState() => new _MainPageState();
}
class _MainPageState extends State<MainPage> {
int count = 0;
bool isMultiSelectStarted = false;
void onMultiSelectStarted(int count, bool isMultiSelect) {
print('Count: $count isMultiSelectStarted: $isMultiSelect');
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
backgroundColor: Colors.blue,
title: new Text('Home'),
),
body: new Center(
child: new RaisedButton(
child: new Text('Go to SecondPage'),
onPressed: () {
Navigator.of(context).push(
new MaterialPageRoute(
builder: (context) {
return new SecondPage(onMultiSelectStarted);
},
),
);
},
),
),
);
}
}
class SecondPage extends StatefulWidget {
int count = 1;
bool isMultiSelectStarted = true;
final Function multiSelect;
SecondPage(this.multiSelect);
#override
_SecondPageState createState() => new _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
#override
Widget build(BuildContext context) {
return new Center(
child: new RaisedButton(
child: new Text('Pass data to MainPage'),
onPressed: () {
widget.multiSelect(widget.count, widget.isMultiSelectStarted);
},
),
);
}
}