Flutter - CircleAvatar with dynamic radius according to available space - flutter

I'd like the CircleAvatar to take a specific percentage (e.g. 50%) of the available space instead of specifying a fixed radius. How would you approach that?

I am not sure this is the best solution.
You can use LayoutBuilder to get the max constraints then build according to it.
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Container(
color: Colors.red,
height: 100.0,
width: 100.0,
child: _buildCircleAvatar(),
),
Container(
color: Colors.green,
height: 300.0,
width: 300.0,
child: _buildCircleAvatar(),
),
],
),
);
}
Widget _buildCircleAvatar() {
return LayoutBuilder(
builder: (context, constraints) {
final radius = min(constraints.maxHeight / 4, constraints.maxWidth / 4);
return Center(
child: CircleAvatar(
radius: radius,
backgroundImage: NetworkImage(
"https://as.ftcdn.net/r/v1/pics/7b11b8176a3611dbfb25406156a6ef50cd3a5009/home/discover_collections/optimized/image-2019-10-11-11-36-27-681.jpg",
),
),
);
},
);
}
}

Container(
color: Colors.green,
height: MediaQuery.of(context).size.height/2,
width: MediaQuery.of(context).size.width/2 ,
child:CircleAvatar(),
)
you can use this to get the responsive avatar

Related

How to clip overflowing column widget contained by stack contained by column

There are two main widgets:
I have this OverflowingWidget that is basically a Column - in reality this contains many widgets - However for this example, it has been given a height bigger than its parent widget.
The Parent widget ContainerWidget is basically AnimatedSwitcher which is basically a Stack contained by Column which constrained by a hight less than OverflowingWidget.
As you can see in dart-pad there is an overflow. The OverflowingWidget.
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: ContainerWidget(),
),
),
);
}
}
class ContainerWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.red,
height: 100,
width: 50,
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: ClipRect(
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: OverflowingWidget(),
layoutBuilder: (currentChild, previousChildren) => Stack(
children: [
...previousChildren,
if (currentChild != null) currentChild,
],
)),
),
),
],
));
}
}
class OverflowingWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Column(
children: [Container(color: Colors.blue, height: 150, width: 50)]);
}
}
Desired result:
I need the OverflowingWidget to be clean clipped without trying to resize the content inside. The same effect that can be obtained when in css:overflow:hidden.
AnimatedSwitcher is getting height 100, but it needs 150 for current widget. you can increase the height on redContainer.
Instead of using Column, you can use Stack widget.
class ContainerWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.red,
height: 150,
width: 50,
child: Stack(
children: [
ClipRect(
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: OverflowingWidget(),
layoutBuilder: (currentChild, previousChildren) => Stack(
children: [
...previousChildren,
if (currentChild != null) currentChild,
],
)),
),
],
));
}
}
I found a way to clip an overflowing column/row. Simply wrap that column/row with Wrap widget. That will do the clipping.
class OverflowingWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Wrap(children: [
Column(children: [
Container(
color: Colors.blue,
height: 150,
width: 50)
])
]);
}
}

how to make a stack widget take full screen on ios in flutter

I am trying to make an audio player app,
and I want to make the player screen fit the whole screen size.
However, the padding at the top and at the bottom doesn't help.
I tried to remove the SafeArea from bottomNavigationBar and other widgets and it didn't work.
How can I handle this?
Image of the player:
(the gray color padding doesn't let the image stretch to the end)
the code of the player:
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: const Color(0xff1c1c1e),
body: GetBuilder<OverlayHandler>(
builder: (getContext) {
if (!Get.find<OverlayHandler>().inPipMode) {
return Stack(
children:[
Container(...)
]
); // player at full screen
} else {
return Stack(...); // player at PiP mode
}
}
)
);
}
the code of the main screen widget:
Widget build(BuildContext context) {
return GetBuilder<NavigationController>(
builder: (controller) {
return Scaffold(
body: SafeArea(
// bottom option of this SafeArea doesn't affect the player size
child: IndexedStack(
index: controller.tabIndex,
children: const [
...
],
),
),
bottomNavigationBar: SafeArea(
// bottom option of this SafeArea doesn't affect the player size
child: SizedBox(
height: 80,
child: BottomNavigationBar(
items: [
...
],
),
),
),
);
}
);
}
}
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
#override
State<HomeScreen> createState() => _HomeScreenState();
}
TextEditingController controller = TextEditingController();
bool hasHash = false;
class _HomeScreenState extends State<HomeScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Container(
height: double.infinity,
decoration: const BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
"https://cdn.pixabay.com/photo/2016/09/10/11/11/musician-1658887_1280.jpg",
),
),
),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
height: 300,
width: double.infinity,
color: Colors.black.withOpacity(.7),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Icon(
Icons.skip_previous_rounded,
size: 55,
color: Colors.white,
),
Icon(
Icons.play_circle_fill_rounded,
size: 110,
color: Colors.white,
),
Icon(
Icons.skip_next_rounded,
size: 55,
color: Colors.white,
),
],
),
),
),
],
),
);
}
}
Android screenshot
iOS screenshot
Try removing the Scaffold()'s background color and add extendBody: true, or set the height of the container to height: double.infinity, or inside the stack just add and empty container with height as height: double.infinity,

How to positioned widget toLeftOf or toRightOf in Flutter

I first want to center one widget and then add another widget to the right of the center widget.
Currently, flutter APIs are not feasible for this. In android, it can be easily done in relative layout or constraint layout.
I tried with some hack with MediaQuery,
Stack(
children: [
Center(child: Text(17)),
Positioned(
bottom: 0,
left: (screenWidth / 2) + (screenWidth / 3),
child: Column(
children: []
)
)
How to try this in an easy way or a proper way.
You can use the Align widget for this.
Code Example
import 'package:flutter/material.dart';
class SandBox extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
width: 300,
height: 300,
color: Colors.blue,
child: Align(
alignment: Alignment.centerRight, //aligns the child widget to the right center
child: Text('17'),
),
),
),
);
}
}
Maybe, it can help you to achieve what you want:
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 30,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Text('17'),
),
Align(
alignment: Alignment.bottomRight,
child: Container(
width: 10,
height: 10,
decoration: BoxDecoration(
color: Colors.black,
shape: BoxShape.circle,
),
),
),
],
),
);
}
}
Or you can adjust it using some calculations using MediaQuery and Stack.
You can use badges package to achieve this goal.
Finally, I found used https://pub.dev/packages/align_positioned
AlignPositioned.relative(
Text(day.englishDate.toString(),
textScaleFactor: 3, style: dayTextStyle),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
....
],
),
moveByChildWidth: 1.1,
minChildWidthRatio: 1,
moveByChildHeight: 0.3)
Please use Align along with Row.
here is simple code to achieve this.
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Stack(
children:[
Align(
child : Row(
mainAxisAlignment: MainAxisAlignment.center,
children:[
Padding(child: Text('Left'),padding: EdgeInsets.all(5)),
Text('Center widget'),
Padding(child: Text('Right'),padding: EdgeInsets.all(5))
]) ,
alignment: Alignment.center),
]
),
),
);
}
}
Here is the output:

Flutter - How to expand the containers in a List View Builder to fill the entire space?

I put containers in the listview builder where, when clicked, each of the 7 containers plays a sound. How to I wrap the containers/CustomButtonWidget so all 7 containers fill the vertical space?
This is the code
import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
import 'package:flutter/painting.dart';
void main() => runApp(XylophoneApp());
class XylophoneApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Expanded(
child: ListView.builder(
itemCount: 7,
itemBuilder: (BuildContext context, int index) {
return CustomButtonWidget(index: index);
},
),
),
),
),
);
}
}
class CustomButtonWidget extends StatelessWidget {
final int index;
CustomButtonWidget({this.index});
void playSound(int soundNumber) {
final player = AudioCache();
player.play('note$soundNumber.wav');
}
#override
Widget build(BuildContext context) {
return TextButton(
style: ButtonStyle(
padding: MaterialStateProperty.all<EdgeInsets>(EdgeInsets.zero),
),
onPressed: () {
playSound(index + 1);
},
child: Container(
height: 80,
width: 90,
color: index.isOdd ? Color(0xffF8BBD0) : Color(0xffC2185B),
),
);
}
}
One way you can do this is like this.
child: Container(
height: MediaQuery.of(context).size.height/7,
width: 90,
color: index.isOdd ? Color(0xffF8BBD0) : Color(0xffC2185B),
),
Use the Expanded widget instead of Container.
You can set your width of Container to double.infinity:
child: Container(
height: 80,
width: double.infinity,
color: index.isOdd ? Color(0xffF8BBD0) : Color(0xffC2185B),
),
this is fill your horizontal space. Sorry I don't read your question carefully.
for fill vertical Space other friends gave Answer

Diagonal design of a container

I want to make a container styled as follows:
https://i.stack.imgur.com/ZPS6H.png
Having no idea how to do that I've tried to just incorporate SVG but it takes a different amount of time to render rectangles than to display SVG.
I've tried LinearGradient but even when I define stops it doesn't look right.
Here's what I have now:
Container(
width: width,
height: 0.7 * height,
child: Row(
children: [
Container(
height: 0.7 * height,
width: width * 0.35,
color: yellow,
child: CircularPhoto(),
),
Container(
width: width * 0.15,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/divider#2x.png'),
fit: BoxFit.fill,
),
),
),
Container(
width: width * 0.50,
color: Colors.white,
child: BannerInfo(),
),
],
),
);
This is an example!:
Maybe copy and paste it here to try it!: https://dartpad.github.io/
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height:200,
width:500,
child: Stack(
children:[
Container(
color:Colors.white
),
ClipPath(
child: Container(
width: MediaQuery.of(context).size.width,
color: Colors.yellow,
),
clipper: CustomClipPath(),
)
]
)
)
;
}
}
class CustomClipPath extends CustomClipper<Path> {
var radius=10.0;
#override
Path getClip(Size size) {
Path path = Path();
path.lineTo(0, 200);
path.lineTo(200,200);
path.lineTo(260,0);
path.lineTo(30, 0);
return path;
}
#override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}