I have a Text widget on pressing which another Route has to be shown. But I could not see any onPressed() method for the Text widget. Please Help.
Just wrap your title in a GestureDetector to handle clicks. Then call Navigator's pushNamed to redirect to a new route.
new GestureDetector(
onTap: () {
Navigator.pushNamed(context, "myRoute");
},
child: new Text("my Title"),
);
Use InkWell
this gives you nice ripple effect as well
new InkWell(
onTap: () {
Navigator.pushNamed(context, "YourRoute");
},
child: new Padding(
padding: new EdgeInsets.all(10.0),
child: new Text("Tap Here"),
),
);
or
new FlatButton(
onPressed: () {
Navigator.pushNamed(context, "YourRoute");
},
child: new Text("Tap Here"),
)
For All the widget of Flutter you can implement onPressed using these widget
1. InkWell() : Using this widget you can add ripple effect on clicking
InkWell(
onTap: () {
Navigator.pushNamed(context, "write your route");
},
child: new Text("Click Here"),
);
2. GestureDetector() : Using this widget you can implement, onTap, onDoubleTap, onLongPress and many more
GestureDetector(
onTap: () {
Navigator.pushNamed(context, "write your route");
},
onLongPress: (){
// open dialog OR navigate OR do what you want
}
child: new Text("Save"),
);
You can use TextButton. Since it has a transparent background, it will look like a text widget.
TextButton(
onPressed: () {
//action
},
child: Text(
'Title Text', //title
textAlign: TextAlign.end, //aligment
),
),
Wrap your text with container and then again wrap your text with widget GestureDetector and use onTap() in the following way
onTap: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: ((context) => *className()*)
);
},
There are many ways to create clickable text.
InkWell
GestureDetector
TextButton
Widget clickableText(Function() onShow) {
return Column(
children: [
InkWell(
onTap: () {
onShow();
},
child: Center(
child: Text(
"Text one",
style: TextStyle(fontSize: 40),
),
),
),
GestureDetector(
onTap: () {
onShow();
},
child: Center(
child: Text(
"Text one",
style: TextStyle(fontSize: 40),
),
),
),
TextButton(
onPressed: () {
onShow();
},
child: Text(
"clickable text",
style: TextStyle(fontSize: 40),
))
],
);
}
Related
I want to place the card in the center
here the code
class _HomeState extends State<Home>{
#override
Widget build(BuildContext context) {
var myActivity=["Join Meeting","Create Meeting", "Schedule Meeting","Yet to be decided"];
var myGridView = new GridView.builder(
itemCount: myActivity.length,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemBuilder: (BuildContext context,int index) {
return new GestureDetector(
child: Card(
elevation: 5.0,
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.only(top: 10.0, bottom: 10.0, left: 10.0),
child: Text(myActivity[index]),
),
),
onTap: () {
showDialog(
barrierDismissible: false,
context: context,
child: CupertinoAlertDialog(
content: Text(myActivity[index],),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Ok"))
],
)
);
},
);
},
);
return Scaffold(
body: myGridView,
);
}
}
Two things required to do that first wrap Grid Widget inside Center Widget & give GridView property as shrinkWrap: true,
#override
Widget build(BuildContext context) {
print("In Test Widget");
// TODO: implement build
var myActivity=["Join Meeting","Create Meeting", "Schedule Meeting","Yet to be decided"];
var myGridView = new GridView.builder(
itemCount: myActivity.length,
shrinkWrap: true,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemBuilder: (BuildContext context,int index) {
return new GestureDetector(
child: Card(
elevation: 5.0,
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.only(top: 10.0, bottom: 10.0, left: 10.0),
child: Text(myActivity[index]),
),
),
onTap: () {
showDialog(
barrierDismissible: false,
context: context,
child: CupertinoAlertDialog(
content: Text(myActivity[index],),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Ok"))
],
)
);
},
);
},
);
return Scaffold(
body: Center(child: myGridView),
);
}
You can wrap your view(Card) with Row component and set mainAxisAlignment attribute to MainAxisAlignment.center like below.
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Card(
elevation: 5.0,
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.only(top: 10.0, bottom: 10.0, left: 10.0),
child: Text(myActivity[index]),
),
)
],
),
To display the GridView at the center of the screen, you can simply wrap myGridView inside a Center widget like this..
return Scaffold(
body: Center(child: myGridView),
);
You also need to set the GridView's shrinkWrap parameter to true. Otherwise gridView will take up the whole screen and so visually the Center widget will have no effect on its position.
Wrap your layout in a Column() and set MainAxisAlignment to center.
Column(
mainAxisAlignment: MainAxisAlignment.center,
child: <--YOUR EXISTING LAYOUT -->)
It can also center horizontally with crossAxisAlignment.
I am working on the todo list app and used CheckBox to check off the todo on the list.
But it keeps going back to the unchecked state on refreshing the page.
I want to save the state in the database.
I am populating the todoList in getAllTodos
Here is the code:
List<Todo>_todoList=List<Todo>();
#override
initState(){
super.initState();
getAllTodos();
}
getAllTodos()async{
_todoService=TodoService();
_todoList=List<Todo>();
var todos= await _todoService.readTodo();
todos.forEach((todo){
setState(() {
var model=Todo();
model.id=todo['id'];
model.title=todo['title'];
model.dueDate=todo['dueDate'];
model.category=todo['category'];
model.isFinished=todo['isFinished'];
_todoList.add(model);
});
});
}
body: ListView.builder(itemCount: _todoList.length,itemBuilder: (context, index){
return Padding(
padding: EdgeInsets.only(top:8.0, left: 8.0, right: 8.0),
child: Card (
elevation: 8.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0)
),
child: InkWell(
onTap: (){
setState(() {
_todoList[index].isChecked=!_todoList[index].isChecked;
});
},
child: ListTile(
leading: Checkbox(
checkColor: Colors.indigo,
value: _todoList[index].isChecked,
onChanged: (bool value){
setState(() {
_todoList[index].isChecked=value;
_todoService.saveTodo(_todoList[index]);
});
},
),
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(_todoList[index].title ?? 'No Title',
style: TextStyle(decoration: (_todoList[index].isChecked? TextDecoration.lineThrough: TextDecoration.none),
),
),
IconButton(icon: Icon(Icons.delete,color: Colors.red,
),
onPressed: (){
_deleteFormDialog(context,_todoList[index].id);
}
),
],
),
subtitle: Text(_todoList[index].dueDate ?? 'No Due Date'),
),
),
),
);
}),
Here is the isChecked value:
class Todo{
bool isChecked=false;
}
Please help me out.
Update: Added a line in the setState() of onChanged callback calling the service method to change the state of of the checkbox via _todoService.saveTodo(_todoList[index]);
Now the problem is that onChange() is called twice on a single tap. How do I correct the multi-calls in the onChange callback?
Follow the steps from here w.r.t checkbox manipulation.
Hopefully, everything works!
I have a menu in my flutter app that use a list of model for the navigation and it look like this:
When i click on one of the element on the list i want to folow one of the material route, since i did not make the menu and that i have not a lot of knowledge in flutter i have no idea how to do that or if it's even possible.
Every suggestion is welcom !!
Here is a view of the custom list:
class NavigationModel{
String title;
IconData icon;
NavigationModel({this.title,this.icon});
}
List<NavigationModel> navigationItems = [
NavigationModel(title: "Dashboard",icon: Icons.insert_chart),
NavigationModel(title: "Calendar",icon:Icons.calendar_today),
NavigationModel(title: "terrain",icon:Icons.landscape),
NavigationModel(title: "professeur",icon:Icons.person_pin),
NavigationModel(title: "joueur",icon:Icons.person_add),
];
sample of the menu builder:
children: <prefix0.Widget>[
SizedBox(
height: 50.0,
),
CollapsingListTile(
title: '$nickname',
icon: Icons.person,
animationController: _animationController,
),
Expanded(
child: ListView.builder(
itemBuilder: (context, counter){
return CollapsingListTile(
title: navigationItems[counter].title,
icon: navigationItems[counter].icon,
animationController: _animationController,
);
},
itemCount: navigationItems.length,
),
),
SizedBox(
height: 50.0,
)
],
Routes in the app:
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Recipes',
initialRoute: '/connection',
routes: {
'/': (context) => Connection(),
'/connection': (context) => Connection(),
'/connexion': (context) => Connexion(),
'/newclub': (context) => NewClub(),
},
);
I'm not familiar with that CollapsingListTile widget, I guess it expands when pressed upon? Anyways, for example if you wish to navigate to a route when pressing a ListTile widget, you would do this:
ListTile(
title: navigationItems[counter].title,
icon: navigationItems[counter].icon,
animationController: _animationController,
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => SomePage()));
)
In the end i get rid of the issue buy getting arround the problem.
What i did is put every element of my list in an InkWell().
With this solution the menu is unchanged and i can do any treatment i want in the onTap().
Expanded(
child: ListView.builder(
itemBuilder: (context, counter){
return new InkWell(
child: CollapsingListTile(
title: navigationItems[counter].title,
icon: navigationItems[counter].icon,
animationController: _animationController,
),
onTap: () {
print('button click: $counter');
}
);
},
itemCount: navigationItems.length,
),
),
SizedBox(
height: 50.0,
)
],
I am developing a flutter app, but it show error when I run the app.
I don't understand what is the problem. I think I mix up the logic of how the widget expand in layout.
Please kindly help to solve this issue.
error message:
flutter: The following assertion was thrown during performResize():
flutter: Vertical viewport was given unbounded height.
Viewports expand in the scrolling direction to fill their container.In this case, a vertical
viewport was given an unlimited amount of vertical space in which to expand. This situation
typically happens when a scrollable widget is nested inside another scrollable widget.
Here with my code:
body: Container(
child: Flexible(
child: FirebaseAnimatedList(
query: databaseReference,
itemBuilder: (_, DataSnapshot snapshot,
Animation<double> animation,
int index) {
return new Card(
color: Colors.black38,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ListTile(
leading: IconButton(
icon: Icon(Icons.format_list_bulleted),
color: Colors.blueAccent,
splashColor: Colors.greenAccent,
onPressed: () {
// Perform some action
debugPrint('button ok');
},
),
title: Text(shopList[index].shopName),
subtitle: Text(shopList[index].address),
),
Container(
child: Flexible(
child: Form(
key: formShopKey,
child: ListView(
children: <Widget>[
ListTile(
leading: Icon(
Icons.money_off,
color: Colors.white,
),
title: TextFormField(
maxLength: 100,
initialValue: "",
maxLines: 3,
//onSaved: (val) => booking.seafoodRequest = val,
//validator: (val) => val == "" ? val : null,
decoration: new InputDecoration(
),
),
),
],
),
),
),
),
ButtonTheme.bar(
// make buttons use the appropriate styles for cards
child: new ButtonBar(
children: <Widget>[
new FlatButton(
child: const Text('BUY TICKETS'),
onPressed: () {
/* ... */
},
),
new FlatButton(
child: const Text('LISTEN'),
onPressed: () {
/* ... */
},
),
],
),
),
],
),
);
},
),
),
);
[1]: https://i.stack.imgur.com/5vAsv.png
[2]: https://i.stack.imgur.com/LuZEl.png
I had to fill in a few gaps but the below should build for you. I also swapped FirebaseAnimatedList with a regular AnimatedList to get it to build. You can compare and adjust the layout.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo'),
);
}
}
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(
body: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
child: AnimatedList(
initialItemCount: 10,
itemBuilder: (BuildContext context, int index,
Animation<double> animation) {
return new Card(
color: Colors.black38,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ListTile(
leading: IconButton(
icon: Icon(Icons.format_list_bulleted),
color: Colors.blueAccent,
splashColor: Colors.greenAccent,
onPressed: () {
// Perform some action
debugPrint('button ok');
},
),
title: Text('Name'),
subtitle: Text('Address'),
),
Container(
constraints: BoxConstraints(
minHeight: 100.0,
maxHeight: 200.0,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expanded(
child: Form(
child: ListView(
children: <Widget>[
ListTile(
leading: Icon(
Icons.money_off,
color: Colors.white,
),
title: TextFormField(
maxLength: 100,
initialValue: "",
maxLines: 3,
//onSaved: (val) => booking.seafoodRequest = val,
//validator: (val) => val == "" ? val : null,
decoration: new InputDecoration(),
),
),
],
),
),
),
],
),
),
ButtonTheme.bar(
// make buttons use the appropriate styles for cards
child: new ButtonBar(
children: <Widget>[
new FlatButton(
child: const Text('BUY TICKETS'),
onPressed: () {
/* ... */
},
),
new FlatButton(
child: const Text('LISTEN'),
onPressed: () {
/* ... */
},
),
],
),
),
],
),
);
},
),
),
],
),
);
}
}
How can I achieve a simple bottom menu in Flutter? I want to show a set number of menu items and respond appropriately to clicks. I haven't been able to find anything in the gallery
Here is an example of what I'm trying to achieve, with custom options (not just media options)
maybe this could help https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/lib/demo/material/modal_bottom_sheet_demo.dart
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: const Text('Modal bottom sheet')),
body: new Center(
child: new RaisedButton(
child: const Text('SHOW BOTTOM SHEET'),
onPressed: () {
showModalBottomSheet<void>(context: context, builder: (BuildContext context)
{
return new Container(
child: new Padding(
padding: const EdgeInsets.all(32.0),
child: ListView(
children: <Widget>[
ListTile(title: Text('Map'),onTap:null),//handle on tap here
//build other menu here
],
);
)
textAlign: TextAlign.center,
style: new TextStyle(
color: Theme.of(context).accentColor,
fontSize: 24.0
)
)
)
);
});
}
)
)
);