When I use SubmenuButton, setting Alignment.center for ButtonStyle doesn't work
runApp(MaterialApp(
title: "菜单",
// theme: ThemeData(useMaterial3: true),
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.blue,
useMaterial3: true,
// expansionTileTheme: ExpansionTileThemeData()
),
home: Scaffold(
appBar: AppBar(title: const Text("菜单工具栏")),
body: const SizedBox(
height: 40,
width: 100,
child: SubmenuButton(
style: ButtonStyle(
alignment: Alignment.center,
backgroundColor: MaterialStatePropertyAll(Colors.blue),
),
menuStyle: MenuStyle(alignment: Alignment.centerRight),
menuChildren: [
MenuItemButton(child: Text("submenu one")),
MenuItemButton(child: Text("submenu two")),
MenuItemButton(child: Text("submenu three")),
],
child: Icon(Icons.star),
),
),
),
));
Example:
Why, is this a bug?
Someone tell me why?
We can provide the width on child by removing default padding.
body: const SizedBox(
height: 40,
width: 100,
child: SubmenuButton(
style: ButtonStyle(
alignment: Alignment.center,
backgroundColor: MaterialStatePropertyAll(Colors.blue),
),
menuStyle: MenuStyle(alignment: Alignment.centerRight),
menuChildren: [
MenuItemButton(child: Text("submenu one")),
MenuItemButton(child: Text("submenu two")),
MenuItemButton(child: Text("submenu three")),
],
child: SizedBox(
// hoped Center would solve but it is not getting enough constraints,
// while size goes Up on flutter , I am providing width here
width: 100 - 24, //-12*2 is padding of both side
child: Icon(Icons.star),
),
),
more about layout/constraints.
Related
I want to increase CupertinoNavigationBar heigth. The code is like this:
child: CustomCupertinoNavigationBar(
padding: EdgeInsetsDirectional.zero,
backgroundColor: Colors.white,
middle: Semantics(
label: "dashboard-main-page-title",
child: Text(
"My Title",
style: TextStyles.HankenSans_Bold_18_PrimaryBlack,
key: Key('dashboard-main-page-title'),
),
),
leading: Semantics(
label: "dashboard-back-button",
child: Material(
color: Colors.white,
child: CustomBackButton(
onPressHandler: () {
Navigation().openMyAccountPage();
},
),
),
),
);
I tried creating my own custom cupertino. I copied the cupertino/nav_bar.dart and changed _kNavBarPersistentHeight parameter as const double _kNavBarPersistentHeight = 58.0; but it resulted at two navigation bars in IOS. Can anybody help me with this? Much appreciated.
I solved this problem as
appBar: PreferredSize(
preferredSize: Size.fromHeight(100.0),
child: Container(
height: 120,
child: CupertinoNavigationBar(
padding: EdgeInsetsDirectional.zero,
backgroundColor: Colors.white,
middle: Semantics(
label: "dashboard-main-page-title",
child: Text(
CustomerLoyaltyLocalizations.instance.dashboardMainPageTitle,
style: TextStyles.HankenSans_Bold_18_PrimaryBlack,
key: Key('dashboard-main-page-title'),
),
),
leading: Semantics(
label: "dashboard-back-button",
child: Material(
color: Colors.white,
child: CustomBackButton(
onPressHandler: () {
Navigation().openMyAccountPage();
},
),
),
),
),
),
),
First I used PreferredSize and then Container widget before CupertinoNavigationBar.
In this simple implementation code I added some PopupMenuButton items into AppBar actions argument and now I want to styling PopupMenuDivider color which that have default color.
You can do this very easily using the Theme class, using which you can change the icon color, text color and PopupMenuDivider color.
Again, you can easily do this on Divider at PopupMenuItem using PopupMenuDivider.
appBar: AppBar(
title: Text("PopUpMenu Flutter"),
centerTitle: true,
actions: [
Theme(
data: Theme.of(context).copyWith(
dividerTheme: DividerThemeData(
color: Colors.black,
),
iconTheme: IconThemeData(color: Colors.white),
textTheme: TextTheme().apply(bodyColor: Colors.white),
),
child: PopupMenuButton<int>(
color: Colors.indigo,
//onSelected: (item) => onSelected(context, item),
itemBuilder: (context) => [
PopupMenuItem<int>(
value: 0,
child: Text('Settings'),
),
PopupMenuDivider(),
PopupMenuItem<int>(
value: 1,
child: Text('Share'),
),
PopupMenuDivider(),
PopupMenuItem<int>(
value: 2,
child: Row(
children: [
Icon(Icons.logout),
const SizedBox(width: 8),
Text('Sign Out'),
],
),
),
],
),
),
],
),
You can use PopupMenuItem child and assign Column like
PopupMenuItem<WhyFarther>(
value: WhyFarther.harder,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Working a lot harder'),
Divider(
color: Colors.grey,
)
],
),
),
the floating action button is not defined is shown on vs code I am unable to understand my self I was just following a youtube tutorial.
import 'package:flutter/material.dart';
void main() => runApp(
MaterialApp(
home:Scaffold(
appBar: AppBar(
title: Text('flutter'),
centerTitle: true ,
),
body:Center(
child:Text("welcome to flutter") ,)
),
floatingActionButton : FloatingActionButton(
child: Text('click')
),
),
);
You are using the floatingActionButton property on MaterialApp.
It is only available for a Scaffold.
Try like this:
void main() =>
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('flutter'),
centerTitle: true,
),
body: Center(
child: Text("welcome to flutter"),
),
floatingActionButton: FloatingActionButton(
onPressed: () => print("FloatingActionButton Clicked"),
child: Text('click'))),
),
);
You are using the floatingActionButton property on MaterialApp.
It is only available for a Scaffold. but if you want same functionality you Text Button like this.
TextButton(
child:Container(
width: 40,
height: 40,
child: Icon(
Icons.add,
color: Colors.white,
),
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromARGB(250, 255, 166, 158),
Color.fromARGB(
250,
250,
243,
221,
),
]))),
onPressed: () {
},
),
I am using CurvedNavigationBar as my bottom Navigation Bar. On tap, I want to change this black icon colour to primary colour. But when it is not tapped I want ti as a black colour only. How can I do it?
I tried changing the last three properties of CurvedNavigationBar but not getting the desired output.
Code:
final bottomNavBar = CurvedNavigationBar(
items: [
SvgPicture.asset(
'assets/images/hand-shake.svg',
key: Key('SessionsPage'),
height: ScreenUtil().setHeight(26),
width: ScreenUtil().setWidth(26),
color: Colors.black,
),
SvgPicture.asset(
'assets/images/home.svg',
height: ScreenUtil().setHeight(26),
key: Key("FeedsPage"),
width: ScreenUtil().setWidth(26),
color: Colors.black,
),
Card(
elevation: 5,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(50)),
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 24,
child: Icon(
Icons.edit,
color: Colors.black,
size: 35,
),
),
),
Image.asset(
'assets/images/booking_icon.png',
height: ScreenUtil().setHeight(26),
width: ScreenUtil().setWidth(26),
color: Colors.black,
),
StreamBuilder(
stream: usersRef
.document(CurrentUser().currentUser.databaseID)
.collection('Alerts')
.snapshots(),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return Stack(
children: <Widget>[
SvgPicture.asset(
'assets/images/noti.svg',
height: ScreenUtil().setHeight(26),
width: ScreenUtil().setWidth(26),
color: Colors.black,
),
CircleAvatar(
radius: 2,
)
],
);
default:
List rev1 = snapshot.data.documents.toList();
List res1 = [];
for (var i in rev1) {
if (i.data['seen'] == false) res1.add(i.documentID);
}
return Stack(
children: <Widget>[
SvgPicture.asset(
'assets/images/noti.svg',
height: ScreenUtil().setHeight(26),
width: ScreenUtil().setWidth(26),
color: Colors.black,
),
res1.length == 0
? CircleAvatar(
radius: 2,
)
: CircleAvatar(
radius: 8,
backgroundColor: Colors.white,
child: Text(
res1.length.toString(),
style: TextStyle(fontSize: 10),
),
),
],
);
}
// child: SvgPicture.asset(
// 'assets/images/noti.svg',
// height: ScreenUtil().setHeight(28),
// width: ScreenUtil().setWidth(28),
// color: whiteColor,
// ),
}),
],
onTap: onTabTapped,
index: _currentIndex,
backgroundColor: Colors.white,
buttonBackgroundColor: whiteColor,
color: Colors.white,
height: 60,
);
return Scaffold(bottomNavigationBar: bottomNavBar, body: returnBody());
}
}
I do not know this CurvedNavigationBar widget, but, looking to your code I can assume that you already have the selected index inside the _currentIndex variable.
So, you can just change your items do validate this property to define the correct color:
CurvedNavigationBar(
items: [
SvgPicture.asset(
'assets/images/hand-shake.svg',
key: Key('SessionsPage'),
height: ScreenUtil().setHeight(26),
width: ScreenUtil().setWidth(26),
color: _currentIndex == 0 ? Colors.red : Colors.black,
),
// Other items, incrementing the index validation (e.g. == 1, == 2, etc)
],
// Other properties...
);
Just change the Colors.red to the color you want.
outside the build
final colorsNavigation = [
Colors.purple,
Colors.amber,
Colors.orange,
Colors.red,
Colors.black26
];
inside the build and scaffold
bottomNavigationBar: Theme(
data: Theme.of(context).copyWith(
iconTheme: const IconThemeData(color: Colors.white)
),
child: CurvedNavigationBar(
//buttonBackgroundColor: Colors.purple,
index: _page,
items: bottomNavigationBarItems,
onTap: (index) {
_pageController.animateToPage(
index,
duration: const Duration(milliseconds: 400),
curve: Curves.ease
);
},
height: 60,
//animationDuration: Duration(milliseconds: 400),
color: colorsNavigation[_page],
//color: Colors.purple,
backgroundColor: Colors.transparent,
//key: _bottomNavigationKey,
),
),
I am trying to put a CircleAvatar inside the AppBar in the actions list, but the CircleAvatar will stick its height to that of the AppBar, making it impossible to resize it and to keep it circular. I already tried wrapping it inside a Container or SizedBox, but it didn't work.
Example:
import 'package:flutter/material.dart';
class HoursScreenEmployee extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white10,
brightness: Brightness.light,
elevation: 0,
centerTitle: false,
actions: [
CircleAvatar(
backgroundImage: NetworkImage("https://picsum.photos/500/300"),
maxRadius: 15,
minRadius: 15,
),
],
title: Text(
"La Chance 🍻",
style: TextStyle(
fontFamily: "Masiva",
fontSize: 27,
fontWeight: FontWeight.w800,
color: Colors.black,
),
),
),
);
}
}
You can wrap the CircleAvatar in a Row:
actions: [
Row(
children: <Widget>[
Container(
height: 60,
width: 60,
child: CircleAvatar(
backgroundImage:
NetworkImage("https://picsum.photos/500/300"),
maxRadius: 15,
minRadius: 15,
),
),
],
),
],
Result: