How can I add space equally where I use the dart logo as a floating action button.
I want to use this floating action button in the center so there will be the same space between the five of them. The other 4 icons are the bottom navigation bar item.
Can anyone please help me to find out the solution to this problem?
Here is my code:
import 'package:convex_bottom_bar/convex_bottom_bar.dart';
import 'package:eye_buddy/screen/homepage/pages/profile.dart';
import 'package:eye_buddy/screen/homepage/pages/settings.dart';
import 'package:flutter/material.dart';
import 'chat.dart';
import 'dashboard.dart';
import 'package:eye_buddy/util/colorconfig.dart';
class Home extends StatefulWidget {
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
int currentTab = 0; // to keep track of active tab index
final List<Widget> screens = [
Dashboard(),
Chat(),
Profile(),
Settings(),
]; // to store nested tabs
final PageStorageBucket bucket = PageStorageBucket();
Widget currentScreen = Dashboard(); // Our first view in viewport
#override
Widget build(BuildContext context) {
return Scaffold(
body: PageStorage(
child: currentScreen,
bucket: bucket,
),
floatingActionButton: GestureDetector(
onTap: () {
// Do Something
},
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/icon/eye.png'),
),
// borderRadius: BorderRadius.circular(0.50),
),
width: 110,
height: 94.68,
),
),
// FloatingActionButton(
// elevation: 0.0,
// child: Image.asset("assets/icon/eye.png"),
// backgroundColor: Colors.transparent,
// onPressed: () {},
// ),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: ConvexAppBar(
backgroundColor: colorFromHex('#181D3D'),
style: TabStyle.react,
items: [
TabItem(icon: Icons.search),
TabItem(icon: Icons.bubble_chart),
TabItem(icon: Icons.assessment),
TabItem(icon: Icons.baby_changing_station),
],
initialActiveIndex: 1 /*optional*/,
onTap: (int i) => print('click index=$i'),
),
);
}
}
Try padding your dart logo :
Container(
padding: EdgeInsets.fromLTRB(16, 16, 16, 16),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/icon/eye.png'),
),
// borderRadius: BorderRadius.circular(0.50),
),
If till not works, then :
return BottomAppBar(
color: Colors.white,
child: Container(
height: 64,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [--your nav icons---],
),
),
shape: CircularNotchedRectangle(),
clipBehavior: Clip.antiAlias,
notchMargin: 5.0,
);
Related
I use the CurvedNavigationBar V1.0.3 to navigate between 5 sides. each of them has an extra dart file. The bottombar is in my main.dart.
I want to use a button in /body which leads to another side and change also to the right icon in bottombar.
main.dart
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'Pages/Beritpage.dart';
import 'Pages/donepage.dart';
import 'Pages/settingspage.dart';
import 'Pages/tinapage.dart';
import 'Pages/assmapage.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
static final String title = 'Curved Navigation Bar';
const MyApp({super.key});
#override
Widget build(BuildContext context) => MaterialApp(
debugShowCheckedModeBanner: false,
home: MainPage(),
);
}
class MainPage extends StatefulWidget {
#override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
final navigationKey = GlobalKey<CurvedNavigationBarState>();
int index = 2;
final screens = [
AssmaPage(),
BeritPage(),
TinaPage(),
SettingsPage(),
DonePage(),
];
#override
Widget build(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
double screenHeight = MediaQuery.of(context).size.height;
final items = <Widget>[
Icon(Icons.mediation, size: 30),
Icon(Icons.donut_small, size: 30),
Icon(Icons.home, size: 30),
Icon(Icons.settings, size: 30),
Icon(Icons.done, size: 30),
];
// SafeArea and Cliptrect für IOS
return Container(
color: Color(0xFF145246),
child: SafeArea(
top: false,
child: ClipRect(
child: Scaffold(
extendBody: true,
// Topbar Start
appBar: AppBar(
toolbarHeight: 80,
// Set this height
leading: Icon(Icons.account_circle_rounded),
centerTitle: true,
title: SizedBox(
width: 70,
child: Image.network('https://i.ibb.co/c8ZvGYC/New.png'),
),
//actions: <Widget>[
//Padding(
//padding: EdgeInsets.only(right: 20.0),
//child: GestureDetector(
//onTap: () {},
//child: Icon(
//Icons.,
//size: 26.0,
//),
//)),
//],"
flexibleSpace: Container(
color: Color(0xFF145246),
//child: Column(
//children: [
//Text('One'),
//Text('Two'),
//],
//),
),
),
//Drawer Start
endDrawer: Container(
height: screenHeight,
width: screenWidth * 0.7,
color: Color(0xFF145246)),
//Drawer Ende
//Topbar Ende
body: screens[index],
//Bottombar Start
bottomNavigationBar: Theme(
data: Theme.of(context).copyWith(
iconTheme: IconThemeData(color: Colors.white),
),
child: CurvedNavigationBar(
key: navigationKey,
color: Color(0xFF145246),
buttonBackgroundColor: Color(0xFF145246),
backgroundColor: Colors.transparent,
height: 60,
animationCurve: Curves.easeInOut,
animationDuration: Duration(milliseconds: 300),
index: index,
items: items,
onTap: (index) => setState(() => this.index = index),
),
//Bottombar Ende
),
),
),
),
);
}
}
The recommended way is
final navigationState = navigationKey.currentState!;
navigationState.setPage(0);
but that only seems working, when i have the screens in the same dart file.
i tried to put the code in one of my page files but then i get a
"_CastError (Null check operator used on a null value)"
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'beritpage.dart';
import 'donepage.dart';
import 'settingspage.dart';
import 'tinapage.dart';
import 'assmapage.dart';
import 'package:test_app/main.dart';
class TinaPage extends StatelessWidget {
#override
Widget build(BuildContext context) => Scaffold(
backgroundColor: Color(0xFFB1C5AD),
body: Center(
//Aenderung Hermann
child: Column(
children: <Widget>[
SizedBox(
width: 400,
height: 70,
child: Center(
child: Text('Material'),
),
),
SizedBox(
width: 300,
height: 200,
child: Center(
child: Image.asset('assets/T-Shirt.png'),
),
),
SizedBox(
width: 200,
height: 70,
child: Center(
child: Text('Artikel-Nr.: 1062053001 '),
),
),
SizedBox(
height: 30,
),
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: const StadiumBorder(),
backgroundColor: Color(0xFF145245),
),
onPressed: () {
final navigationState = navigationKey.currentState!;
navigationState.setPage(0);
},
child: const Text('Cotton')),
SizedBox(
height: 30,
),
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: const StadiumBorder(),
backgroundColor: Color(0xFF145245),
),
onPressed: () {},
child: const Text('Viskose')),
SizedBox(
height: 30,
),
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: const StadiumBorder(),
backgroundColor: Color(0xFF145245),
),
onPressed: () {},
child: const Text('Elastan')),
],
),
),
);
}
I also tried Navigation.push to another side. but then i lose the Appbar and Buttonbar which are defined in main.dart.
I wanted to press the button "Cotton" and move to "AssmaPage" and see the first icon in Bottombar is highlighted.
I am trying to make a Bottom Navigation Bar that looks exactly like this. Since I'm just a beginner to learn flutter, I am having a lot of problems one of which is not able to find the icons so I decided to use other similarly available icons. Now I just confused with my own code.
This is what I want:
this is how my Bottom Navigation Bar looks:
This is my code:
Scaffold(bottomNavigationBar:
Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
height: 50,
width: MediaQuery.of(context).size.width / 5,
decoration: BoxDecoration(color:Color(0xfffed307)),
child: Column(
children: [
Icon(Icons.store_mall_directory_outlined),
Text('My Page')
],
),
),
Container(
height: 50,
width: MediaQuery.of(context).size.width / 5,
decoration: BoxDecoration(color: Color(0xfffed307)),
child: Column(
children: [Icon(Icons.apps), Text('Election')],
),
),
Container(
height: 50,
width: MediaQuery.of(context).size.width / 5,
decoration: BoxDecoration(color: Color(0xfffed307)),
child: Image.asset('images/scan_icon.png'),
),
Container(
height: 50,
width: MediaQuery.of(context).size.width / 5,
decoration: BoxDecoration(color: Color(0xfffed307)),
child: Column(
children: [Icon(Icons.apps), Text('Add Election')],
),
),
Expanded(
child: Container(
height: 50,
width: MediaQuery.of(context).size.width / 5,
decoration: BoxDecoration(color: Color(0xfffed307)),
child: Column(
children: [Icon(Icons.groups_outlined), Text('Customer')],
),
),
),
],
)
,);
You can use floatingActionButton for the scan icon and use floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
With Flutter, to make this kind of UI is easy peasy)) Use Positioned Widget inside Stack Widget if you really wanna make this UI using bottomNavigationBar property of Scaffold Widget.
Result UI
Copy and paste the code below to see the effect:
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(
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: MyScreen(),
);
}
}
class MyScreen extends StatefulWidget {
const MyScreen({Key? key}) : super(key: key);
#override
_MyScreenState createState() => _MyScreenState();
}
class _MyScreenState extends State<MyScreen> {
late List<Widget> _screens;
int currentIndex = 0;
#override
void initState() {
super.initState();
_screens = [
TestScreen(title: '1st Screen'),
TestScreen(title: '2nd Screen'),
TestScreen(title: '3rd Screen'),
TestScreen(title: '4th Screen'),
TestScreen(title: '5th Screen'),
];
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: currentIndex,
children: _screens,
),
bottomNavigationBar: BottomBar(
selectedIndex: currentIndex,
children: [
BottomBarItem(icon: Icons.home),
BottomBarItem(icon: Icons.search),
BottomBarItem(icon: Icons.favorite),
BottomBarItem(icon: Icons.person),
],
onMainPressed: () {
setState(() {
currentIndex = 4;
});
},
onPressed: (index) {
setState(() {
currentIndex = index;
});
},
),
);
}
}
class BottomBarItem {
BottomBarItem({required this.icon});
IconData icon;
}
class BottomBar extends StatelessWidget {
final List<BottomBarItem> children;
final Function? onPressed;
final Function? onMainPressed;
final selectedIndex;
BottomBar({
this.children = const [],
this.onPressed,
this.onMainPressed,
this.selectedIndex,
});
#override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Container(
color: Colors.grey[100],
child: SafeArea(
bottom: true,
child: Container(
height: 60,
decoration: BoxDecoration(
color: Colors.grey[100],
boxShadow: [
BoxShadow(
color: Colors.grey,
blurRadius: 8.0,
offset: Offset(
0.0, // horizontal, move right 10
-6.0, // vertical, move down 10
),
),
],
),
child: Stack(
clipBehavior: Clip.none,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: children.map<Widget>(
(item) {
int index = children.indexOf(item);
bool isSelected = selectedIndex == index;
return Expanded(
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
onPressed!(index);
},
child: Padding(
padding: EdgeInsets.zero,
child: Icon(
item.icon,
size: isSelected ? 35 : 30,
color: isSelected ? Colors.blue : Colors.grey,
),
),
),
),
);
},
).toList()
..insert(2, SizedBox(width: 80)),
),
Positioned(
top: -14,
width: size.width,
child: Center(
child: Container(
height: 60,
width: 60,
child: ClipOval(
child: Material(
color: selectedIndex == 4 ? Colors.blue : Colors.grey,
child: InkWell(
onTap: () {
onMainPressed!();
},
child: Center(
child: Icon(
Icons.adb,
size: 27,
color: Colors.white,
),
),
),
),
),
decoration: BoxDecoration(
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: Colors.grey,
blurRadius: 6.0,
),
],
),
),
),
),
],
),
),
),
);
}
}
class TestScreen extends StatelessWidget {
final String title;
const TestScreen({required this.title, Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: Text(title),
)),
);
}
}
I have this piece of code inside a scaffold with SafeArea:
Column(
children: [Flexible(
fit: FlexFit.loose,
child: Container(
// width: 130,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [Icon(Icons.ac_unit), Text("Trending")],
),
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.grey[300]),
),
)]
)
Which gives the following result:
What I want to achieve is this:
How do I make the row warp around the text.
from the documentation
Wrap(
spacing: 8.0, // gap between adjacent chips
runSpacing: 4.0, // gap between lines
children: <Widget>[
Chip(
avatar: CircleAvatar(backgroundColor: Colors.blue.shade900, child: Text('AH')),
label: Text('Hamilton'),
),
Chip(
avatar: CircleAvatar(backgroundColor: Colors.blue.shade900, child: Text('ML')),
label: Text('Lafayette'),
),
Chip(
avatar: CircleAvatar(backgroundColor: Colors.blue.shade900, child: Text('HM')),
label: Text('Mulligan'),
),
Chip(
avatar: CircleAvatar(backgroundColor: Colors.blue.shade900, child: Text('JL')),
label: Text('Laurens'),
),
],
)
if you want to use that row just do
Row(
mainAxisSize: MainAxisSize.min
children: [
...
])
replace that Chip() with the above row
Edit:
replace the Chip() with that _buildChip() custom widget something like this, It should give you a better control over the widget
import 'package:flutter/material.dart';
main() {
runApp(MaterialApp(
home: MyPage(),
));
}
class MyPage extends StatelessWidget {
Widget _buildChip() {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20), color: Colors.grey.withOpacity(.4) ),
child: InkWell(
highlightColor: Colors.blue.withOpacity(.4),
splashColor: Colors.green,
borderRadius: BorderRadius.circular(20),
onTap: () {
},
child: Container(
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 7),
child: Row(
mainAxisSize: MainAxisSize.min,
children:[
Icon(Icons.ac_unit, size: 14),
SizedBox(width: 10),
Text("Trending")
]),
),
),
);
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Wrap(
spacing: 8.0, // gap between adjacent chips
runSpacing: 4.0, // gap between lines
children: <Widget>[
_buildChip(),
_buildChip(),
_buildChip(),
_buildChip(),
_buildChip(),
],
)
);
}
}
let me know if It is what you want, so that I edit that answer
Take a look at this example. You should be using Wrap and RichText to achieve this design. Not Flexible, because it will take all the available space.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key? key, required this.title}) : super(key: key);
#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: Wrap(children: [
Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20), color: Colors.grey[300]),
child: RichText(
text: TextSpan(
children: [
WidgetSpan(
child: Icon(Icons.ac_unit, size: 14),
),
TextSpan(
text: "Trending",
),
],
),
),
),
SizedBox(width: 5,),
Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20), color: Colors.grey[300]),
child: RichText(
text: TextSpan(
children: [
WidgetSpan(
child: Icon(Icons.ac_unit, size: 14),
),
TextSpan(
text: "Trending",
),
],
),
),
)
]),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Immediately after my app start it will show a button.
As soon as this button is pressed I want to build a slider within the same button to control the volume for this sound.
All I need is to make this slider appear and not the mechanics to control the volume.
what i want to acheive is here..
my button code
void main() => runApp(Home());
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: "RelaX",
home: Container(
child: Scaffold(
appBar: AppBar(
elevation: 20.0,
backgroundColor: Color(0xFF001E3D),
title: Text(
'Relax',
style: GoogleFonts.raleway(
fontSize: 30.0,
color: Color(0xFF55b9f3),
),
),
centerTitle: true,
),
body: Column(
children: <Widget>[
RaisedButton(
child: Text("View Slider"),
onPressed: () => print("view slider")),
],
),
),
),
);
}
}
You may use the Visibility widget to set the visibility of the slider. Please see the code below. I'm using Container along with Inkwell to achieve the same effect as RaisedButton.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
double _currentSliderValue = 0;
bool _sliderVisible = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Raised Button"),
),
body: Center(
child: ClipRRect(
borderRadius: const BorderRadius.all(
Radius.circular(20),
),
child: Container(
decoration: BoxDecoration(
color: Colors.blue[200],
borderRadius: const BorderRadius.all(
Radius.circular(20),
),
),
child: Material(
elevation: 2,
child: InkWell(
onTap: () {
setState(() {
_sliderVisible = !_sliderVisible;
});
},
child: Container(
width: 125.0,
height: 125.0,
child: Column(
children: [
const SizedBox(
height: 15,
),
Icon(
Icons.nightlight_round,
color: Colors.indigo[900],
size: 48,
),
const SizedBox(
height: 5,
),
Visibility(
visible: _sliderVisible,
child: Slider(
value: _currentSliderValue,
min: 0,
max: 10,
onChanged: (double value) {
setState(() {
_currentSliderValue = value;
});
},
activeColor: Colors.indigo[900],
inactiveColor: Colors.indigo[900],
),
)
],
),
),
),
color: Colors.transparent,
),
),
),
),
);
}
}
I am new to flutter and I have created a menu button to open a side menu but however, I need some help to make it happen.
Can anyone help me implemented or guide me in my code of how to make it work. please and thank you very much!
Here is my code:
Widget _icon(IconData icon, {Color color = LightColor.iconColor}) {
return Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(13)),
color: Theme.of(context).backgroundColor,
boxShadow: AppTheme.shadow),
child: Icon(
icon,
color: color,
),
);
}
Main:
class _MainPageState extends State<MainPage> {
bool isHomePageSelected = true;
Widget _appBar() {
return Container(
padding: AppTheme.padding,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
RotatedBox(
quarterTurns: 4,
child: _icon(Icons.menu, color: Colors.black54),
),
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(13)),
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).backgroundColor,
boxShadow: <BoxShadow>[
BoxShadow(
color: Color(0xfff8f8f8),
blurRadius: 10,
spreadRadius: 10),
],
),
),
)
],
),
);
}
You can copy paste run full code below
You can use GlobalKey() and call _key.currentState.openDrawer();
code snippet
GlobalKey<ScaffoldState> _key = GlobalKey();
...
child: InkWell(
onTap: () {
_key.currentState.openDrawer();
},
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,
),
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;
GlobalKey<ScaffoldState> _key = GlobalKey();
void _incrementCounter() {
setState(() {
_counter++;
});
}
Widget _icon(IconData icon, {Color color = Colors.blue}) {
return Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(13)),
color: Theme.of(context).backgroundColor,
//boxShadow: AppTheme.shadow
),
child: InkWell(
onTap: () {
_key.currentState.openDrawer();
},
child: Icon(
icon,
color: color,
),
),
);
}
bool isHomePageSelected = true;
Widget _appBar() {
return Container(
//padding: AppTheme.padding,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
RotatedBox(
quarterTurns: 4,
child: _icon(Icons.menu, color: Colors.black54),
),
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(13)),
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).backgroundColor,
boxShadow: <BoxShadow>[
BoxShadow(
color: Color(0xfff8f8f8),
blurRadius: 10,
spreadRadius: 10),
],
),
),
)
],
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
key: _key,
appBar: AppBar(leading: _appBar()),
drawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: Text('Item 1'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
],
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Use this code:
Scaffold.of(context).openDrawer();
Reference: https://api.flutter.dev/flutter/material/ScaffoldState/openDrawer.html
The recommended way is to call Scaffold.of(context).openDrawer();
Sample Code:
drawer: const NavBar(), // Your nav bar here
appBar: AppBar(
title: "Home".appBarText(),
leading: Builder(builder: (context) {
return IconButton(
icon: const Icon(Icons.menu),
onPressed: () {
Scaffold.of(context).openDrawer();
},
);