SizedBox in flutter has 0 argument? - flutter

I'm a beginner at flutter. I'm practicing to make in Scaffold.
In my code, the last area that Sized Box has errors in child and elevation, and color.
I don't understand why they are used in not correctly, Could you explain that to me?
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home:Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(Icons.menu, color: Colors.pink,),
onPressed: () {
print("menu button is clicked");
}
),
title: IconButton(
onPressed: () { print("이미지버튼 동작"); },
icon: Image.asset('assets/images/Slimer_by_shinrider-dcqaiza.webp')),
),
body: Container(
width: 100,
height: 200,
margin: EdgeInsets.all(5.0),
decoration: BoxDecoration(
color: Colors.pinkAccent,
border: Border.all(color: Colors.purple)
),
),
child: SizedBox(
height: 80, width: 100,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0)
),
),
elevation: 4.0,
color: Colors.red
)
)
);
}
}

You have closed Container and Card early on. Please check below against yours and use below to fix the issue.
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(
Icons.menu,
color: Colors.pink,
),
onPressed: () {
print("menu button is clicked");
}),
title: IconButton(
onPressed: () {
print("이미지버튼 동작");
},
icon: Image.asset(
'assets/images/Slimer_by_shinrider-dcqaiza.webp')),
),
body: Container(
width: 100,
height: 200,
margin: EdgeInsets.all(5.0),
decoration: BoxDecoration(
color: Colors.pinkAccent,
border: Border.all(color: Colors.purple)),
child: SizedBox(
height: 80,
width: 100,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0)),
elevation: 4.0,
color: Colors.red)))));
}
}

your body is Container, which is only need 1 Widget as child.
if you want to use more than 1 Widget , you need to wrap your children that provide List<Widget> as their children.
such as : Column, Row , ListView, etc.
What you miss is you add 1 close bracket in your code
body: Container(
width: 100,
height: 200,
margin: EdgeInsets.all(5.0),
decoration: BoxDecoration(
color: Colors.pinkAccent,
border: Border.all(color: Colors.purple)
),
), <= here your error , you close your container , so your child not included inside the container.
remove what i notice above

Related

Stack with circular progress bar and elevated button in flutter

I'm wanted to achieve the following UI in the screenshot and achieved by the below code on two different phones. But the same is not working on many other devices. Is there a way to handle the size of CircularProgressIndicator(currently I'm using Transform.scale() which is not giving me generic result) so that it fits around the sibling container inside the stack?
Edit:
The reason for using CircularProgressIndicator is to show progress.
Widget build(BuildContext context) {
final mediaq = MediaQuery.of(context).size;
return Stack(alignment: Alignment.center, children: [
Transform.scale(
scale: mediaq.width <= 360 ? 2.35 : 2.70,
child: const CircularProgressIndicator(
backgroundColor: Colors.white,
color: Colors.orange,
strokeWidth: .75,
value: 1,
),
),
Container(
decoration: const BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
colors: [Colors.red, Colors.orange],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.transparent,
fixedSize: Size(mediaq.width * .24, mediaq.height * .12),
shape: const CircleBorder(side: BorderSide.none),
),
onPressed: () {},
child: const Text("child")),
),
]);
}
Re wrote your code and here is my output
Transform.scale(
scale: mediaq.width> 375 ? 3.10 : 2.35,
child: const CircularProgressIndicator(
backgroundColor: Colors.white,
color: Colors.white,
strokeWidth: .75,
value: 1,
),
),
Output:
Iphone 12 Pro Max (W:428 , H:926)
Iphone SE (Second Generation) (W:375, H: 667)
Also you can try this package for responsive ui.
You can use flutter_screenutil package to make your UI more responsive, instead of MediaQuery.
I have done your screen using flutter_screenutil package.
Please try this out. I assumed the heights, widths, and radius. You can modify these as per your requirement. I have also attached the UI I got.
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return ScreenUtilInit(
designSize: const Size(375, 667),
minTextAdapt: true,
builder: () {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const ButtonTest(),
);
});
}
}
class ButtonTest extends StatefulWidget {
const ButtonTest({Key? key}) : super(key: key);
#override
_ButtonTestState createState() => _ButtonTestState();
}
class _ButtonTestState extends State<ButtonTest> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Container(
height: 94.h, // You can change here
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.white,
),
),
child: Padding(
padding: EdgeInsets.all(
2.0.w, // You can change here
),
child: GestureDetector(
onTap: () {
print("Button Pressed");
},
child: Container(
height: 92.h, // You can change here
width: 92.h, // You can change here
decoration: const BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
colors: [
Colors.red,
Colors.orange,
],
),
),
child: const Center(
child: Text(
'child',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
)),
),
),
),
),
),
);
}
}

Flutter: Place widget on top of the other with negative positioning

I am trying to implement this modalBottomSheet
and I thought to use Stack with negative positioning
showModalBottomSheet(
context: context,
builder: (context) {
return Stack(
alignment: AlignmentDirectional.bottomStart,
children: [
Container(
width: double.infinity,
height: 200,
color: Colors.white,
child: Column(//here there will be the text)
),
Positioned(
top: -20,
left: 0,
right: 0,
child: CircleAvatar(
backgroundColor: Palette.white,
radius: 38,
child: CircleAvatar(
backgroundImage: NetworkImage(snapshot.data.image),
radius: 34,
backgroundColor: Palette.white),
),
)]);
However this cuts the top part of the CircleAvatar picture (as I kind of expected).
Any idea on how to implement this?
you need to change the clipBehavior to Clip.none on the Stack
Stack(
clipBehavior: Clip.none,
...)
Here is my tricky implementation.
Set bottom sheet's background color to transparent.
Add top margin as avatar's half height to base Container.
Set base container's background color to white
Change the avatar's top position value to 0
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(),
floatingActionButton: FloatingActionButton(
onPressed: () {
show();
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
void show() {
showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
builder: (context) {
return Stack(
alignment: AlignmentDirectional.bottomStart,
children: [
Container(
width: double.infinity,
height: 200,
margin: EdgeInsets.only(top: 35),
color: Colors.transparent,
child: Container(
padding: EdgeInsets.only(top: 60),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Alessandro Liparoti',
style: TextStyle(
color: Colors.black,
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 25),
Text(
'4 games palyed',
style: TextStyle(
color: Colors.grey,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
],
),
),
),
Positioned(
top: 0,
left: 0,
right: 0,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 38,
child: CircleAvatar(
backgroundImage: NetworkImage(
'https://image.flaticon.com/icons/png/512/147/147144.png'),
radius: 34,
backgroundColor: Colors.white),
),
),
],
);
});
}
}

Flutter reuse custom widget

Let's say I created a custom widget that emulates button functions.
#override
Widget build(BuildContext context) {
return RawMaterialButton(
onPressed: () {},
elevation: 2.0,
fillColor: Colors.lightGreenAccent,
child: SizedBox (
width: 90 ,
height: 90,
child: Column (
children: [
Icon(Icons.face,
size: 50,
color: Colors.red,),
Text('Enable ->'),
Container(
height: 20,
width: 80,
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: new BorderRadius.all(Radius.elliptical(45, 10)),
),
),
],
),
),
padding: EdgeInsets.all(15.0),
shape: CircleBorder(),
);
}
How can I create a group of such widgets, but so that each has a different icon or text by reusing my widget?
You can customize your widget like this:
import 'package:flutter/material.dart';
class CustomWidget extends StatelessWidget {
CustomWidget({this.icon, this.text});
final Widget icon;
final Widget text;
#override
Widget build(BuildContext context) {
return RawMaterialButton(
onPressed: () {},
elevation: 2.0,
fillColor: Colors.lightGreenAccent,
child: SizedBox (
width: 90 ,
height: 90,
child: Column (
children: [
icon,
text,
Container(
height: 20,
width: 80,
decoration: new BoxDecoration(
color: Colors.red,
borderRadius: new BorderRadius.all(Radius.elliptical(45, 10)),
),
),
],
),
),
padding: EdgeInsets.all(15.0),
shape: CircleBorder(),
);
}
}

How Can I get the border Side color in flutter

I am designing a simple screen. I want to acheive this
How can I remove the topLeft border. I try to using border side to show only one side border but it showing error not able to change the another then uniform. Did i remove the app bar then stack the widget. Kindly tell me which thing I am doing wrong.
My Code is
import 'package:flutter/material.dart';
class SendScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
final GlobalKey<ScaffoldState> _scaffoldKey =
new GlobalKey<ScaffoldState>();
return Scaffold(
drawer: Drawer(),
key: _scaffoldKey,
appBar: PreferredSize(
preferredSize: Size.fromHeight(100.0),
child: AppBar(
leading: IconButton(
icon: Icon(Icons.access_alarm),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
}),
actions: [
Container(
width: 50,
decoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: const DecorationImage(
image: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg'),
fit: BoxFit.cover,
),
border: Border.all(
color: Colors.black,
// width: 8,
),
borderRadius: BorderRadius.circular(12),
),
),
SizedBox(
width: 20,
)
],
shape: ContinuousRectangleBorder(
borderRadius: BorderRadius.only(
// bottomLeft: Radius.circular(30),
bottomRight: Radius.circular(80),
),
),
title: Text('Sliver AppBar'),
),
),
body: Column(
children: [
Container(
height: 80,
decoration: BoxDecoration(
border: Border.all(
color: Colors.red[500],
),
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(80),
),
),
child: Row(
children: [],
),
)
],
),
);
}
}
This is a known "issue". The problem is that once you assign a border radius, you don't know where the border starts and where it ends. Therefore flutter can't assign a border color to only certain border.
The only trick which works for you situation of everything I tried is to stack 2 container, the one on the back acting to do the border.
Here is the implementation:
import 'package:flutter/material.dart';
main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SendScreen(),
),
);
}
}
class SendScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
return Scaffold(
drawer: Drawer(),
key: _scaffoldKey,
appBar: PreferredSize(
preferredSize: Size.fromHeight(100.0),
child: AppBar(
leading: IconButton(
icon: Icon(Icons.access_alarm),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
}),
actions: [
Container(
width: 50,
decoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: const DecorationImage(
image: NetworkImage(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg'),
fit: BoxFit.cover,
),
border: Border.all(
color: Colors.black,
// width: 8,
),
borderRadius: BorderRadius.circular(12),
),
),
SizedBox(
width: 20,
)
],
shape: ContinuousRectangleBorder(
borderRadius: BorderRadius.only(
// bottomLeft: Radius.circular(30),
bottomRight: Radius.circular(80),
),
),
title: Text('Sliver AppBar'),
),
),
body: CustomRectangle(
borderSize: 20,
borderColor: Colors.grey[300],
height: 80.0,
borderRadius: 80.0,
child: Container(
color: Colors.red,
),
),
);
}
}
class CustomRectangle extends StatelessWidget {
final Widget child;
final double height;
final double borderSize;
final double borderRadius;
final Color borderColor;
const CustomRectangle({
Key key,
#required this.height,
#required this.borderSize,
#required this.borderRadius,
#required this.child,
#required this.borderColor,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Stack(
children: [
ClipRRect(
borderRadius: BorderRadius.only(bottomRight: Radius.circular(borderRadius)),
child: Container(
height: height + borderSize,
decoration: BoxDecoration(
color: borderColor,
),
),
),
ClipRRect(
borderRadius: BorderRadius.only(bottomRight: Radius.circular(borderRadius)),
child: Container(
height: height,
child: child,
),
),
],
);
}
}
Look at CustomRectangle it is my custom widget which solve your problem. You can expand from there.

Flutter - Button Group style and position

I am trying to create something like the attached image. I got this far ...
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(32),
topRight: Radius.circular(32),
),
),
child: ButtonTheme(
child: ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () => print('hi'),
child: Text('Referals'),
color: Color(0xff2FBBF0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(15.0),
topLeft: Radius.circular(15.0)),
),
),
RaisedButton(
onPressed: () => print('hii'),
child: Text('Stats'),
color: Color(0xff2FBBF0),
),
RaisedButton(
onPressed: () => print('hiii'),
child: Text('Edit Profile'),
color: Color(0xff2FBBF0),
// color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(15.0),
topRight: Radius.circular(15.0)),
),
),
],
),
),
),
),
But I don't really feel like it will look like the image.
I would also like the button group to be at the top of the Container. Now they're in the absolute center. Just like they would be if wrapped in a Center widget.
Here's the complete code. I have just used Container and Row because I find it more suitable and easy to achieve without any headache. :P
If you want with RaisedButton, figure it out.
Source:
import 'package:flutter/material.dart';
class Demo extends StatefulWidget {
#override
_DemoState createState() => new _DemoState();
}
class _DemoState extends State<Demo> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("DEMO")),
body: Padding( // used padding just for demo purpose to separate from the appbar and the main content
padding: EdgeInsets.all(10),
child: Container(
alignment: Alignment.topCenter,
child: Container(
height: 60,
padding: EdgeInsets.all(3.5),
width: MediaQuery.of(context).size.width * 0.9,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(15)),
),
child: Row(
children: <Widget>[
Expanded(
child: InkWell(
onTap: () {},
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(12),
topLeft: Radius.circular(12))),
child: Text("Referrals",
style: TextStyle(
color: Colors.blue,
fontSize: 17,
)),
))),
Expanded(
child: InkWell(
onTap: () {},
child: Container(
alignment: Alignment.center,
child: Text("Stats",
style: TextStyle(
color: Colors.white, fontSize: 17)),
))),
Padding(
padding: EdgeInsets.symmetric(vertical: 5),
child: Container(color: Colors.white, width: 2)),
Expanded(
child: InkWell(
onTap: () {},
child: Container(
alignment: Alignment.center,
child: Text("Edit Profile",
style: TextStyle(
color: Colors.white, fontSize: 17)),
)))
],
)),
)));
}
}
Output Screenshot:
Check my ButtonGroup widget that I created
import 'package:flutter/material.dart';
class ButtonGroup extends StatelessWidget {
static const double _radius = 10.0;
static const double _outerPadding = 2.0;
final int current;
final List<String> titles;
final ValueChanged<int> onTab;
final Color color;
final Color secondaryColor;
const ButtonGroup({
Key key,
this.titles,
this.onTab,
int current,
Color color,
Color secondaryColor,
}) : assert(titles != null),
current = current ?? 0,
color = color ?? Colors.blue,
secondaryColor = secondaryColor ?? Colors.white,
super(key: key);
#override
Widget build(BuildContext context) {
return Material(
color: color,
borderRadius: BorderRadius.circular(_radius),
child: Padding(
padding: const EdgeInsets.all(_outerPadding),
child: ClipRRect(
borderRadius: BorderRadius.circular(_radius - _outerPadding),
child: IntrinsicHeight(
child: Row(
mainAxisSize: MainAxisSize.min,
children: _buttonList(),
),
),
),
),
);
}
List<Widget> _buttonList() {
final buttons = <Widget>[];
for (int i = 0; i < titles.length; i++) {
buttons.add(_button(titles[i], i));
buttons.add(
VerticalDivider(
width: 1.0,
color: (i == current || i + 1 == current) ? color : secondaryColor,
thickness: 1.5,
indent: 5.0,
endIndent: 5.0,
),
);
}
buttons.removeLast();
return buttons;
}
Widget _button(String title, int index) {
if (index == this.current)
return _activeButton(title);
else
return _inActiveButton(title, index);
}
Widget _activeButton(String title) => FlatButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
disabledColor: secondaryColor,
disabledTextColor: color,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.zero,
),
child: Text(title),
onPressed: null,
);
Widget _inActiveButton(String title, int index) => FlatButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
color: Colors.transparent,
textColor: Colors.white,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.zero,
),
child: Text(title),
onPressed: () {
if (onTab != null) onTab(index);
},
);
}
You can use it like this
ButtonGroup(
titles: ["Button1", "Button2", "Button3"],
current: index,
color: Colors.blue,
secondaryColor: Colors.white,
onTab: (selected) {
setState(() {
index = selected;
});
},
)
Example:
import 'package:flutter/material.dart';
import 'package:flutter_app_test2/btn_grp.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MainPage(),
);
}
}
class MainPage extends StatefulWidget {
#override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int current = 0;
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ButtonGroup(
titles: ["Button1", "Button2", "Button3", "Button3"],
current: current,
onTab: (selected) {
print(selected);
setState(() {
current = selected;
});
},
),
),
);
}
}
try adding following in all RaisedButton widgets:
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
and buttonPadding: EdgeInsets.all(1), in ButtonBar
Source: https://api.flutter.dev/flutter/material/MaterialTapTargetSize-class.html