flutter dot indicator to pageview - flutter

I want to add a dot indicator to pageview. When a person scrolls the image to the right to left image changes on my code. NO problem with there but I couldn't manage to add a dot indicator to my code. How can I put a dot indicator when I scroll across images also the dots switch to the order of images.........................................................................................................
PageView.builder(
itemCount: totalCards.toInt(),
itemBuilder: (context, i) {
return Container(
margin: EdgeInsets.only(top: 10),
child: Container(
child: Column(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: PageView(
children: <Widget>[
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(4.0),
image: DecorationImage(
image: AssetImage(
photos[startphoto]),
fit: BoxFit.cover)),
),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(4.0),
image: DecorationImage(
image: AssetImage(
photos[startphoto1]),
fit: BoxFit.cover)),
),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(4.0),
image: DecorationImage(
image: AssetImage(
photos[startphoto2]),
fit: BoxFit.fitHeight)),
),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(4.0),
image: DecorationImage(
image: AssetImage(
photos[startphoto3]),
fit: BoxFit.cover)),
),
],
),
),
],
),
)
);
},
),
),

You can do that with this package: https://pub.dev/packages/page_view_indicators
In the example you can see how to implement it.
EDIT: Implement your dot indicator like this:
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final ValueNotifier<int> _pageNotifier = new ValueNotifier<int>(0);
final PageController _pageController = new PageController();
#override
Widget build(BuildContext context) {
return Column(
children: [
PageView.builder(
itemCount: totalCards.toInt(),
controller: _pageController,
itemBuilder: (context, i) {
...
},
onPageChanged:(index) {
setState(() {
_pageNotifier.value = index;
});
},
),
Center(
child: CirclePageIndicator(
currentPageNotifier: _pageNotifier,
itemCount: totalCards.toInt(),
),
)
],
);
}
}

Related

having problem with Draggble widget in flutter

In BottomShield, there are emojis that users can drag to the parent widget, which is a GridView. As you can see in the code, I placed the grid view in a stack where there is a transparent emoji. When the user moves the emoji in BottomShield to the parent widget, I expect it to substitute this transparent emoji, but it doesn't. I would really appreciate some help with this. Thank you
class GridViewCustom extends StatefulWidget {
final String imgUrl;
final int itemCount;
const GridViewCustom({
required this.imgUrl,
required this.itemCount,
super.key,
});
#override
State<GridViewCustom> createState() => _GridViewCustomState();
}
class _GridViewCustomState extends State<GridViewCustom> {
String overlayImg = 'assets/images/poo2.png';
#override
Widget build(BuildContext context) {
return GridView.custom(
gridDelegate: SliverQuiltedGridDelegate(
crossAxisCount: 2,
crossAxisSpacing: 4,
mainAxisSpacing: 10,
repeatPattern: QuiltedGridRepeatPattern.inverted,
pattern: [
QuiltedGridTile(2, 2),
QuiltedGridTile(1, 1),
QuiltedGridTile(1, 1),
QuiltedGridTile(1, 2),
],
),
childrenDelegate: SliverChildBuilderDelegate(
(context, index) {
return GestureDetector(
onTap: () {
dialog(context);
},
child: Stack(
alignment: Alignment.center,
children: [
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(widget.imgUrl),
fit: BoxFit.fill,
),
),
),
DragTarget<String>(
onAccept: (data) => setState(() {
overlayImg = data;
}),
builder: (context, candidateData, rejectedData) =>
Container(
height: 80,
width: 90,
decoration: BoxDecoration(
color: Colors.transparent,
image: DecorationImage(
opacity: 0,
image: AssetImage(
overlayImg,
),
fit: BoxFit.fill,
),
),
),
),
],
),
);
},
));
}
Future<dynamic> dialog(BuildContext context) {
return showModalBottomSheet(
elevation: 0,
backgroundColor: Colors.black,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(15),
),
),
context: context,
builder: (BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Emoji(urlPath: 'assets/images/poo1.png'),
Emoji(urlPath: 'assets/images/poo2.png'),
Emoji(urlPath: 'assets/images/poo3.png'),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Emoji(urlPath: 'assets/images/poo4.png'),
Emoji(urlPath: 'assets/images/poo5.png'),
Emoji(urlPath: 'assets/images/poo6.png'),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Emoji(urlPath: 'assets/images/poo7.png'),
Emoji(urlPath: 'assets/images/poo8.png'),
Emoji(urlPath: 'assets/images/poo4.png'),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Emoji(urlPath: 'assets/images/poo3.png'),
Emoji(urlPath: 'assets/images/poo6.png'),
Emoji(urlPath: 'assets/images/poo2.png'),
],
),
],
);
});
}
}
class Emoji extends StatefulWidget {
final String urlPath;
Emoji({required this.urlPath, super.key});
#override
State<Emoji> createState() => _EmojiState();
}
class _EmojiState extends State<Emoji> {
#override
Widget build(BuildContext context) {
return Draggable(
data: widget.urlPath,
feedback: Container(
height: 70,
width: 70,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(widget.urlPath),
fit: BoxFit.fill,
),
),
),
child: Container(
height: 70,
width: 70,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(widget.urlPath),
fit: BoxFit.fill,
),
),
),
);
}
}

Containers are not appear, how can work arrange these in flutter?

I'm flutter begginner. I tried to make a listview and make some containers and text widgets. but there's only text widget and don't appear containers that have images. Could you review where's some problems in code below.
class DonatePage extends StatelessWidget {
const DonatePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body:
ListView(
scrollDirection: Axis.vertical,
reverse: true,
addAutomaticKeepAlives: false,
children: [
Column(
children: [
Container(
width: double.infinity,
height: 400,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/components/shop/donate/card2.png")
)
),
),
Text("Campaign of the week"),
Container(
width: double.infinity,
height: 400,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/components/shop/donate/card1.png")
)
),
),
Text("for good"),
Container(
width: double.infinity,
height: 400,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/components/shop/donate/card3.png")
)
),
),
]
)
],
),
);
}
}
Problems Screenshots below.
Please add folder path like this:
assets/images/components/shop/donate/

How to connect a menu item to a container through scroll property in flutter i.e. an anchor in html

Hello everyone I am new to flutter and I am facing a difficulty in my website development. I have three different classes:
Main menu (which is not in my Navbar but a row in my body widget)
Menu Items ( dropdown item of a menu item)
Carousel slider (item in my body widget)
I want to connect a sub menu item to my carousel slider container using an 'OnTap' function in the sub menu class. It seems the only solution is to combine the widgets but I don't want to since they are not supposed to be close but just linked via a click. In few words I am in search of codes where we have a click on a menu sub-item and the screen context scrolls down to the specific item on the canvas without changing page.
Here a code example:
//my main class
import 'package:flutter/material.dart';
import 'package:navigation_scroll/widgets/view/homePage.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: MyHomePage(),
);
}
}
//my homePage class
import 'package:flutter/material.dart';
import 'package:navigation_scroll/widgets/carousels/carouselOne.dart';
import 'package:navigation_scroll/widgets/carousels/carouselTwo.dart';
import 'package:navigation_scroll/widgets/menu/dropDownMenu.dart';
class MyHomePage extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return MyHomePageState();
}
}
//where you have the combination of all the widgets
class MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Center(child: Text("ListView")),
),
body: Container(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
height: 30.0,
),
Container(
child:ListViewDemo(),
),
SizedBox(
height: 50.0,
),
Container(
child: CarouselOne()),
SizedBox(
height: 50.0,
),
Container(
child: CarouselTwo()),
],
),
)
);
}
}
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
// this is the first carousel that appears on screen
class CarouselOne extends StatelessWidget {
#override
Widget build(BuildContext context) {
return CarouselSlider(
items: [
//1st Image of Slider
Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage("https://cdn.eso.org/images/thumb300y/eso1907a.jpg"),
fit: BoxFit.cover,
),
),
),
//2nd Image of Slider
Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage("https://cdn.eso.org/images/thumb300y/eso1907a.jpg"),
fit: BoxFit.cover,
),
),
),
//3rd Image of Slider
Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage("https://cdn.eso.org/images/thumb300y/eso1907a.jpg"),
fit: BoxFit.cover,
),
),
),
//4th Image of Slider
Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage("https://cdn.eso.org/images/thumb300y/eso1907a.jpg"),
fit: BoxFit.cover,
),
),
),
//5th Image of Slider
Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage("https://cdn.eso.org/images/thumb300y/eso1907a.jpg"),
fit: BoxFit.cover,
),
),
),
],
//Slider Container properties
options: CarouselOptions(
height: 180.0,
enlargeCenterPage: true,
autoPlay: true,
aspectRatio: 16 / 9,
autoPlayCurve: Curves.fastOutSlowIn,
enableInfiniteScroll: true,
autoPlayAnimationDuration: Duration(milliseconds: 800),
viewportFraction: 0.8,
),
);
}
}
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
// this is the second carousel that appears on screen
class CarouselTwo extends StatelessWidget {
#override
Widget build(BuildContext context) {
return CarouselSlider(
items: [
//1st Image of Slider
Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage("https://cdn.eso.org/images/thumb300y/eso1907a.jpg"),
fit: BoxFit.cover,
),
),
),
//2nd Image of Slider
Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage("https://cdn.eso.org/images/thumb300y/eso1907a.jpg"),
fit: BoxFit.cover,
),
),
),
//3rd Image of Slider
Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage("https://cdn.eso.org/images/thumb300y/eso1907a.jpg"),
fit: BoxFit.cover,
),
),
),
//4th Image of Slider
Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage("https://cdn.eso.org/images/thumb300y/eso1907a.jpg"),
fit: BoxFit.cover,
),
),
),
//5th Image of Slider
Container(
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: NetworkImage("https://cdn.eso.org/images/thumb300y/eso1907a.jpg"),
fit: BoxFit.cover,
),
),
),
],
//Slider Container properties
options: CarouselOptions(
height: 180.0,
enlargeCenterPage: true,
autoPlay: true,
aspectRatio: 16 / 9,
autoPlayCurve: Curves.fastOutSlowIn,
enableInfiniteScroll: true,
autoPlayAnimationDuration: Duration(milliseconds: 800),
viewportFraction: 0.8,
),
);
}
}
import 'package:flutter/material.dart';
// this is the dropdown menu from which we select and want to arrive at the carousel slider
class ListViewDemo extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return ListViewDemoState();
}
}
class ListViewDemoState extends State<ListViewDemo> {
List<String> selectedItemValue = List<String>();
#override
void initState() {
// TODO: implement initState
super.initState();
}
#override
Widget build(BuildContext context) {
return Container(
height: 50.0,
child: ListView (
children: [
Center(
child: DropdownButton(
//value:'ABOUT',
onChanged: (value) { },
items: [
DropdownMenuItem(
child: GestureDetector(
child: Text(
'VISION'
),
onTap: (){}, // we need to write a function here that does what we want to a container in the carousel slider
),
),
DropdownMenuItem(
child: Text(
'MISSION'
),
onTap: (){}, // we need to write a function here that does what we want to a container in the carousel slider
),
DropdownMenuItem(
child: Text(
'TEAM'
),
onTap: (){}, // we need to write a function here that does what we want to a container in the carousel slider
),
],
),
),
],
)
);
}
}
I think you can achieve this with CarouselSlider controller you can trigger it onTap and scroll it to your desired destination
This can be achieved using CarouselController attached with your CarouselSlider.
On Button/Menu click, you just need to call
carouselController.jumpToPage(whateverYourTargetedPageNoInInt);

Flutter set background image for a page

I want to set the image as the background color for the page but the code below seems not to work well. I'm not sure if there is something missing in my code or mistake inside. And there is no error message shown.
class LoginPage extends StatefulWidget {
#override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.tealAccent,
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/background.png"),
fit: BoxFit.cover,
),
),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(height: 50),
_signInButton(),
],
),
),
),
);
}
you have to just use stack widget just like this
Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/login.png"), fit: BoxFit.fill)),
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: <YOUR CHILD COMPONENTS WILL BE PLACED HERE>
),
],
),
Hope it will help!

Flutter Position Grid Elements

I need to push or rather position some elements inside my Widget, the main idea is to place them at the bottom of the view
The idea is to position the elements of the blue box in the other blue box
class Principal extends StatelessWidget {
#override
Widget build(BuildContext context) {
final screenWidth = MediaQuery.of(context).size.width;
final screenHeight = MediaQuery.of(context).size.height;
final List<String> _listItem = [
'assets/foto.jpg',
'assets/foto2.jpg',
'assets/foto3.png',
'assets/foto4.jpg',
];
return Stack(
children: <Widget>[
Scaffold(
body: SafeArea(
child: Stack(
children: <Widget>[
_containerImg(context,screenWidth,screenHeight),
_table(_listItem),
],
),
),
),
],
);
}
Widget _containerImg(BuildContext context,ScreenWidth,ScreenHeight){
return Container(
height: ScreenHeight,
width: ScreenWidth,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/480359.jpg'),
fit: BoxFit.cover,
),
),
);
}
Widget _table(List<String> _listItem){
return GridView.count(crossAxisCount: 4,
padding: EdgeInsets.all(5.0),
crossAxisSpacing: 1.0,
mainAxisSpacing: 1.0,
children: _listItem.map((item) => Card(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50.0),
image: DecorationImage(
image: AssetImage(item),
fit: BoxFit.cover,
),
),
),
),
).toList()
);
}
}
I leave samples of the code, I tried to place a SizedBox, and an expanded one in the middle of the Widget, but it didn't work
class Principal extends StatelessWidget {
#override
Widget build(BuildContext context) {
final screenWidth = MediaQuery.of(context).size.width;
final screenHeight = MediaQuery.of(context).size.height;
final List<String> _listItem = [
'https://images.unsplash.com/photo-1535498730771-e735b998cd64?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80',
'https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500',
'https://images.unsplash.com/photo-1494548162494-384bba4ab999?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80',
'https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80',
];
return Scaffold(
body: SafeArea(
child: _containerImg(context, screenWidth, screenHeight, _listItem),
),
);
}
Widget _containerImg(
BuildContext context, ScreenWidth, ScreenHeight, List<String> listItem) {
return Container(
height: ScreenHeight,
width: ScreenWidth,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"https://aegis-be.com.sg/wp-content/themes/aegis_theme/images/banner_2.jpg"),
fit: BoxFit.cover,
),
),
alignment: Alignment.bottomCenter,
child: _table(listItem),
);
}
Widget _table(List<String> _listItem) {
return Wrap(
verticalDirection: VerticalDirection.up,
children: _listItem
.map(
(item) => Card(
child: Container(
height: 80,
width: 80,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50.0),
image: DecorationImage(
image: NetworkImage(item),
fit: BoxFit.cover,
),
),
),
),
)
.toList());
}
}