How to add highlighting border on buttonbar Flutter? - flutter

I have buttonbar with a few buttons, here is the code:
ButtonBar(
children: [
Container(
width: 40,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: <Color>[
Colors.purpleAccent,
Colors.pinkAccent,
],
stops: const <double>[
0.5,
0,
],
),
shape: BoxShape.circle,
),
child: ElevatedButton(
onPressed: () =>
setColors(Colors.purpleAccent, Colors.pinkAccent),
style: ElevatedButton.styleFrom(
shape: CircleBorder(),
primary: Colors.transparent,
shadowColor: Colors.transparent,
),
child: Ink())
),
...
)
I want to highlight selected button with border, something like this:
How can I do this with current architecture?

Try below code I have try using ListView.builder(), not ButtonBar hope its help to you.use StatefulWidget for below code
declare index and bool variable:
bool isPressed = false;
int? buttonIndex;
Your Widget:
Center(
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 10,
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.all(3),
padding: EdgeInsets.all(2),
height: 40,
width: 40,
decoration: BoxDecoration(
color: isPressed && buttonIndex == index
? Colors.primaries[index]
: Colors.transparent,
shape: BoxShape.circle,
),
child: Container(
padding: EdgeInsets.all(2),
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
child: ClipOval(
child: Container(
height: 40,
width: 40,
child: OutlinedButton(
style: OutlinedButton.styleFrom(
backgroundColor: Colors.primaries[index],
shape: CircleBorder(),
),
onPressed: () {
setState(() {
isPressed = !isPressed;
buttonIndex = index;
});
},
child: const SizedBox(),
),
),
),
),
);
},
),
),
Result Screen normal/without button click->
Result Screen with button click->
Test above code on Dartpad

Related

how can I change the surface tint color when I open a dialog in flutter

When I open a dialog in flutter I want the background to become a light blue with opacity as shown in the image below:
I've set the surfaceTintColor of my Dialog widget to the color I want, but it is not being applied. Instead I see the default elevation of the dialog. here is my code:
class PickImageDialog extends StatelessWidget {
const PickImageDialog({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Dialog(
surfaceTintColor: Theme.of(context).colorScheme.surfaceTint,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16))),
child: Padding(
padding: const EdgeInsets.fromLTRB(10, 16, 10, 22),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
IconButton(
onPressed: () {
context.router.pop();
},
icon: Icon(Icons.clear),
),
Expanded(
child: Text(
AppStrings.pickImage,
textAlign: TextAlign.center,
),
),
],
),
SizedBox(
height: 26,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
height: 60,
width: 60,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(16)),
border: Border.all(
width: 2, color: Theme.of(context).primaryColor),
color: Theme.of(context).colorScheme.surfaceTint),
child: SvgPicture.asset(
AppVectors.camera,
color: Theme.of(context).primaryColor,
height: 28,
),
), Container(
height: 60,
width: 60,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(16)),
border: Border.all(
width: 2, color: Theme.of(context).primaryColor),
color: Theme.of(context).colorScheme.surfaceTint),
child: SvgPicture.asset(
AppVectors.gallery,
color: Theme.of(context).primaryColor,
height: 28,
),
),
],
)
],
),
),
);
}
}
If you want to show a dialog you can use barrierColor property, like this:
InkWell(
child: Text('click'),
onTap: () {
showDialog(
context: context,
builder: (_) => PickImageDialog(),
barrierColor: Colors.red.withOpacity(0.4), // <--- add this
);
},
),
And if you want to blur the background you can wrap your Dialog with BackdropFilter in your PickImageDialog class:
BackdropFilter( // <--- add this
filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),// <--- add this
child: Dialog(
shape: RoundedRectangleBorder(
...
)
Salaam, For having a blur background first of all edit your PickImageDialog to this:
BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: Dialog(
// surfaceTintColor: Theme.of(context).colorScheme.surfaceTint,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16))),
child: Padding(
padding: const EdgeInsets.fromLTRB(10, 16, 10, 22),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(Icons.clear),
),
Expanded(
child: Text(
'AppStrings.pickImage',
textAlign: TextAlign.center,
),
),
],
),
SizedBox(
height: 26,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
height: 60,
width: 60,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(16)),
border: Border.all(
width: 2, color: Theme.of(context).primaryColor),
color: Theme.of(context).colorScheme.surfaceTint),
child: Container(color: Colors.red,),
), Container(
height: 60,
width: 60,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(16)),
border: Border.all(
width: 2, color: Theme.of(context).primaryColor),
color: Theme.of(context).colorScheme.surfaceTint),
child: Container(color: Colors.amber,),
),
],
)
],
),
),
),
);
by the way i didn't see any codes to show your dialog if you haven't implemented it before here is the code you need:
/// put it in your CTA(call to action) of showing dialog
showDialog(
context: context,
builder: (_) => PickImageDialog(),
);
happy coding after Polytechnic!

how to make child widget looks like its bigger then its parent widget in flutter

I want to make a custom navigationbar widget in Flutter. The blue circle widget size is bigger than its parent widget. But I don't know how to make it without any plugins. Please see the image for clarification.
class CustomNavBar extends StatelessWidget {
const CustomNavBar({final super.key});
#override
Widget build(final BuildContext context) {
return Stack(
alignment: Alignment.center,
children: [
Container(
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(16)),
),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
width: 150,
child: Row(
children: [
GestureDetector(
child: const Icon(
Icons.phone,
color: Color(0xFF24A3FF),
),
onTap: () {
// On phone tapped
},
),
const Spacer(),
GestureDetector(
child: const Icon(
Icons.contacts,
color: Color(0xFF24A3FF),
),
onTap: () {
// On contacts tapped
},
),
],
),
),
GestureDetector(
child: Container(
height: 50,
width: 50,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
border: Border.all(color: Color(0xFF24A3FF), width: 6),
),
child: Icon(UniconsLine.plus, color: Color(0xFF24A3FF)),
),
onTap: () {
// On plus tapped
},
),
],
);
}
}
You can use a Stack to achieve this effect
SizedBox(
height: 70,
width: MediaQuery.of(context).size.width *0.7,
child :Stack(
children: [
Center(
child : Container(
height : 40,
width : MediaQuery.of(context).size.width * 0.7,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color : Colors.red,
)
child: Row(
children : [
Icon(Icons.add),
Container(),
Icon(Icons.remove),
]
)
),
),
Center(
child: CircleAvatar(
radius : 70,
)
)
]
)
)

Flutter- adjust the custom button tap area

Widget customButton() {
return Container(
child: InkWell(
onTap: () {},
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
color: Colors.amber,
shape: BoxShape.circle,
),
child: Icon(Icons.auto_fix_normal),
),
),
);
}
I'm trying to create custom button with inkwell, but button can tapping from the outside of the button . How i can adjust the button tap area ?
Using customBorder: CircleBorder(), on InkWell fixed the splash effect, but to work it properly I am extending the snippet.
Widget customButton() {
return Container(
decoration: const BoxDecoration(
shape: BoxShape.circle,
),
child: Material(
color: Colors.orange,
shape: const CircleBorder(),
child: InkWell(
splashColor: Colors.black,
onTap: () {},
customBorder: const CircleBorder(),
child: Ink(
decoration: const BoxDecoration(shape: BoxShape.circle),
height: 100,
width: 100,
child: const Icon(Icons.holiday_village),
),
),
),
);
}

How to create a DraggableScrollableSheet with showModelBottomSheet?

I created with showModelBottomSheet with DraggableScrollableSheet(), But the problem is the there is no appBar for show some text while dragging, ie, the SingleChildsSrollView() will move up and down while dragging.
*bottomsheet(context) {
Size size = MediaQuery.of(context).size;
return showModalBottomSheet(
backgroundColor: Colors.white,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(13)),
),
context: context,
builder: (BuildContext context) {
return Padding(
padding: const EdgeInsets.all(5),
child: Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
foregroundColor: Colors.green,
backgroundColor: Colors.white,
elevation: 1,
shadowColor: Colors.grey.shade100,
automaticallyImplyLeading: false,
title: const Text('Connect with Reconnect'),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(
25,
),
child: Container(
padding: const EdgeInsets.only(
left: 15,
right: 15,
top: 15,
),
height: size.height,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(13),
boxShadow: const [
BoxShadow(blurRadius: 2, color: Colors.grey),
],
),
child: Column(children: [
TextField(
cursorColor: Colors.green,
decoration: InputDecoration(
label: const Text('Name'),
labelStyle: TextStyle(color: Colors.grey.shade500),
icon: const Icon(
FontAwesomeIcons.user,
color: Color(0xff453e3d),
size: 22,
),
border: InputBorder.none,
),
),
Container(
width: 315,
height: 1,
margin: const EdgeInsets.only(top: 3, bottom: 3),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(100),
),
),
TextField(
cursorColor: Colors.green,
decoration: InputDecoration(
label: const Text('Email'),
labelStyle: TextStyle(color: Colors.grey.shade500),
icon: const Icon(
FontAwesomeIcons.envelope,
color: Color(0xff453e3d),
size: 22,
),
border: InputBorder.none,
),
),
Container(
width: 315,
height: 1,
margin: const EdgeInsets.only(top: 3, bottom: 3),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(100),
),
),
TextField(
cursorColor: Colors.green,
decoration: InputDecoration(
label: const Text('Phone Number'),
labelStyle: TextStyle(color: Colors.grey.shade500),
icon: const Icon(
FontAwesomeIcons.mobileAlt,
color: Color(0xff453e3d),
size: 22,
),
border: InputBorder.none,
),
),
Container(
width: 315,
height: 1,
margin: const EdgeInsets.only(top: 3, bottom: 3),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(100),
),
),
Container(
margin: const EdgeInsets.only(top: 10),
height: 25,
alignment: Alignment.bottomLeft,
child: Row(
children: [
const Icon(
FontAwesomeIcons.car,
color: Color(0xff453e3d),
),
const SizedBox(
width: 15,
),
Text(
'Services',
style: TextStyle(
color: Colors.grey.shade500, fontSize: 16),
),
],
),
),
]),
),
),
),
),
);
});
}*
I need a bar on top of the singlechildscrollview, I tried with column instead, but no result.
The Problem will solve by the following code!
body: Center(
child: Column(
children: [
const SizedBox(
height: 400,
),
ElevatedButton(
onPressed: () =>
//The showModelBottomSheet Will push a sheet to our page for show widgets and actions
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (context) =>
//The DraggableScrollableSeet will drag top to bottom or vise versa with respect to user interaction.
DraggableScrollableSheet(
initialChildSize: .6,
maxChildSize: .9,
minChildSize: .2,
builder: (context, controller) =>
//The customsheet method will return a widget with a child CustoomScrollView to Drag our DraggableScrollableSheet.
customSheet(context, controller),
),
),
child: const Text('clickme'),
),
],
),
),
//customSheet body
Widget customSheet(BuildContext context, ScrollController controller) {
return Container(
alignment: Alignment.bottomLeft,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
// While use the CustomScrollView have an SliverAppBar for set our bar to constant, ie, the appBar will not move while dragging
child: CustomScrollView(
controller: controller,
slivers: [
SliverAppBar(
shadowColor: Colors.grey.shade100,
elevation: 1,
pinned: true,
backgroundColor: Colors.white,
automaticallyImplyLeading: false,
title: Container(
margin: const EdgeInsets.only(bottom: 45),
width: 60,
height: 4,
color: Colors.grey.shade300),
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.grey.shade200),
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
),
SliverFillRemaining(
hasScrollBody: false,
child: Column(
children: [
for (int i = 0; i < 10; i++)
Container(
width: 100,
height: 100,
color: Colors.green[i * 100],
),
],
),
)
],
),
);
}
Problem: The above code has a problem with an exit from the DraggableScrollableSheet(),
this is because if we use a GestureDetecter instead of DraggableScrollableSheet(), the child will pop by the following code
GestureDetector(onTap: ()=>Navigator.of(context).pop(),behavior: HitTestBehavior.opaque,
child: DraggableScrollableSheet(
initialChildSize: .6,
maxChildSize: .9,
minChildSize: .2,
builder: (context, controller) =>
customSheet(context, controller),
),
),
But we cannot pop the sheet with tapped by outside of the DraggableScrollabelSheet().
Note: the HitTestBehaviour.opaque will pop the sheet but if we tap anywhere in the Scaffold.
Thank You for following me up and If you have the solution, Please ping me #sureshm2suresh#gmail.com

Flutter How to get value from a custom widget

I have a container that color can change using a color list. I want to get the selected color of the container from the main page. When the container clicked, a list of colors appears and we can select the color for the container. I want to do is to get the color value from that container when I press a button
Widget build(BuildContext context) {
return Container(
height: 80,
width: 40,
padding: EdgeInsets.all(5),
child: InkWell(
onTap: () {
showDialog(
context: context,
child: Dialog(
backgroundColor: Colors.white,
// insetPadding: EdgeInsets.all(100),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
child: Column(
children: [
Container(
color: Colors.red,
height: 50,
alignment: Alignment(0, 0),
child: Text(
'Select the Color',
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 20),
),
),
ListView.builder(
shrinkWrap: true,
itemCount: colors.length,
itemBuilder: (context, index) {
return GestureDetector(
child: Container(
decoration: BoxDecoration(
//border: Border.all(),
),
padding: EdgeInsets.all(5),
child: Row(
children: <Widget>[
SizedBox(
width: 20,
),
Container(
decoration: BoxDecoration(
boxShadow: <BoxShadow>[
BoxShadow(blurRadius: 10)
],
border: Border.all(),
borderRadius:
BorderRadius.circular(100),
color: color[index]),
padding: EdgeInsets.all(5),
//color: color[index],
height: 45,
width: 45,
),
Padding(
padding: EdgeInsets.all(10),
),
Text(
colors[index],
style: TextStyle(
fontFamily:
GoogleFonts.inter().fontFamily,
color: color[index],
fontSize: 20.0,
shadows: [
Shadow(
// bottomLeft
offset: Offset(0, 1.5),
blurRadius: 5,
color: Colors.black),
],
),
),
],
),
),
onTap: () {
Navigator.pop(context);
setState(() {
selectedColor = color[index];
print(index);
});
},
);
})
],
)),
);
},
child: Container(
padding: EdgeInsets.all(10),
width: 20,
height: 60,
decoration: BoxDecoration(
color: selectedColor,
borderRadius: BorderRadius.circular(10),
),
),
),
);
}
You can pass the value to parent when you call Navigator.pop().
https://flutter.dev/docs/cookbook/navigation/returning-data
onTap: () {
Navigator.pop(context, color[index]);
setState(() {
selectedColor = color[index];
print(index);
});
},
In this case, you need to wait for result after call Navigator push method.
final result = await Navigator.push(
...