This is my code where I have increased my height of this container to make big circle in mobile emulator but couldn't make it. I don't know the reason behind these.
import 'package:flutter/material.dart';
void main () {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({ Key? key }) : super(key: key);
#override
Widget build(BuildContext context) {
return const MaterialApp(
home: NewScreen(),
debugShowCheckedModeBanner: false,
);
}
}
class NewScreen extends StatelessWidget {
const NewScreen({ Key? key }) : super(key: key);
#override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
return Scaffold(
body: Stack(
children: [
Positioned(
top: -150.0,
left: 0.0,
bottom: 450.0,
right: 0.0,
child: Container(
height: 800.0,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
),
),
],
),
);
}
}
I cannot make much bigger than initial size. What can I do to finish this?
I think you try to achieve this result
It's simple, give the left and right properties negative values as you want to achieve the result that you want
Positioned(
left: -100.0,
right: -100.0,
Position wrapping Container is constraining the Container's size.
Current Position widget is forcing child widget's size to have Stack's height-600 and same width with Stack.
Since you didn't include the final result of your expected output, i have add few version of circle that might suit your needs. Few this to take note here:
a) when giving height: size.height the container widget will take the space of the whole screen. The reason you see that the circle did not fit to the height because it is bounded by the width. This can be overcome with the code from example 2
b) height: size,height/2 means that the container will take half the size of the screen.
Big red circle in the centre of the screen
import 'package:flutter/material.dart';
void main () {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({ Key? key }) : super(key: key);
#override
Widget build(BuildContext context) {
return const MaterialApp(
home: NewScreen(),
debugShowCheckedModeBanner: false,
);
}
}
class NewScreen extends StatelessWidget {
const NewScreen({ Key? key }) : super(key: key);
#override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Scaffold(
body: Container(
height: size.height,
width: size.width,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
),
);
}
}
Circle with hidden side
import 'package:flutter/material.dart';
void main () {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({ Key? key }) : super(key: key);
#override
Widget build(BuildContext context) {
return const MaterialApp(
home: NewScreen(),
debugShowCheckedModeBanner: false,
);
}
}
class NewScreen extends StatelessWidget {
const NewScreen({ Key? key }) : super(key: key);
#override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Scaffold(
body: Stack(
children: [
Positioned(
left: -100,
right: -100,
child: Container(
height: size.height,
width: size.width,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
),
),
],
),
);
}
}
Circle at the top of the screen
import 'package:flutter/material.dart';
void main () {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({ Key? key }) : super(key: key);
#override
Widget build(BuildContext context) {
return const MaterialApp(
home: NewScreen(),
debugShowCheckedModeBanner: false,
);
}
}
class NewScreen extends StatelessWidget {
const NewScreen({ Key? key }) : super(key: key);
#override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Scaffold(
body: Column(
children: [
Container(
height: size.height/2,
width: size.width,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
),
],
),
);
}
}
Related
I wanted to create diagonal buttons in a Row() similar to this as shown in this image. First and last buttons has vertical ends at the end, other edges will be diagonal.
These buttons can contain "text" or "icon". It would be ideal if we can make the Elevated button to this style, so that we can use other options in the elevated buttons as well.
https://ibb.co/M7sV6zW
is this possible with Flutter ?
Yes it is possible. You can use ClipPath with CustomClipper to achieve "any" appearance.
https://api.flutter.dev/flutter/widgets/ClipPath-class.html
void main() {
runApp(const CounterApp());
}
class CounterApp extends StatelessWidget {
const CounterApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const DiagnalButtonListView(),
);
}
}
class DiagnalButtonListView extends StatelessWidget {
const DiagnalButtonListView({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("diagnal buttons"),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView.builder(
itemCount: 5,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) => Button(
title: "$index.Button",
),
),
),
);
}
}
class Button extends StatefulWidget {
final String title;
const Button({super.key, required this.title});
#override
State<Button> createState() => _ButtonState();
}
class _ButtonState extends State<Button> {
#override
Widget build(BuildContext context) {
return Container(
height: 72,
width: 72,
child: Stack(children: [
Container(color: Colors.white),
ClipPath(
child: Container(
width: 72,
color: Colors.yellow,
child: Center(child: Text(widget.title)),
height: 32,
),
clipper: CustomClipPath(),
)
]));
}
}
class CustomClipPath extends CustomClipper<Path> {
#override
Path getClip(Size size) {
Path path = Path();
path.lineTo(0, 60);
path.lineTo(60, 60);
path.lineTo(72, 0);
path.lineTo(12, 0);
path.lineTo(0, 60);
return path;
}
#override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
I'm trying to create a curved container (semi circle) on whole screen using flutter
image for the curved
I tried to use some solutions found on stackoverflow but it didn't work.
can anyone help
thanks
Wrap your widget with stack and then provide clip oval to the container and positioned your container to the left with half of your container width.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: 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 Demo(),
);
}
}
class Demo extends StatelessWidget {
const Demo({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Positioned(
top: -10,
bottom: -10,
left: -200,
child: ClipOval(
child: Container(
height: double.maxFinite,
width: 400,
color: Colors.black.withOpacity(0.5),
),
),
),
],
),
);
}
}
You will get the following output
I have SVG as an image file where I want to do this cursor hover effect.
I tried onhoverbutton but that does not work here.
I am using flutter_svg package for showing SVG image
Container(
width: double.infinity,
height: 400,
child: SvgPicture.asset('assets/below.svg',
cacheColorFilter: true,
color: const Color(0xff2C66B8)),
)
This is what I want in my web-app
if you run below code in dartpad, you will get idea about how hover works in flutter.
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MouseRegion with custom cursor',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Center(
child: MouseRegionWithCustomCursor(
cursor: const Icon(Icons.refresh, color: Colors.white),
// here you have to replace this with your svg image
child: Container(width: 300, height: 200, color: Colors.blueGrey),
),
);
}
}
// and in this class you can implement your blue line expansion and compression lines
class MouseRegionWithCustomCursor extends HookWidget {
final Widget cursor;
final Widget child;
const MouseRegionWithCustomCursor({
Key? key,
required this.cursor,
required this.child,
}) : super(key: key);
#override
Widget build(BuildContext context) {
final cursorPosition = useState<Offset?>(null);
return MouseRegion(
cursor: SystemMouseCursors.none,
onHover: (event) => cursorPosition.value = event.localPosition,
onExit: (event) => cursorPosition.value = null,
child: Stack(
children: [
child,
if (cursorPosition.value != null)
AnimatedPositioned(
duration: const Duration(),
left: cursorPosition.value?.dx,
top: cursorPosition.value?.dy,
child: cursor,
),
],
),
);
}
}
i know this is not best explanation but you can start with this.
I would like to have a persistent container occupy the space about my material Scaffolds AppBar. I would like the Scaffold to resize to take up the available space.
When I try to do this, my Scaffold continues to be the height of the entire screen, and it is simply pushed lower, with a portion overflowing off the screen.
Is there a way I can have the Scaffold to resize to the available space?
Here is what I have coded so far...
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return PersistenTopBar(
child: Scaffold(
appBar: AppBar(
title: const Text("Test App"),
),
body: Container(),
),
);
}
}
class PersistenTopBar extends StatelessWidget {
final Widget child;
const PersistenTopBar({Key? key , required this.child }) : super(key: key);
#override
Widget build(BuildContext context) {
var mediaQuery = MediaQuery.of(context);
return Column(
children: [
Container(
width: double.infinity,
height: 200,
color: Colors.red,
),
SizedBox(
width: mediaQuery.size.width,
height: mediaQuery.size.height,
child: child,
),
],
);
}
}
You could also create a CustomAppBar that would take as children a topChild and an appBar.
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
final double height;
final Widget topChild;
final AppBar appBar;
const CustomAppBar(
{Key? key,
this.height = 200.0,
required this.topChild,
required this.appBar})
: super(key: key);
#override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(child: topChild),
appBar,
],
);
}
#override
Size get preferredSize => Size.fromHeight(height);
}
Full code sample
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light(),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: CustomAppBar(
topChild: Container(color: Colors.red),
appBar: AppBar(title: const Text('My awesome Test App')),
),
body: const Center(
child: Text(
"Test App",
style: TextStyle(fontSize: 32.0),
),
),
);
}
}
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
final double height;
final Widget topChild;
final AppBar appBar;
const CustomAppBar(
{Key? key,
this.height = 200.0,
required this.topChild,
required this.appBar})
: super(key: key);
#override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(child: topChild),
appBar,
],
);
}
#override
Size get preferredSize => Size.fromHeight(height);
}
the available space = mediaQuery.size.height - the Height of the Container above the appBar so the SizedBox under the appBar wil be :
SizedBox(
width: mediaQuery.size.width,
height: mediaQuery.size.height - 200,
child: child,
),
the result:
or you can wrap your SizedBox with Expanded Widget :
Expanded(
child: SizedBox(
width: mediaQuery.size.width,
child: child,
),
),
the same result :
I am trying to pass some color info from one widget to another but I am not able to get that color in destination widget. I wanted to build a single class with some UI code in it and just call this class in my main widget so that I don't have to repeat the code all over again, but I am not able to pass the data,
Here is the code that I am working on: What I am doing wrong?
void main(List<String> args) => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const Text('Stateless/Clean Code'),
),
body: const StatelessOne(),
),
);
}
}
class StatelessOne extends StatelessWidget {
const StatelessOne({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Text(
'Widget color can be customised without \nhaving to retype the entire code'),
StatelessTwo(key: key, param: Colors.green),
StatelessTwo(key: key, param: Colors.pink),
StatelessTwo(key: key, param: Colors.blue),
StatelessTwo(key: key, param: Colors.orange),
],
),
);
}
}
class StatelessTwo extends StatelessWidget {
StatelessTwo({Key? key, #required param}) : super(key: key);
final Map param = {};
#override
Widget build(BuildContext context) {
return Container(
height: 120,
width: 250,
color: param['color'],
child: Center(child: Text('Your Repetitive Widget $key')),
);
}
}
Simple way will be
class StatelessTwo extends StatelessWidget {
final Color color;
const StatelessTwo({
Key? key,
required this.color,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
height: 120,
width: 250,
color: color,
child: Center(
child: Text('Your Repetitive Widget ${key ?? "no key found"}')),
);
}
}
-----
color: color, //use
Pass color
StatelessTwo(key: key, color: Colors.green), and seems you are passing same key, avoid it is not necessary for Ui-logic. Most likely, you don't need to pass key, if you still want to pass use UinqueKey()