Can anyone advise on how to create layout as shown below using flutter for displaying image.
1 big square with 5 small square and must resize accordingly according to the width of the screen.
In this case you can use flutter_staggered_grid_view package.
import the package in pubspec.yaml .
dependencies:
...
flutter_staggered_grid_view: ^0.2.2
Follow the Following Code To get staggered grid view.
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new HomePage(),
);
}
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text("Demo"),
),
body: new Padding(
padding: const EdgeInsets.only(top: 12.0),
child: new StaggeredGridView.count(
crossAxisCount: 3,
staggeredTiles: _staggeredTiles,
children: _tiles,
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
padding: const EdgeInsets.all(4.0),
)
)
);
}
}
List<StaggeredTile> _staggeredTiles = const <StaggeredTile>[
const StaggeredTile.count(2, 2),
const StaggeredTile.count(1, 1),
const StaggeredTile.count(1, 1),
const StaggeredTile.count(1, 1),
const StaggeredTile.count(1, 1),
const StaggeredTile.count(1, 1),
];
List<Widget> _tiles = const <Widget>[
const _Example01Tile(Colors.green, Icons.widgets),
const _Example01Tile(Colors.lightBlue, Icons.wifi),
const _Example01Tile(Colors.amber, Icons.panorama_wide_angle),
const _Example01Tile(Colors.brown, Icons.map),
const _Example01Tile(Colors.deepOrange, Icons.send),
const _Example01Tile(Colors.indigo, Icons.airline_seat_flat),
];
class _Example01Tile extends StatelessWidget {
const _Example01Tile(this.backgroundColor, this.iconData);
final Color backgroundColor;
final IconData iconData;
#override
Widget build(BuildContext context) {
return new Card(
color: backgroundColor,
child: new InkWell(
onTap: () {},
child: new Center(
child: new Padding(
padding: const EdgeInsets.all(4.0),
child: new Icon(
iconData,
color: Colors.white,
),
),
),
),
);
}
}
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
return new Container(
child: new Column(
children: <Widget>[
new Row(children: <Widget>[
new Container(
width: 2*width/4,
height: 2*width/4,
color: Colors.lightGreen,
),
new Column(children: <Widget>[
new Container(
width: width/4,
height: width/4,
color: Colors.redAccent,
),
new Container(
width: width/4,
height: width/4,
color: Colors.red,
)
],)
],),
new Row(children: <Widget>[
new Container(
width: width/4,
height: width/4,
color: Colors.black54,
),
new Container(
width: width/4,
height: width/4,
color: Colors.redAccent,
),
new Container(
width: width/4,
height: width/4,
color: Colors.green,
),
],)
],
),
);
}
Related
Executed program picture I need help. I want to make the button click circular instead of rectangular.
This is the flutter dart code that I've provided, I want to know what attribute I should change or insert for making my question possible.
import 'package:flutter/material.dart';
import 'dart:math';
void main() {
runApp(MaterialApp(
title: 'Ask me anything',
home: Scaffold(
appBar: AppBar(
title: Text('Ask me anything'),
centerTitle: true,
backgroundColor: Colors.blue[700],
),
backgroundColor: Colors.blue[800],
body: MyApp(),
),
));
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int ballNumber = 1;
#override
Widget build(BuildContext context) {
return Center(
child: Expanded(
child: TextButton(
style: ButtonStyle(enableFeedback: true,shape: ),
onPressed: () => setState(() {
ballNumber = Random().nextInt(5) + 1;
}),
child: Image.asset(
'images/ball$ballNumber.png',
width: 400,
// height: 200,
),
),
));
}
}
You can either change button style, or just wrap the image with GestureDetector.
Round Button
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
padding: const EdgeInsets.all(50),
),
onPressed: () {},
child: const FittedBox(
child: Text('Round Button'),
),
),
ElevatedButton(
style: ButtonStyle(
shape: MaterialStateProperty.all<CircleBorder>(
const CircleBorder(),
),
padding: MaterialStateProperty.all<EdgeInsets>(
const EdgeInsets.all(50),
),
),
onPressed: () {},
child: const FittedBox(
child: Text('Round Button'),
),
),
GestureDetector
GestureDetector(
onTap: () {
setState(() {
ballNumber = Random().nextInt(5) + 1;
});
},
child: Image.asset('images/ball$ballNumber.png'),
),
I learnt how to make this navigation bar on Youtube for my app but I'm struggling implementing navigation and routing from other tutorials online to the navigation bar that I created.
As you can see below, I've created the bottom navigation bar and called it BottomNavyBar() . As I press each Icon I'd like it to go to a new screen made from a separate dart file to show that each icon goes to a new route.
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
void main() => runApp(App());
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Homepage(),
);
}
}
class Homepage extends StatefulWidget {
#override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
double xOffset = 0;
double yOffset = 0;
double scaleFactor = 1;
bool isDrawerOpen = false;
#override
Widget build(BuildContext context) {
return AnimatedContainer(
duration: Duration(microseconds: 250),
transform: Matrix4.translationValues(xOffset, yOffset, 0)
..scale(scaleFactor),
child: Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
Expanded(
child: Align(
alignment: Alignment.topCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
isDrawerOpen
? IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: () {
setState(() {
xOffset = 0;
yOffset = 0;
scaleFactor = 1;
isDrawerOpen = false;
});
},
)
: IconButton(
icon: Icon(Icons.menu),
onPressed: () {
setState(() {
xOffset = 230;
yOffset = 150;
scaleFactor = 0.6;
isDrawerOpen = true;
});
},
),
Text(
'nghbrly',
style: GoogleFonts.quicksand(
color: const Color(0xff8fc6bb), fontSize: 45),
),
Image(image: AssetImage('assets/images/letterbox.png')),
],
),
),
),
],
),
),
bottomNavigationBar: BottomNavyBar(),
),
);
}
}
class NghbrlySideMenu extends StatefulWidget {
#override
_NghbrlySideMenuState createState() => _NghbrlySideMenuState();
}
class _NghbrlySideMenuState extends State<NghbrlySideMenu> {
#override
Widget build(BuildContext context) {
return Container();
}
}
class BottomNavyBar extends StatefulWidget {
#override
_BottomNavyBarState createState() => _BottomNavyBarState();
}
class _BottomNavyBarState extends State<BottomNavyBar> {
int selectedIndex = 0;
Color backgroundColor = Colors.white;
List<NavigationItem> items = [
NavigationItem(
Icon(
const IconData(0xe904, fontFamily: 'nghbrly'),
),
Text(' Borrow')),
NavigationItem(
Icon(
const IconData(0xe9c8, fontFamily: 'nghbrlyfullicons'),
),
Text(' Lend')),
NavigationItem(
Icon(
const IconData(0xe97a, fontFamily: 'nghbrlyfullicons'),
),
Text(' Favorites')),
NavigationItem(
Icon(
const IconData(0xe996, fontFamily: 'nghbrlyfullicons'),
),
Text(' Nghbrs')),
NavigationItem(
Icon(
const IconData(0xe9cc, fontFamily: 'nghbrlyfullicons'),
),
Text(' Cart')),
];
Widget _buildItem(NavigationItem item, bool isSelected) {
return AnimatedContainer(
duration: Duration(milliseconds: 270),
height: double.maxFinite,
width: isSelected ? 125 : 50,
padding: isSelected
? EdgeInsets.only(left: 16, right: 16)
: EdgeInsets.only(left: 13, right: 8),
decoration: isSelected
? BoxDecoration(
color: const Color(0xff8fc6bb),
borderRadius: BorderRadius.all(Radius.circular(50)))
: null,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
IconTheme(
data: IconThemeData(
size: 24,
color: isSelected ? backgroundColor : const Color(0xff655454),
),
child: item.icon,
),
Padding(
padding: const EdgeInsets.only(left: 5),
child: isSelected
? DefaultTextStyle.merge(
style: TextStyle(
color: backgroundColor,
),
child: item.title)
: Container(),
)
],
)
],
),
);
}
//int _currentIndex = 0;
//final List<Widget> _children = [];
#override
Widget build(BuildContext context) {
return SafeArea(
bottom: true,
child: Container(
height: 56,
padding: EdgeInsets.only(left: 8, right: 8, top: 4, bottom: 4),
decoration: BoxDecoration(color: backgroundColor, boxShadow: [
BoxShadow(
color: Colors.white,
blurRadius: 0,
)
]),
width: MediaQuery.of(context).size.width,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: items.map((item) {
var itemIndex = items.indexOf(item);
return GestureDetector(
onTap: () {
setState(() {
selectedIndex = itemIndex;
});
},
child: _buildItem(item, selectedIndex == itemIndex),
);
}).toList(),
),
),
);
}
}
Another thing that I'd like to find out if possible is, what if I wanted to place the navigation bar below the app heading "nghbrly" like in this picture . I'd really appreciate it.
use this plugin Presistant_bottom_nav_bar. you can use the bottomnav bar in every pages.also you can disable the bottomnav in specific screen checkout above link
it very good plugin in my opinion.checkout navgation style in this link.you can change the desgin of bottomnavbar navBarStyle: NavBarStyle.style9, just change the style9 to whatever number provided by plugin. i believe 15 of them available
You can use your custom icon instead of default icon, also instead of this CupertinoColors.systemPurple you can also use Colors.red kind of this Let me know if it works
PersistentTabController _controller =PersistentTabController(initialIndex: 0);
//Screens for each nav items.
List<Widget> _NavScreens() {
return [
Screen1(),
Screen2(),
Screen3(),
Screen4(),
];
}
List<PersistentBottomNavBarItem> _navBarsItems() {
return [
PersistentBottomNavBarItem(
icon: Icon(Icons.home),
title: ("Borrow"),
activeColor: CupertinoColors.activeBlue,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(Icons.favorite),
title: ("Lend"),
activeColor: CupertinoColors.activeGreen,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(Icons.person_pin),
title: ("Favorites"),
activeColor: CupertinoColors.systemRed,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(Icons.local_activity),
title: ("Cart"),
activeColor: CupertinoColors.systemIndigo,
inactiveColor: CupertinoColors.systemGrey,
),
];
}
#override
Widget build(BuildContext context) {
return Center(
child: PersistentTabView(
controller: _controller,
screens: _NavScreens(),
items: _navBarsItems(),
confineInSafeArea: true,
backgroundColor: Colors.white,
handleAndroidBackButtonPress: true,
resizeToAvoidBottomInset: true,
hideNavigationBarWhenKeyboardShows: true,
decoration: NavBarDecoration(
borderRadius: BorderRadius.circular(10.0),
),
popAllScreensOnTapOfSelectedTab: true,
navBarStyle: NavBarStyle.style9,
),
);
}
This is working box with rounded corner on the bottom left corner:
Container(
decoration: BoxDecoration(
// border: Border(
// right: BorderSide(
// color: Colors.red,
// width: 2,
// ),
// ),
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(10.0)),
color: Colors.grey,
),
child: new Column(
children: <Widget>[
new Padding(
padding: EdgeInsets.only(top: 5, bottom: 5),
child: new Text("......"))
],
))
If I uncomment the above 6 lines, the content disappears, but no top border appears.
What am I doing wrong?
You can copy paste run full code below
This violate rule in framework's box_border.dart
assert(borderRadius == null, 'A borderRadius can only be given for uniform borders.');
You can use ClipRRect's borderRadius
code snippet
ClipRRect(
borderRadius: BorderRadius.only(bottomLeft: Radius.circular(10.0)),
child: Container(
decoration: BoxDecoration(
border: Border(
right: BorderSide(
color: Colors.red,
width: 2,
),
),
color: Colors.grey,
),
working demo
full code
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> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ClipRRect(
borderRadius: BorderRadius.only(bottomLeft: Radius.circular(10.0)),
child: Container(
decoration: BoxDecoration(
border: Border(
right: BorderSide(
color: Colors.red,
width: 2,
),
),
//borderRadius: BorderRadius.only(bottomLeft: Radius.circular(10.0)),
color: Colors.grey,
),
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 5, bottom: 5),
child: Text("......"))
],
)),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
I want to build a search page with multiple search filter exactly like the one in the picture... please advice on how to achieve this or refer me to a place (link) where i can get the information on how to do it. i SHOULD BE ABLE TO ENTER ALL THE FILTERS AND EXECUTE IT ALL WITH A BUTTON AND DISPLAY THE CRITERIA RESULTS. THANK YOU IN ADVANCE.
You can use ToggleButton and DropdownButton for your use case. Here's a sample to help you get started.
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(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<bool> purchaseType = [false, false, false];
String propertyType = 'Apartment & Unit';
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Stack(
alignment: AlignmentDirectional.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: const EdgeInsets.fromLTRB(8, 1, 8, 1),
height: 50,
width: 100,
color: Colors.grey,
),
Container(
margin: const EdgeInsets.fromLTRB(8, 1, 8, 1),
height: 50,
width: 100,
color: Colors.grey,
),
Container(
margin: const EdgeInsets.fromLTRB(8, 1, 8, 1),
height: 50,
width: 100,
color: Colors.grey,
),
],
),
ToggleButtons(
renderBorder: false,
selectedColor: Colors.white,
disabledColor: Colors.black,
highlightColor: Colors.black54,
fillColor: Colors.black,
textStyle: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 18),
isSelected: purchaseType,
children: const [
Padding(
padding: EdgeInsets.all(8.0),
child: SizedBox(
height: 36,
width: 100,
child: Center(child: Text('Buy')),
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: SizedBox(
height: 36,
width: 100,
child: Center(child: Text('Rent')),
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: SizedBox(
height: 36,
width: 100,
child: Center(child: Text('Sell')),
),
),
],
onPressed: (int index) {
setState(() {
var cursor = purchaseType.length - 1;
while (cursor >= 0) {
if (cursor == index) {
purchaseType[index] = !purchaseType[index];
} else {
purchaseType[cursor] = false;
}
cursor--;
}
// isSelected[index] = !isSelected[index];
});
},
),
],
),
Container(
alignment: AlignmentDirectional.centerStart,
padding: const EdgeInsets.fromLTRB(16, 16, 16, 0),
child: const Text('Property Type'),
),
Container(
height: 62,
alignment: AlignmentDirectional.centerStart,
padding: const EdgeInsets.all(16),
margin: const EdgeInsets.all(16),
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
borderRadius: BorderRadius.circular(5),
),
child: DropdownButton<String>(
isExpanded: true,
value: propertyType,
icon: const Icon(Icons.expand_more),
iconSize: 24,
elevation: 16,
underline: Container(
height: 2,
),
onChanged: (String? newValue) {
setState(() {
propertyType = newValue!;
});
},
items: <String>[
'Apartment & Unit',
'High-rise Unit',
'Single-family Home',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
],
),
),
);
}
}
Demo
How can I make a Card in Flutter that overlaps the AppBar? Negative margins are not possible as far as I know.
See the image for clarity.
For one card it could be easily done with Stack widget
E.g.
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
Home({Key key}) : super(key: key);
#override
HomeState createState() {
return new HomeState();
}
}
class HomeState extends State<Home> {
bool _hasCard;
#override
void initState() {
super.initState();
_hasCard = false;
}
#override
Widget build(BuildContext context) {
List<Widget> children = new List();
children.add(_buildBackground());
if (_hasCard) children.add(_buildCard());
return MaterialApp(
home: Stack(
children: children,
),
);
}
void _showCard() {
setState(() => _hasCard = true);
}
void _hideCard() {
setState(() => _hasCard = false);
}
Widget _buildCard() => new Container(
child: new Center(
child: new Container(
height: 700.0,
width: 200.0,
color: Colors.lightBlue,
child: new Center(
child: new Text("Card"),
),
),
),
);
Widget _buildBackground() => new Scaffold(
appBar: new AppBar(
title: new Text("AppBar"),
),
body: new Container(
child: _hasCard
? new FlatButton(
onPressed: _hideCard, child: new Text("Hide card"))
: new FlatButton(
onPressed: _showCard, child: new Text("Show card")),
),
);
}
void main() {
runApp(
new Home(),
);
}
If there are many cards, you can wrap them into ListView.
class Sample2 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Material(
child: CustomScrollView(
slivers: [
SliverPersistentHeader(
delegate: MySliverAppBar(expandedHeight: 200),
pinned: true,
),
SliverList(
delegate: SliverChildBuilderDelegate(
(_, index) => ListTile(
title: Text("Index: $index"),
),
),
)
],
),
),
);
}
}
class MySliverAppBar extends SliverPersistentHeaderDelegate {
final double expandedHeight;
MySliverAppBar({#required this.expandedHeight});
#override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return Stack(
fit: StackFit.expand,
overflow: Overflow.visible,
children: [
Image.network(
"https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
fit: BoxFit.cover,
),
Center(
child: Opacity(
opacity: shrinkOffset / expandedHeight,
child: Text(
"MySliverAppBar",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w700,
fontSize: 23,
),
),
),
),
Positioned(
top: expandedHeight / 2 - shrinkOffset,
left: MediaQuery.of(context).size.width / 4,
child: Opacity(
opacity: (1 - shrinkOffset / expandedHeight),
child: Card(
elevation: 10,
child: SizedBox(
height: expandedHeight,
width: MediaQuery.of(context).size.width / 2,
child: FlutterLogo(),
),
),
),
),
],
);
}
#override
double get maxExtent => expandedHeight;
#override
double get minExtent => kToolbarHeight;
#override
bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) => true;
}