Related
import 'package:flutter/material.dart';
import 'package:stone_recipe_app/homepage.dart';
import 'package:stone_recipe_app/models/recipe.dart';
import 'package:share_plus/share_plus.dart';
class DetailedScreen extends StatelessWidget {
DetailedScreen({Key? key, required this.recipe, required this.index}) : super(key: key);
List<Recipe>? recipe;
int index;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Row(
children: [
IconButton(
icon: const Icon(Icons.arrow_back_ios),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => HomePage()));
},
),
Text(
'Back',
style: TextStyle(color: Color(0xff007AFF)),
)
],
),
iconTheme: const IconThemeData(color: Color(0xff007AFF)),
title: Center(
child: Text(
recipe![index].recipe_name,
style: TextStyle(color: Colors.black),
),
),
backgroundColor: Colors.white,
elevation: 0,
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
children: [
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(200),
),
child: Container(
width: 400,
height: 200,
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(9.0)),
image: DecorationImage(
image: AssetImage("images/cook.jpg"),
fit: BoxFit.cover,
),
boxShadow: [
BoxShadow(
blurRadius: 5,
color: Colors.black,
)
],
),
),
),
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
const Icon(
Icons.file_download_sharp,
color: Colors.blue,
size: 40,
),
const Icon(
Icons.document_scanner,
size: 40,
),
IconButton(
onPressed: () {
Share.share(recipe![index].recipe_prep);
},
icon: const Icon(
Icons.share,
color: Colors.blue,
size: 40,
)),
const Icon(
Icons.favorite,
color: Colors.red,
size: 40,
),
]),
SizedBox(
height: 500,
child: Card(
elevation: 4,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Text(
'Ingredients',
style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
),
),
Column(
children: [
Text(recipe![index].recipe_ingrdients),
],
),
Center(
child: Text(
'Method',
style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
),
),
Text(
recipe![index].recipe_prep,
style: TextStyle(
fontSize: 20,
),
)
],
),
),
)
],
),
),
);
// Scaffold(
// appBar: AppBar(
// title: Text(recipe![index].recipe_name),
// ),
// body: Center(
// child: SingleChildScrollView(
// child: Column(
// mainAxisSize: MainAxisSize.min,
// children: [
// SelectableText(recipe![index].recipe_cat),
// Text(recipe![index].recipe_prep),
// Text(recipe![index].recipe_id.toString()),
// Text(recipe![index].recipe_ingrdients),
// Text(recipe![index].image_name),
// OutlinedButton(
// onPressed: () {
// Share.share(recipe![index].recipe_prep);
// },
// child: Text('Share'))
// ],
// ),
// ),
// ),
// );
}
}
import 'package:flutter/material.dart';
import 'package:stone_recipe_app/detailedScreen.dart';
import 'package:stone_recipe_app/models/recipe.dart';
import 'package:stone_recipe_app/services/db_services.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
#override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final dbservice = DataBaseService();
#override
void dispose() {
dbservice.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Recipe App')),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: FutureBuilder<List<Recipe>>(
future: dbservice.getRecipe(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Center(child: CircularProgressIndicator());
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data?.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailedScreen(
recipe: snapshot.data,
index: index,
)));
},
title: Text(snapshot.data![index].recipe_name, style: TextStyle(color: Colors.black)),
// trailing: Text(
// snapshot.data![index].recipe_cat,
// style: TextStyle(color: Colors.black),
// overflow: TextOverflow.fade,
// ),
);
},
);
},
)),
);
}
}
enter image description herei am trying to show data from sqlite db in flutter when user click on country category like india china after user click user navigate to other page which are recipe name related to chine or related to india i am trying to show data from sqlite db in flutter when user click on country category like india china after user click user navigate to other page which are recipe name related to chine or related to india enter image description here
add group_list_view: ^1.1.1 package in your pubspec.yaml file.
try below the example then implement it in your project
import 'package:flutter/material.dart';
import 'package:group_list_view/group_list_view.dart';
import 'package:group_listview_example/app_colors.dart';
void main() => runApp(MyApp());
Map<String, List> _elements = {
'Team A': ['Klay Lewis', 'Ehsan Woodard', 'River Bains'],
'Team B': ['Toyah Downs', 'Tyla Kane'],
'Team C': ['Marcus Romero', 'Farrah Parkes', 'Fay Lawson', 'Asif Mckay'],
'Team D': [
'Casey Zuniga',
'Ayisha Burn',
'Josie Hayden',
'Kenan Walls',
'Mario Powers'
],
'Team Q': ['Toyah Downs', 'Tyla Kane', 'Toyah Downs'],
};
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Group List View Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Group List View Demo'),
),
body: GroupListView(
sectionsCount: _elements.keys.toList().length,
countOfItemInSection: (int section) {
return _elements.values.toList()[section].length;
},
itemBuilder: _itemBuilder,
groupHeaderBuilder: (BuildContext context, int section) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 8),
child: Text(
_elements.keys.toList()[section],
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
),
);
},
separatorBuilder: (context, index) => SizedBox(height: 10),
sectionSeparatorBuilder: (context, section) => SizedBox(height: 10),
),
),
);
}
Widget _itemBuilder(BuildContext context, IndexPath index) {
String user = _elements.values.toList()[index.section][index.index];
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Card(
elevation: 8,
child: ListTile(
contentPadding:
const EdgeInsets.symmetric(horizontal: 18, vertical: 10.0),
leading: CircleAvatar(
child: Text(
_getInitials(user),
style: TextStyle(color: Colors.white, fontSize: 18),
),
backgroundColor: _getAvatarColor(user),
),
title: Text(
_elements.values.toList()[index.section][index.index],
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w400),
),
trailing: Icon(Icons.arrow_forward_ios),
),
),
);
}
String _getInitials(String user) {
var buffer = StringBuffer();
var split = user.split(" ");
for (var s in split) buffer.write(s[0]);
return buffer.toString().substring(0, split.length);
}
Color _getAvatarColor(String user) {
return AppColors
.avatarColors[user.hashCode % AppColors.avatarColors.length];
}
}
so I want to have a List/Array that contains images and where each image has his own title.
The image and the title should be shown in two different buttons and the title should change when I tap on the image.
I already could solve the first two points (to show in different places and that the title changes)
But I don't know how to add images to my List, could you help me?
In addition to that, the List should be randomized, but the title should always "stay" with his image.
I hope you understand what I want and thank you for your help.
class Sachen {
String title;
String image;
Sachen(this.title, this.image);
}
final List<Sachen> infoBank = [
Sachen("Chips", "images/snack1.png",),
Sachen("Erdnussflips", "images/snack2.png",),
];
int bankNumber = Random().nextInt(2) +1;
#override
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
Card(
margin: EdgeInsets.fromLTRB(50, 35, 50, 0),
elevation: 8,
color: Color(0xFF4caf50),
child: SizedBox(
height: 80,
width: 150,
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 18),
child: GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Rezept('Rezept'),
),
);
},
child: SizedBox(
height: 125,
width: 150,
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 18),
child: Text(
infoBank[bankNumber].title,
style: GoogleFonts.rubik(
fontSize: 20,
color: Colors.white,
),
),
),
),
),
),
),
),
),
),
Row(
children: [
Expanded(
child: TextButton(
onPressed: () {
setState(() {
bankNumber++;
});
changeDiceNumber();
print('LeftDiceNumber = $DiceNumber');
},
child: Container(
margin: EdgeInsets.fromLTRB(10, 20, 10, 20),
decoration: BoxDecoration(
border: Border.all(
width: 2,
color: Colors.grey.shade700,
),
),
height: 350,
width: 350,
child: Image.asset('images/snack$DiceNumber.png',
fit: BoxFit.cover,
),
),
),
),
],
),
],
),
);
}
}
have you tried using Flutter's ListViewBuilder?
Like alread said ListView().builder will be a good usage.
Instead of building the widget you can just use ListTile or Card there you already have trailing, leading, and title for the positioning of your images.
Althou i would change the class Sachen with a final Icon or IconData to directly add it to your List. In The Constructor please use required for the parameters its for better reading and don't do mistakes
This is my first idea to use this
class Sachen {
final String title;
final String image;
Sachen({
required this.title,
required this.image,
});
}
final List<Sachen> liste = [
Sachen(title: "title", image: "name"),
Sachen(title: "title1", image: "name1"),
];
return ListView.builder(itemBuilder: (context, index) {
return ListTile(
leading: Image.asset(liste[index].image),
title: Text(liste[index].title),
onTap: (){
//Do On Tap
//remember to use SetState when you want to rebuild some changes
},
);
});
Other Idea maybe a little bit better:
class Sachen {
final String title;
final Image image;
Sachen({
required this.title,
required this.image,
});
}
final List<Sachen> liste = [
Sachen(title: "title", image: Image.asset("name")),
Sachen(title: "title1", image: Image.asset("name1")),
];
#override
Widget build(BuildContext context) {
return ListView.builder(itemBuilder: (context, index) {
return ListTile(
leading: liste[index].image,
title: Text(liste[index].title),
onTap: () {
//Do On Tap
//remember to use SetState when you want to rebuild some changes
},
);
});
}
Screenshot here
I would like to change the color of my arrow (Icon Button) after querying my firebase (likedby) array. Essentially my upvote / downvote system works by adding the user to a likedby/dislikedby array on the post and I want the arrow color to change if the users id is in the likedby or dislikedby arrays.
class HomeScreen extends StatefulWidget {
final String currentUserId;
const HomeScreen({Key key, this.currentUserId}) : super(key: key);
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(backgroundColor: Color(0xffff5722), title: Text("Feed")),
body: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('The Laryngoscope')
.snapshots(),
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
return ListView(
children: snapshot.data.docs.map((document) {
return Center(
child: Padding(
padding: EdgeInsets.fromLTRB(15, 6, 15, 2),
child: Card(
elevation: 8.0,
child: Column(
children: [
ListTile(
title: Text(document['title'] + "\n"),
subtitle: Text(document['authors']),
),
Divider(),
ButtonBar(
children: [
Text(document['likes'].toString()),
IconButton(
icon: Icon(
FontAwesomeIcons.chevronUp,
color: Colors.grey,
size: 30,
),
onPressed: () async {
articleRef.doc(document['title']).update({
"dislikedby": FieldValue.arrayRemove(
[widget.currentUserId]),
"likedby": FieldValue.arrayUnion(
[widget.currentUserId])
});
var documents = (await articleRef
.doc(document['title'])
.get());
articleRef.doc(document['title']).update({
"likes":
(documents.data()['likedby'].length) -
(documents
.data()['dislikedby']
.length)
});
}),
IconButton(
icon: Icon(
FontAwesomeIcons.chevronDown,
color: Colors.grey,
size: 30,
),
onPressed: () async {
articleRef.doc(document['title']).update({
"likedby": FieldValue.arrayRemove(
[widget.currentUserId]),
"dislikedby": FieldValue.arrayUnion(
[widget.currentUserId])
});
var documents = (await articleRef
.doc(document['title'])
.get());
articleRef.doc(document['title']).update({
"likes": (documents
.data()['likedby']
.length) -
(documents.data()['dislikedby'].length)
});
},
),
OutlinedButton(
style: ElevatedButton.styleFrom(
primary: Colors.white, // background
onPrimary: Color(0xffff5722), // foreground
),
onPressed: () {},
child: Text('Comment'),
),
FloatingActionButton(
child: Icon(Icons.share),
backgroundColor: Color(0xFFFF5722),
onPressed: () {},
),
],
)
],
),
),
),
);
}).toList(),
);
}),
);
}
}
So I have a ListView of ListTiles and every tile has a checkbox in it. I can change the state of the checkboxes just fine but the proper animation does not show. What am I missing here? Thanks!
Widget _buildListTile(BuildContext context, Task item) {
return Dismissible(
// Show a red background as the item is swiped away
background: Container(color: Colors.red),
key: Key(item.hashCode.toString()),
onDismissed: (direction) {
setState(() {
tasks.remove(item);
});
},
child: ListTile(
leading: Checkbox(
value: item.checked,
onChanged: (bool newValue) {
setState(() {
item.checked = newValue;
});
},
activeColor: Colors.blue,
),
title: InkWell(
onTap: () {
editTask(item);
},
child: Text(
item.task,
style: TextStyle(fontSize: 18),
)),
contentPadding: EdgeInsets.all(8),
//trailing: Icon(Icons.drag_handle),
),
);
}
I want to create a round CheckBox like this
I've tried multiple variations of this, but none of them seem to work. Including I tried to use ClipRRect .
Because there are more code, I only select part of it to show here.
new Row(
children: <Widget>[
//new ClipRRect(
// borderRadius: BorderRadius.all(Radius.circular(90.0)),
// child:
new Checkbox(
tristate: true,
value: true,
onChanged: (bool newValue){
setState(() {
});
},
activeColor: Color(0xff06bbfb),
),
// ),
new Expanded(
child: new Text('将此手机号码和QQ号绑定,提高账号安全性。',
style: new TextStyle(
color: Color(0xff797979)
),
)
),
],
),
I am new to Flutter.Thanks in advance.
The Material Design checkbox has a shape property and you can set CircleBorder() to it and it will be round.
Checkbox(
checkColor: Colors.white,
fillColor: MaterialStateProperty.resolveWith(getColor),
value: isChecked,
shape: CircleBorder(),
onChanged: (bool? value) {
setState(() {
isChecked = value!;
});
},
);
You can try & play with this Code: Round CheckBox
bool _value = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Circle CheckBox"),
),
body: Center(
child: InkWell(
onTap: () {
setState(() {
_value = !_value;
});
},
child: Container(
decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.blue),
child: Padding(
padding: const EdgeInsets.all(10.0),
child: _value
? Icon(
Icons.check,
size: 30.0,
color: Colors.white,
)
: Icon(
Icons.check_box_outline_blank,
size: 30.0,
color: Colors.blue,
),
),
),
)),
);
}
Since Flutter 2.2.0, the shape property has been introduced in the Checkbox Material Class.
Example for adding round corners to a checkbox:
Checkbox(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
)
Copy the code of the Material Checkbox and create your new Checkbox widget.
In this widget change the variable const Radius _kEdgeRadius = Radius.circular(1.0) to Radius.circular(100).
I had a hard time building this and came up with this solution. I thought someone like me would need it one day. So here it is.
ClipRRect(
clipBehavior: Clip.hardEdge,
borderRadius: BorderRadius.all(Radius.circular(5)),
child: SizedBox(
width: Checkbox.width,
height: Checkbox.width,
child: Container(
decoration: new BoxDecoration(
border: Border.all(
width: 1,
),
borderRadius: new BorderRadius.circular(5),
),
child: Theme(
data: ThemeData(
unselectedWidgetColor: Colors.transparent,
),
child: Checkbox(
value: isWinnerTakesAll,
onChanged: (state) =>
setState(() => isWinnerTakesAll = !isWinnerTakesAll),
activeColor: Colors.transparent,
checkColor: CommonColors.checkBoxColor,
materialTapTargetSize: MaterialTapTargetSize.padded,
),
),
),
),
),
I hope this solves your problem! Any questions are welcomed.
Reason for using Theme widget
Without the theme widget, the checkbox had its original square box in the unselected state and I removed it with making it transparent.
BE AWARE: THIS PACKAGE IS DISCONTINUED !
There is a simple package you can add that keeps the functionality and animation of the checkbox: https://pub.dev/packages/circular_check_box
Implementation after installing and importing package:
CircularCheckBox(
value: someBooleanValue,
materialTapTargetSize: MaterialTapTargetSize.padded,
onChanged: (bool x) {
someBooleanValue = !someBooleanValue;
}
),
Set up the Theme like this:
Theme(
data: ThemeData(
checkboxTheme: CheckboxThemeData(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
),
);
With Flutter 2.2.1 shape property is added. so its easy to add change shape of checkbox.
Here I have created a circular checkbox widget:
import 'package:flutter/material.dart';
class CircularCheckbox extends StatelessWidget {
final bool? value;
final ValueChanged<bool?>? onChanged;
const CircularCheckbox({Key? key, required this.value, required this.onChanged}) : super(key: key);
#override
Widget build(BuildContext context) {
return Transform.scale(
scale: 1.3,
child: Checkbox(
value: value,
onChanged: onChanged,
),
);
}
}
you can scale the checkbox to respect to your design.
for theming the checkbox:
Update themeData of MaterialApp like this:
MaterialApp(
theme: ThemeData(toggleableActiveColor: colorScheme.primary,
checkboxTheme: Theme.of(context).checkboxTheme.copyWith(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
side: BorderSide(
width: 1.5, color:
Theme.of(context).unselectedWidgetColor),
splashRadius: 0),)
)
by applying the theme you have a nice circular checkbox:
Checkbox(
value: _bigBox,
//Simply add this line
shape: CircleBorder(),
splashRadius: 20,
onChanged: (value) {
_bigBox = value;
setState(() {});
},
),
I had a hard time building this and came up with this solution. I thought someone like me would need it one day. So here it is.
InkWell(
onTap: () {
setState(() {
selected = !selected;
});
},
child: Container(
height: 28,
width: 28,
decoration: BoxDecoration(
shape: BoxShape.circle, color: selected ? Colors.green : Colors.white,
border: Border.all(width: 2, color: Colors.green)
),
child: Padding(
padding: const EdgeInsets.all(3.0),
child: selected ? Icon(
Icons.check,
size: 18.0,
color: Colors.white,
) : Container()
),
),
),
We can use only material icons to achieve this as below:
bool checkboxValue = false;
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
onTap: () {
setState(() {
checkboxValue = !checkboxValue;
});
},
child: checkboxValue
? Icon(
Icons.radio_button_checked,
color: Colors.blue,
size: 20,
)
: Icon(
Icons.radio_button_unchecked,
color: Colors.grey,
size: 20,
),
),
SizedBox(width: 5),
Text("Accept Terms and Conditions"),
],
),
Checkbox(
checkColor: Colors.white,
fillColor: MaterialStateProperty.resolveWith(getColor),
value: isChecked,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(2)),
onChanged: (bool? value) {
setState(() {
isChecked = value!;
});
},
);
You can simply set the shape of the Checkbox as below;
Checkbox(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
)
link to documentation
Why do you complicate it with ClipRect ? Just use the official material icon.
try this:
import 'package:flutter/material.dart';
class CircleCheckbox extends StatelessWidget {
final bool value;
final ValueChanged<bool> onChanged;
final Color activeColor;
final Color checkColor;
final bool tristate;
final MaterialTapTargetSize materialTapTargetSize;
CircleCheckbox({
Key key,
#required this.value,
this.tristate = false,
#required this.onChanged,
this.activeColor,
this.checkColor,
this.materialTapTargetSize,
}) : assert(tristate != null),
assert(tristate || value != null),
super(key: key);
#override
Widget build(BuildContext context) {
return ClipOval(
child: SizedBox(
width: Checkbox.width,
height: Checkbox.width,
child: Container(
decoration: new BoxDecoration(
border: Border.all(
width: 2,
color: Theme.of(context).unselectedWidgetColor ??
Theme.of(context).disabledColor),
borderRadius: new BorderRadius.circular(100),
),
child: Checkbox(
value: value,
tristate: tristate,
onChanged: onChanged,
activeColor: activeColor,
checkColor: checkColor,
materialTapTargetSize: materialTapTargetSize,
),
),
),
);
}
}
Assuming you have a boolean variable acceptTerms, you can implement it in Flutter 2.2 like this. Make sure you save your checkbox state too:
Checkbox(
value: acceptTerms,
onChanged: (value) {
setState(() {
acceptTerms = !acceptTerms;
});
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
),
Container(
decoration: BoxDecoration(
border: Border.all(
color: (checkedValue) ? Colors.red : Colors.blue,
width: 1,
),
borderRadius: BorderRadius.all(
Radius.circular(5),
)),
width: 24,
height: 24,
child: Theme(
data: ThemeData(
unselectedWidgetColor: Colors.transparent,
),
child: Checkbox(
activeColor: Colors.transparent,
checkColor: Colors.red,
value: checkedValue,
tristate: false,
onChanged: (bool isChecked) {
setState(() {
checkedValue = isChecked;
});
},
),
),
),