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
},
);
});
}
Related
im new to flutter and i wanted to know how can i retrieve a user data from firebase to my profile page?
my firebase data contain a name, email, blood type, and a date of birth. and i would like to retrieve these data to my app's profile page.
this is my profile page code
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:line_awesome_flutter/line_awesome_flutter.dart';
import '../Reminder/ui/theme.dart';
class ProfilePage extends StatefulWidget {
const ProfilePage({super.key});
#override
State<ProfilePage> createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
final user = FirebaseAuth.instance.currentUser!;
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
//leading: IconButton(onPressed: (){}, icon: const Icon(Icons.arrow_back_ios_new),),
centerTitle: true,
title: Text(
'Profile',
style: headingStyle,
),
backgroundColor: Get.isDarkMode ? Colors.grey[700] : Colors.white,
),
body: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(10),
child: Column(
children: [
SizedBox(
width: 120,
height: 120,
child: Image(image: AssetImage("images/profile.png")),
),
const SizedBox(height: 50),
Form(
child: Column(
children: [
TextFormField(
decoration: InputDecoration(
prefixIconColor: Get.isDarkMode?Colors.black:Colors.white,
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(100), borderSide: BorderSide(color: Get.isDarkMode?Colors.white:Colors.black,)),
labelText: "Email",
prefixIcon: Icon(LineAwesomeIcons.envelope_1, color: Get.isDarkMode?Colors.white:Colors.black),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(100))),
),
SizedBox(height: 10),
TextFormField(
decoration: InputDecoration(
prefixIconColor: Get.isDarkMode?Colors.black:Colors.white,
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(100), borderSide: BorderSide(color: Get.isDarkMode?Colors.white:Colors.black,)),
labelText: "Full Name",
prefixIcon: Icon(LineAwesomeIcons.user, color: Get.isDarkMode?Colors.white:Colors.black),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(100))),
),
SizedBox(height: 10),
TextFormField(
decoration: InputDecoration(
prefixIconColor: Get.isDarkMode?Colors.black:Colors.white,
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(100), borderSide: BorderSide(color: Get.isDarkMode?Colors.white:Colors.black,)),
labelText: "Date of Birth",
prefixIcon: Icon(LineAwesomeIcons.baby_carriage, color: Get.isDarkMode?Colors.white:Colors.black),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(100))),
),
SizedBox(height: 10),
TextFormField(
decoration: InputDecoration(
prefixIconColor: Get.isDarkMode?Colors.black:Colors.white,
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(100), borderSide: BorderSide(color: Get.isDarkMode?Colors.white:Colors.black,)),
labelText: "Blood Type",
prefixIcon: Icon(Icons.bloodtype, color: Get.isDarkMode?Colors.white:Colors.black),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(100))),
),
SizedBox(height: 15,),
SizedBox(
width: 100,
child: MaterialButton(
onPressed: () {
FirebaseAuth.instance.signOut();
},
color: Colors.redAccent,
child: Text('SIGN OUT'),
),
),
],
),
)
],
),
),
),
);
}
}
Use either StreamBuilder or FutureBuilder and get the data from firebase and display it accordingly
1. StreamBuilder
When you want to listen to changes constantly, and want the data to get updated without hot reload/restart
2. FutureBuilder
When you want to get the document only once and have no requirement of listening constantly to the change of the document.
Using StreamBuilder
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
stream: FirebaseFirestore
.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid) // 👈 Your document id which is equal to currentuser
.snapshots(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return const Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("Loading");
}
Map<String, dynamic> data =
snapshot.data!.data()! as Map<String, dynamic>;
return Text(data['fullName']); // 👈 your valid data here
},
),
),
);
}
Using FutureBuilder
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder<DocumentSnapshot<Map<String, dynamic>>>(
future: FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid) // 👈 Your document id which is equal to currentuser
.get(),
builder: (_, snapshot) {
if (snapshot.hasError) return Text('Error = ${snapshot.error}');
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("Loading");
}
Map<String, dynamic> data = snapshot.data!.data()!;
return Text(data['fullName']); //👈 Your valid data here
},
)),
);
}
You can use stram builder to retrieve data from the firebase and display it to the user on real time. It may have some issues with styling, since I wrote it without any IDE, but I hope you'll get the idea how to get your data from stream builder.
Example:
class ProfilePage extends StatefulWidget {
const ProfilePage({super.key});
#override
State<ProfilePage> createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children:[
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('your_collection')
.snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(
child: SizedBox(
height: 50,
width: 50,
child: CircularProgressIndicator(
color: Style.blueColorDark,
backgroundColor: Style.olive,
),
),
);
}else{
return ListView(
shrinkWrap: true,
children: snapshot.data!.docs.map((document) {
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16),
child: Card(
color: const Color.fromRGBO(
255, 255, 255, 1)
.withOpacity(0.6),
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(15),
),
child: Column(
children: <Widget>[
Text(data['name']),
Text(data['email']),
Text(data['bloodType']),
Text(data['dob']),
],
),
),
);
}).toList(),
);
}
}
)
]
)
}
}
I need to show the data that I add at the top, but it does not happen, I thought that the data is arranged according to the date it was added to the Firebase Firestore,
I think the data is sorted by document ID
After I almost finished the application, I could not solve this problem, I am not very skilled in Firebase and I did not find the solution
class News extends StatefulWidget {
News({Key? key}) : super(key: key);
#override
State<News> createState() => _NewsState();
}
class _NewsState extends State<News> {
final Stream<QuerySnapshot> data =
FirebaseFirestore.instance.collection('news').snapshots();
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromARGB(255, 102, 102, 102),
appBar: AppBar(
backgroundColor: Colors.blueAccent,
title: Center(
child: Text(
'أخبار الأنمي',
style: TextStyle(fontSize: 30),
),
),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: StreamBuilder<QuerySnapshot>(
stream: data,
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('حدث خطأ');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Column(
children: [
Center(child: Text('جاري التحميل')),
CircularProgressIndicator()
],
);
}
final anime = snapshot.requireData;
return ListView.builder(
physics: BouncingScrollPhysics(),
padding:
EdgeInsets.only(top: 3, left: 3, right: 3, bottom: 80),
itemCount: anime.size,
itemBuilder: (context, index) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.blue, width: 3),
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 3.5,
spreadRadius: 1,
)
],
),
margin: EdgeInsets.all(5),
child: Row(
children: [
Expanded(
flex: 3,
child: Container(
alignment: Alignment.centerRight,
child: Text(
textDirection: TextDirection.rtl,
'${anime.docs[index]['title']}',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w700),
),
)),
Expanded(
flex: 1,
child: Card(
child: Image.network(
'${anime.docs[index]['image']}',
fit: BoxFit.fill,
height: 120,
),
),
)
],
),
),
],
);
});
})),
);
}
}
Yes, firestore will return base on alphabetically sorting based on document ID.
Use the .orderBy("FieldToOrderBy") to return it in your desired order.
Order and limit data - Firestore
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];
}
}
Background: I'm writing an app in which you can store the plus points and fives of a class.
I do this with Flutter and Firebase. My Firestore database collection is called 'mates' and it has several documents, with a unique, auto-ID. Every document has three fields: a name(string), points(number), fives(number).
I have made this far with the help of this tutorial: https://www.youtube.com/watch?v=DqJ_KjFzL9I&t=591s
I want to modify the GUI a bit, in each listtile I display the name, button for decrementing points, the value of points, button for incrementing points, button for decrementing fives, value of fives, button for incrementing fives and a button for setting the value of fives to zero.
My question would be, that how can I refer to the fields(points, fives) of the database when changing their values?
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Widget _buildListItem(BuildContext context, DocumentSnapshot document){
return ListTile(
title: Row(
children: [
Expanded(
child: Text(
document["Name"],
style: Theme.of(context).textTheme.headline,
)
),
Container(
decoration: const BoxDecoration(
color: Color(0xffddddff)
),
padding: const EdgeInsets.all(10.0),
child: Text(
document['Points'].toString(),
style: Theme.of(context).textTheme.display1,
),
),
Container(
child: Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.keyboard_arrow_left),
iconSize: 30,
onPressed: (){
setState(() {
if(points!=0) { //I mean here
points--; //I mean here
}
});
}
),
Text(
"$points", //I mean here
style: TextStyle(
fontSize: 30
),
),
IconButton(
icon: Icon(Icons.keyboard_arrow_right),
iconSize: 30,
onPressed: (){
setState(() {
points++; //I mean here
if(points==10){ //I mean here
points=0; //I mean here
fives++; //I mean here
}
});
}
),
Text(
"$fives", //I mean here
style: TextStyle(
fontSize: 30
),
),
IconButton(
icon: Icon(Icons.check_circle),
iconSize: 30,
onPressed: (){
setState(() {
fives=0; //I mean here
});
}
),
]
)
),
],
),
);
}
#override
Widget build(BuildContext context) {
return Center(
child: Scaffold(
appBar: AppBar(
title: Text("Punkte++"),
centerTitle: true,
),
body: StreamBuilder(
stream: Firestore.instance.collection("mates").snapshots(),
builder: (context, snapshot) {
if(!snapshot.hasData) return const Text("Loading...");
return ListView.builder(
itemExtent: 80.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_buildListItem(context, snapshot.data.documents[index]),
);
}
),
)
);
}
}
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;
});
},
),
),
),