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

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,
),
),
),
),
]);
}
}

Related

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 to center the Text in the middle of the Container? Flutter

i want to center my texts in the middle of my containers but they only center them in top middle of the containers.
Here is my code:
import 'package:flutter/material.dart';
class Homepage extends StatefulWidget {
const Homepage({Key? key}) : super(key: key);
#override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Homepage'),
backgroundColor: Colors.green,
),
body: SafeArea(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(75, 100, 75, 100),
height: 100,
width: 300,
color: Colors.green,
child: const Text('BLE & Data'),
),
//const SizedBox(height: 10),
Container(
margin: EdgeInsets.fromLTRB(75, 25, 75, 100),
height: 100,
width: 300,
color: Colors.green,
child: const Text('Statistiken',
textAlign: TextAlign.center,),
),
const SizedBox(height: 10),
Container(
//padding: EdgeInsets.all(50),
margin: EdgeInsets.fromLTRB(75, 25, 75, 100),
height: 100,
width: 300,
color: Colors.green,
child: const Text('Personal Data',
textAlign: TextAlign.center,
),
),
],
),
));
}
}
And that´s my output: Demo
I would be very thankful if anyone can solve my problem
You only need to wrap your text widget with the center widget.
Center(child:Text("")),
Add This Code:-
class Homepage extends StatefulWidget {
const Homepage({Key? key}) : super(key: key);
#override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Homepage'),
backgroundColor: Colors.green,
),
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(75, 100, 75, 100),
height: 100,
width: 300,
color: Colors.green,
child: const Center(child: Text('BLE & Data')),
),
//const SizedBox(height: 10),
Container(
margin: EdgeInsets.fromLTRB(75, 25, 75, 100),
height: 100,
width: 300,
color: Colors.green,
child: const Center(
child: Text(
'Statistiken',
textAlign: TextAlign.center,
),
),
),
const SizedBox(height: 10),
Container(
//padding: EdgeInsets.all(50),
margin: EdgeInsets.fromLTRB(75, 25, 75, 100),
height: 100,
width: 300,
color: Colors.green,
child: const Center(
child: Text(
'Personal Data',
textAlign: TextAlign.center,
),
),
),
],
),
),
));
}
}
Wrap whatever you want centered in a Center widget:
For example:
Container(
margin: EdgeInsets.fromLTRB(75, 100, 75, 100),
height: 100,
width: 300,
color: Colors.green,
child: Center( child: const Text('BLE & Data') ),
),
Use alignment : Alignment.center of container() widget.
class Homepage extends StatefulWidget {
const Homepage({Key key}) : super(key: key);
#override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Homepage'),
backgroundColor: Colors.green,
),
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
alignment: Alignment.center,
margin: EdgeInsets.fromLTRB(75, 100, 75, 100),
height: 100,
width: 300,
color: Colors.green,
child: const Text('BLE & Data'),
),
//const SizedBox(height: 10),
Container(
alignment: Alignment.center,
margin: EdgeInsets.fromLTRB(75, 25, 75, 100),
height: 100,
width: 300,
color: Colors.green,
child: const Text(
'Statistiken',
textAlign: TextAlign.center,
),
),
const SizedBox(height: 10),
Container(
alignment: Alignment.center,
//padding: EdgeInsets.all(50),
margin: EdgeInsets.fromLTRB(75, 25, 75, 100),
height: 100,
width: 300,
color: Colors.green,
child: const Text(
'Personal Data',
textAlign: TextAlign.center,
),
),
],
),
),
));
}
}

How to make a container outside of parent container

What my try looked like:
What I want to do:
What I have tried is Stack widget. But the half of container became invisible when container moves outside.
Stack(
children: [
Align(
alignment: new Alignment(0.0, 0.0),
child: Container(
height: 150,
width: double.infinity,
decoration: BoxDecoration(color: Colors.red),
),
),
Positioned(
top: -15,
left: 140,
child: Container(
height: 50,
width: 100,
decoration: BoxDecoration(color: Colors.black),
)),
],
)
Any info would be welcome.
Answer:
Just adding Padding to main container
try use Transform
return Container(
width: 400,
height: 200,
child: Stack(
children: <Widget>[
Align(alignment: Alignment.topCenter,
child: Transform.translate(
child: Container(
width: 100,
height: 50,
color: Colors.green,
child: Text('some text'),
),
offset: Offset(0, -20),
),
),
],
),
color: Colors.red,
);
Use Stack along with Padding on one container to make it lower.
return Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Padding(
padding: EdgeInsets.only(top: 10),
child: Container(
width: 400,
height: 100,
color: Colors.white
),
),
),
Align(
alignment: Alignment.topCenter,
child: Container(
width: 70,
height: 30,
color: Colors.red
),
),
]
);
Full code to see the result exactly as you want using dartpad.dev/flutter paste 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: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Builder(
builder: (context){
return Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Padding(
padding: EdgeInsets.only(top: 10),
child: Container(
width: 400,
height: 100,
color: Colors.white
),
),
),
Align(
alignment: Alignment.topCenter,
child: Container(
width: 70,
height: 30,
color: Colors.red
),
),
]
);
}
);
}
}
You were correct in using a Stack widget. The code would look like the following:
Center(
child: Stack(
alignment: Alignment.topCenter,
children: [
Padding(
padding: const EdgeInsets.only(top: 25,),
child: Container(
color: Colors.green,
height: 200,
width: 200
),
),
Container(
color: Colors.red,
height: 50,
width: 100
),
]
),
),
By adding top padding to the green container you are pushing it down making room for the red container to sit on top.
The result looks like this:

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

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
)

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.