Flutter: How to scroll a container rather than its content? - flutter

Lets take a quick look at these two screenshots:
The left screenshot shows a box (decorated container) containing a CustomScrollView that handles scrolling. This one's pretty easy to accomplish and works well.
The right screenshot shows the intended behavior, where the box itself is scrolled, rather than its content. The problem is, that the box reaches out of the screens bounds, causing the overflow error message.
Do you have any ideas how this could be accomplished anyway?
Thank you.

You can wrap your Container in a SingleChildScrollView to get your desired result. Just put all the elements that should be inside the Container in a Column inside the Container.
import 'package:flutter/material.dart';
void 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(
appBar: AppBar(
title: Text('Container Scroll'),
),
body: SingleChildScrollView(
child: Center(
child: Container(
margin: EdgeInsets.symmetric(vertical: 30),
width: 300,
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.black54,
blurRadius: 10.0,
offset: Offset(0.0, 0.75))
],
),
child: Column(
children: <Widget>[
Container(
height: 200,
width: 250,
color: Colors.red,
),
Container(
height: 200,
width: 250,
color: Colors.green,
),
Container(
height: 200,
width: 250,
color: Colors.blue,
),
Container(
height: 200,
width: 250,
color: Colors.yellow,
),
Container(
height: 200,
width: 250,
color: Colors.orange,
),
Container(
height: 200,
width: 250,
color: Colors.pink,
),
Container(
height: 200,
width: 250,
color: Colors.purple,
),
Container(
height: 200,
width: 250,
color: Colors.teal,
),
Container(
height: 200,
width: 250,
color: Colors.black,
),
Container(
height: 200,
width: 250,
color: Colors.indigo,
),
],
),
),
),
)),
);
}
}

If you don't want to create your own Sliver there is a package that can help you with that
dependencies:
flutter:
sdk: flutter
flutter_group_sliver: ^0.0.2
...
Then using it inside CustomScrollView slivers
import 'package:flutter_group_sliver/flutter_group_sliver.dart';
SliverGroupBuilder(
margin: EdgeInsets.all(12.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
),
],
),
child: SliverList(...) // Your SliverList with ListTiles here
)

Related

How to create multiple circles using flutter

i am new to the flutter, need to create 12 circles around circle with proper angle, pls help to find proper answer with mathematically correct. pls refer the above diagram.
I have just faced the same issue...
Easy workaround:
Container(
width: 28,
height: 28,
decoration: BoxDecoration(
color: Colors.green.withOpacity(0.25), // border color
shape: BoxShape.circle,
),
child: Padding(
padding: EdgeInsets.all(2), // border width
child: Container( // or ClipRRect if you need to clip the content
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.blue, // inner circle color
),
child: Container(), // inner content
),
),
),
You Just Need To Run This Code:
import 'package:flutter/material.dart';
import 'circle_file.dart';
import 'dart:math' as math;
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',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const CircelFile(),
);
}
}
class CircelFile extends StatefulWidget {
const CircelFile({Key? key}) : super(key: key);
#override
State<CircelFile> createState() => _CircelFileState();
}
class _CircelFileState extends State<CircelFile> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Stack(
children: [
Container(
width:300,
height: 300,
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 30,
height:30,
decoration: BoxDecoration(
color: Colors.red, // border color
shape: BoxShape.circle,
),
),
Container(
height:1.0,
width:70,
color:Colors.black,),
Container(
width: 70,
height: 70,
decoration: BoxDecoration(
color: Colors.green, // border color
shape: BoxShape.circle,
),
),
Container(
height:1.0,
width:70,
color:Colors.black,),
Container(
width: 30,
height:30,
decoration: BoxDecoration(
color: Colors.red, // border color
shape: BoxShape.circle,
),
),
],
),
),
Positioned(
left: 136,
top: 18,
child: Column(
children: [
Container(
width: 30,
height:30,
decoration: BoxDecoration(
color: Colors.red, // border color
shape: BoxShape.circle,
),
),
Container(
height:70,
width:1,
color:Colors.black,),
],
),
),
Positioned(
left: 136,
bottom:15,
child: Column(
children: [
Container(
height:70,
width:1,
color:Colors.black,),
Container(
width: 30,
height:30,
decoration: BoxDecoration(
color: Colors.red, // border color
shape: BoxShape.circle,
),
),
],
),
),
Positioned(
left: 197,
bottom:36,
child: Transform.rotate(
angle: -math.pi /4,
child: Column(
children: [
Container(
height:70,
width:1,
color:Colors.black,),
Container(
width: 30,
height:30,
decoration: BoxDecoration(
color: Colors.red, // border color
shape: BoxShape.circle,
),
),
],
),
),
),
Positioned(
left:70,
top: 160,
child: RotationTransition(
turns: AlwaysStoppedAnimation(-20 / 23),
child: Column(
children: [
Container(
height:70,
width:1,
color:Colors.black,),
Container(
width: 30,
height:30,
decoration: BoxDecoration(
color: Colors.red, // border color
shape: BoxShape.circle,
),),
],
),
),
),
Positioned(
right: 65,
top: 55,
child: RotationTransition(
turns: AlwaysStoppedAnimation(-20 / 58),
child: Column(
children: [
Container(
height:70,
width:1,
color:Colors.black,),
Container(
width: 30,
height:30,
decoration: BoxDecoration(
color: Colors.red, // border color
shape: BoxShape.circle,
),),
],
),
),
),
Positioned(
left: 70,
top: 40,
child: RotationTransition(
turns: AlwaysStoppedAnimation(-20 / 32),
child: Column(
children: [
Container(
height:70,
width:1,
color:Colors.black,),
Container(
width: 30,
height:30,
decoration: BoxDecoration(
color: Colors.red, // border color
shape: BoxShape.circle,
),),
],
),
),
),[enter link description here][1]
],
),
),
);
}
}

remove border in background in flutter

I'm confused how to make the colored border cut off, when I meet the word "Month", I tried with BoxDecoration(color: Colors.transparent), but it doesn't produce anything, the border still doesn't get cut.
like the following :
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Container(
decoration: BoxDecoration(color: Colors.blue),
child: Center(
child: Container(
width: 120,
height: 110,
decoration: BoxDecoration(color: Colors.pink),
child: Stack(
children: [
Positioned(
bottom: 0,
child: Container(
height: 55,
width: 120,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.all(Radius.circular(10)),
border: Border.all(width: 2, color: Colors.white),
),
),
),
Positioned(
left: (120 - 50) / 2,
top: 45,
child: Container(
width: 50,
height: 15,
decoration:
BoxDecoration(color: Colors.black.withOpacity(0.1)),
child: Center(
child: Text("Month"),
),
),
),
],
),
),
),
)),
);}}
I want a look like this:
Please for the guidance, Thank you
A sample quick trick to have the effect you want is to set the background color for your positioned text as the same as your pink container:
child: Container(
width: 50,
height: 15,
padding: const EdgeInsets.all(5.0) <- with this too
decoration:
BoxDecoration(color: Colors.pink,), <- here
child: Center(
child: Text("Month"),
),
try this
Scaffold(
body: Stack(
children: <Widget>[
Container(
width: double.infinity,
height: 200,
margin: EdgeInsets.fromLTRB(20, 20, 20, 10),
padding: EdgeInsets.only(bottom: 10),
decoration: BoxDecoration(
color: Colors.green,
border: Border.all(width: 2, color: Colors.white),
borderRadius: BorderRadius.circular(15),
shape: BoxShape.rectangle,
),
),
Positioned(
left: 100,
top: 12,
child: Container(
padding: EdgeInsets.only(bottom: 10, left: 10, right: 10),
color: Colors.black.withOpacity(0.1),
child: Text(
'Month',
),
),
),
],
),
)

How do i scroll a container to hide inside another container?

please how do i scroll a container so it appears just like it does in the image below. i don't want the container to hide behind another container. i want the container to scroll into another container when you scroll it. i already know about using gridbox but I want to be able to work with one container and not multiple containers that the grid allows you to use. so basically I want to create one large container where the contents of my page will appear. then the container scrolls directly under the title without the title moving. I want the title of the page to appear where the green box with white border is. Thanks
You can use Stack, to achieve this goal. Below code has similar idea as your desired result
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
#override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatelessWidget(),
);
}
}
class MyStatelessWidget extends StatelessWidget {
const MyStatelessWidget({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Stack(children: [
Positioned(
top: 0,
left: 0,
right: 0,
child: Container(
color: Colors.green,
height: 150,
width: double.infinity,
child: Center(
child: Container(
height: 30,
width: 100,
color: Colors.green,
),
),
),
), Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: [
const SizedBox(height: 20),
Container(
height: 50,
width: 50,
color: Colors.blue,
),
const SizedBox(height: 20),
Container(
height: 50,
width: 50,
color: Colors.blue,
),
const SizedBox(height: 20),
Container(
height: 50,
width: 50,
color: Colors.blue,
),
const SizedBox(height: 20),
Container(
height: 50,
width: 50,
color: Colors.blue,
),
const SizedBox(height: 20),
Container(
height: 50,
width: 50,
color: Colors.blue,
),
const SizedBox(height: 20),
Container(
height: 50,
width: 50,
color: Colors.blue,
),
const SizedBox(height: 20),
Container(
height: 50,
width: 50,
color: Colors.blue,
),
const SizedBox(height: 20),
Container(
height: 50,
width: 50,
color: Colors.blue,
),
const SizedBox(height: 20),
Container(
height: 50,
width: 50,
color: Colors.blue,
),
const SizedBox(height: 20),
Container(
height: 50,
width: 50,
color: Colors.blue,
),
const SizedBox(height: 20),
Container(
height: 50,
width: 50,
color: Colors.red[300],
),
],
),
),
Positioned(
top: 0,
left: 0,
right: 0,
child: Container(
color: Colors.green,
height: 80,
width: double.infinity,
child: Center(
child: Container(
height: 30,
width: 100,
color: Colors.black,
),
),
),
),
]);
}
}

How to stop ColorFiltered blending?

I followed the answer of this question to create a widget with a transparent hole. My problem now is that any other widgets I place on the screen have a blend effect as well. I would
like this to not be the case, if possible. Here's my code:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
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
Widget build(BuildContext context) {
return Material(
child: Stack(
fit: StackFit.expand,
children: [
Image.network(
'https://wallpaperplay.com/walls/full/8/3/a/35405.jpg',
fit: BoxFit.cover,
),
ColorFiltered(
colorFilter: ColorFilter.mode(Colors.black.withOpacity(0.8),
BlendMode.srcOut), // This one will create the magic
child: Column(
children: [
Expanded(
child: Stack(
fit: StackFit.expand,
children: [
Container(
decoration: BoxDecoration(
color: Colors.black,
backgroundBlendMode: BlendMode
.dstOut), // This one will handle background + difference out
),
Align(
alignment: Alignment.topCenter,
child: Container(
margin: const EdgeInsets.only(top: 80),
height: 200,
width: 200,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(10),
),
),
),
],
),
),
Text("Test",
style: TextStyle(
color: Colors.red,
fontSize: 64,
fontWeight: FontWeight.bold)),
],
),
),
Align(
alignment: Alignment.topCenter,
child: Container(
margin: const EdgeInsets.only(top: 80),
height: 200,
width: 200,
decoration: BoxDecoration(
//color: Colors.red,
borderRadius: BorderRadius.circular(10),
border: Border.all(
color: Colors.red,
width: 2,
),
),
),
),
],
),
);
}
}
And the result:
I want the text widget "Test" to be in red and not show the image below. Any widget placed above the color filter should not be blended with the background. I've tried wrapping the text in a ColorFiltered widget and setting the BlendMode to "src" which should discard the destination image (if I understand the documentation correctly), but that did not work.
since you are using a stack, just pull out the text wsidget from the ColoredFilter widget and place it after the widget. This way it will appear on top.
something like this (not tested):
Stack(
fit: StackFit.expand,
children: [
Image.network(
'https://wallpaperplay.com/walls/full/8/3/a/35405.jpg',
fit: BoxFit.cover,
),
ColorFiltered(
colorFilter: ColorFilter.mode(Colors.black.withOpacity(0.8),
BlendMode.srcOut), // This one will create the magic
child: Column(
children: [
Expanded(
child: Stack(
fit: StackFit.expand,
children: [
Container(
decoration: BoxDecoration(
color: Colors.black,
backgroundBlendMode: BlendMode
.dstOut), // This one will handle background + difference out
),
Align(
alignment: Alignment.topCenter,
child: Container(
margin: const EdgeInsets.only(top: 80),
height: 200,
width: 200,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(10),
),
),
),
],
),
),
],
),
),
// I just moved text widget here, you may need to apply any Position, align, padding, margin you wish, since it is now outside of the Expand widget
Text("Test",
style: TextStyle(
color: Colors.red,
fontSize: 64,
fontWeight: FontWeight.bold)),
Align(
alignment: Alignment.topCenter,
child: Container(
margin: const EdgeInsets.only(top: 80),
height: 200,
width: 200,
decoration: BoxDecoration(
//color: Colors.red,
borderRadius: BorderRadius.circular(10),
border: Border.all(
color: Colors.red,
width: 2,
),
),
),
),
],
),

Create curved card with shadow

I really need you because I don't have even start idea how to implement these, and also I am not sure how it is called.
Actually, I want to implement something similar like on image (this little circle in each of cards - that is like chain between two cards).
With help of key I have made image from up with this code:
class InfoPage extends StatefulWidget {
InfoPage();
#override
_InfoPageState createState() => _InfoPageState();
}
class _InfoPageState extends State<InfoPage> {
InfoItemModel infoData = dataSourceInfoUser;
double basicSize = 70;
#override
initState() {
super.initState();
}
#override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text('Example'),
),
body: Container(
color: Colors.white,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
width: double.infinity,
height: 150,
child: AppCardField(
child: Text('Something'),
),
),
_buildCardWithCircle(
bgCircleX: 0.78,
bgCirceY: -2.0,
innerContainerX: 0.756,
innerContainerY: -1.78,
colorInner: Colors.orange
),
],
),
),
),
);
_buildCardWithCircle({double bgCircleX, double bgCirceY, double innerContainerX, double innerContainerY, Color colorInner}) => Container(
width: double.infinity,
height: 150,
child: Stack(
children: <Widget>[
Container(
width: double.infinity,
height: 150,
child: AppCardField(
child: Text('Something'),
),
),
Align(
alignment: Alignment(bgCircleX, bgCirceY),
child: Container(
height: basicSize,
width: basicSize,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(50.0),
),
),
),
Align(
alignment: Alignment(innerContainerX, innerContainerY),
child: Container(
height: basicSize - 10,
width: basicSize - 10,
decoration: BoxDecoration(
color: colorInner,
borderRadius: BorderRadius.circular(50.0),
),
child: Icon(Icons.vertical_align_center),
),
),
],
),
);
}
class AppCardField extends StatelessWidget {
final Widget child;
final double height;
final double paddingVertical, paddingHorizontal;
final double paddingVerticalChild, paddingHorizontalChild;
AppCardField({
this.child,
this.height,
this.paddingVertical = 8,
this.paddingHorizontal = 16,
this.paddingVerticalChild = 8,
this.paddingHorizontalChild = 16,
Key key})
: super(key: key);
#override
Widget build(BuildContext context) => Padding(
padding: EdgeInsets.symmetric(
vertical: paddingVertical, horizontal: paddingHorizontal),
child: Container(
height: height,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.all(Radius.circular(8)),
boxShadow: [
BoxShadow(
color: Colors.red,
blurRadius: 15.0,
offset: Offset(0.0, 5.0),
),
],
),
child: Padding(
padding: EdgeInsets.symmetric(
vertical: paddingVerticalChild,
horizontal: paddingHorizontalChild),
child: child,
),
));
}
But here, I have problem with shadow of the card and strongly white background of circle, OFC I need this shadow to be also in this white space, question is how to solve this?
Something like this
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Some Stuff"),
),
body: Column(
children: <Widget>[
Container(
width: double.infinity,
height: 150.0,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.red,
blurRadius: 20.0
)
]
),
child: Card(
elevation: 20.0,
color: Colors.blueGrey,
child: Text('Something'),
),
),
SizedBox(height: 10.0,),
Container(
width: double.infinity,
height: 150.0,
child: Stack(
children: <Widget>[
Container(
width: double.infinity,
height: 150.0,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.red,
blurRadius: 20.0
)
]
),
child: Card(
elevation: 20.0,
color: Colors.blueGrey,
child: Text('Something'),
),
),
Align(
alignment: Alignment(0.9,-1.5),
child: Container(
height: 50.0,
width: 50.0,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(25.0),
),
),
),
Align(
alignment: Alignment(0.88,-1.35),
child: Container(
height: 40.0,
width: 40.0,
decoration: BoxDecoration(
color: Colors.orange,
borderRadius: BorderRadius.circular(25.0),
),
),
),
Align(
alignment: Alignment(0.85, -1.2),
child: Icon(Icons.access_alarms, color: Colors.white,)
),
],
),
),
],
),
),
);
then you just reaped the card for the other once with different alignment values.