I have an app bar in my application:
SmallTopAppBar(
title = { Text("✨StarDict✨") },
actions = {
IconButton(
onClick = {...}
) {
Icon(
painterResource(R.drawable.ic_dict),
tint = Color.White,
contentDescription = "",
)
}
IconButton(
onClick = {...}
) {
Icon(
Icons.Default.Settings,
tint = Color.White,
contentDescription = "",
)
}
IconButton(
onClick = {...}
) {
Icon(
Icons.Default.Info,
tint = Color.White,
contentDescription = "",
)
}
},
colors = colors,
)
However, the action buttons don't have a ripple effect. All other buttons do, but not the action buttons in the app bar.
Is there a way to add the ripple effect?
Thank you!
Related
i have an issue with page view, it's working fine for first initialization (i mean when open the screen that contain page view from "Screen A" then go to open the "pageView screen" from other screen called "Screen B") in second time getting this issue
i think this issue happen because the controller re-intialiazed again, but i dispose the controller and make it equal to null when dispose screen
ScrollController attached to multiple scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
package:flutter/…/widgets/scroll_controller.dart:1
Failed assertion: line 109 pos 12: '_positions.length == 1'
Using provider to handle change in tabBar and Page View, the below code of provider class
class PageViewProvider extends BaseViewModel {
// Tabs Controller
int selectedTapIndex = 0;
PageController? pageViewController;
initPageIndex(int index) {
if (pageViewController != null) {
pageViewController = null;
}
print('init index $index');
selectedTapIndex = index;
pageViewController = PageController(initialPage: index, keepPage: false);
}
void changeSelectedTapIndex(int index) {
selectedTapIndex = index;
setState(ViewState.Idle);
}
void changePageView(int index) {
pageViewController!.jumpToPage(index);
}
}
this the tabed item widget with onTap Function to change index of TabBar
Consumer<PageViewProvider>(
builder: (context, pageViewProvider, _) => Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: Column(
children: [
Icon(
iconData,
color:
pageViewProvider.selectedTapIndex == selectedIndex
? purpleColor
: grey500,
),
normal14Text(
title,
color:
pageViewProvider.selectedTapIndex == selectedIndex
? purpleColor
: grey500,
),
],
),
).onTap(() {
pageViewProvider.changeSelectedTapIndex(selectedIndex!);
pageViewProvider.changePageView(selectedIndex!);
}));
at least, this is the pageView body
Consumer<PageViewProvider>(
builder: (context, pageViewProvider, _) => PageView(
controller: pageViewProvider.pageViewController,
onPageChanged: (index) {
pageViewProvider.changeSelectedTapIndex(index);
},
pageSnapping: true,
children: [
LeadProfileContent(),
LeadProfileTimeLine(),
LeadProfileCases(),
LeadProfileDeals(),
],
),
),
Thanks in advance.
It's solved. The issue here was with the Consumer above the page view body. Just removed it, and then accessed the pageViewController like this:
context.read<PageViewProvider>.pageViewController
I tried establishing a default background color for all my headers, but it is not working.
I am using defaultNavigationOptions and it is not working.
const RootStack = createStackNavigator(
{
Home: HomeScreen,
Details: DetailsScreen
},
{
initialRouteName: "Home",
defaultNavigationOptions: {
headerStyle: {
backgroundColor: "#6B52AE"
},
headerTintColor: "#fff",
headerTitleStyle: {
fontWeight: "bold"
}
}
}
);
I am expecting a purple background, but when I reload the ios simulator, I get a white header instead.
You can define the tabBarOptions:
tabBarOptions: {
activeTintColor: "#480ee6", //Active label
labelStyle: {
fontSize: 12
},
style: { backgroundColor: "#6B52AE" } //TabBar
}
I am currently using flutter_search_bar plugin with a Tabbed Pages. Since my values change dynamically. I was wondering if there were any ways to add a listener to the package in order to make the necessary changes.
The plugin is here:
https://pub.dartlang.org/flutter/packages?q=flutter_search_bar&api=0
Update: this is what my code looks like now
_NavigationState() {
searchBar = new SearchBar(
controller: controller,
inBar: false,
setState: setState,
onSubmitted: onSubmit,
onChanged: onChange,
buildDefaultAppBar: buildAppBar,
hintText: 'Search Book',
closeOnSubmit: false,
clearOnSubmit: true,
onClosed: onClosed,
);
}
void onClosed() {
isSeaching = false;
loadTabpages();
setState(() {});
}
You can fork the repo and make the changes :
https://github.com/ArcticZeroo/flutter-search-bar/blob/master/lib/src/flutter_search_bar_base.dart
Add this var in SearchBar class:
/// Event triggered when the user close the search bar
final VoidCallback onClosed;
Add the field into the constructor
SearchBar(
{#required this.setState,
#required this.buildDefaultAppBar,
this.onSubmitted,
this.controller,
this.hintText = 'Search',
this.inBar = true,
this.colorBackButton = true,
this.closeOnSubmit = true,
this.clearOnSubmit = true,
this.showClearButton = true,
this.onChanged,
this.onClosed})
Line 144
Set a widget into the leading property
new IconButton(
icon: const BackButtonIcon(),
color: buttonColor,
tooltip: MaterialLocalizations.of(context).backButtonTooltip,
onPressed: () {
if (onClosed != null) {
onClosed();
}
Navigator.maybePop(context);
}),
Using the listener:
new SearchBar(
inBar: true,
setState: setState,
showClearButton: false,
clearOnSubmit: false,
closeOnSubmit: false,
onClosed: () {
}), ...
Updated handling the back button
return new AppBar(
leading: WillPopScope(
onWillPop: () {
if (onClosed != null) {
onClosed();
}
return Future.value(true);
},
child: new IconButton(
icon: const BackButtonIcon(),
color: buttonColor,
tooltip: MaterialLocalizations.of(context).backButtonTooltip,
onPressed: () {
if (onClosed != null) {
onClosed();
}
controller.clear();
Navigator.maybePop(context);
}),
),
....
Is there any way to change the floating button's icon when pressed?
Code:
var musicButton = new FloatingActionButton(
onPressed: (){
if(playerState == PlayerState.playing) {
//setState(() => child = new Icon(Icons.pause));
setState((){
child: new Icon(Icons.play_arrow);
});
pause();
} else if (playerState == PlayerState.paused){
setState((){
child: new Icon(Icons.pause);
});
play();
}
},
tooltip: 'Play Music',
child: new Icon(Icons.pause));
You can just use a state variable to switch between icons.
Example:
bool playing = false;
then
var musicButton = new FloatingActionButton(
onPressed: (){
if(playerState == PlayerState.playing) {
setState((){
playing = false;
});
pause();
} else if (playerState == PlayerState.paused){
setState((){
playing = true;
});
play();
}
},
tooltip: 'Play Music',
child: playing?new Icon(Icons.pause):new Icon(Icons.play_arrow));
Hope that helped!
You can use enumeration also enum used to define named constant values.
enum Gender {Male,Female,} // enum definition globally
Gender selectedGender; // enum declaration
onPress: () {
setState(() {
selectedGender = Gender.Male;
});
},
Now you can use selectedGender Dart Conditional Operators ( ? : ) anywhere in your code.
I'm using react-navigation with StackNavigator. Is there a way to avoid the overlapping of the back button label and the headerTitle by truncating the back button label?
const MainNavigationOptions = {
headerStyle: {
backgroundColor: colors.CiPrimary
},
headerTitleStyle: {
color: 'white',
height: 50,
width: 140
},
headerTintColor: 'white',
headerTitle:
<Text>LONG TEXT FOR TESTING</Text>
}
Illustration of issue:
(This Answer takes into account that the viewer is using react-navigation 5.x)
In Your Screen component
export const screenOptions = (navData) => {
let title = navData.route.params.movieTitle;
if (title.length > 18) {
title = title.substr(0, 18) + "...";
}
return {
headerTitle: title,
};
};
Here, since we are using substr() , you can use it accordingly and truncate it to custom match your case, keeping in mind the edge cases.
Then you can import it in your AppNavigator.js or wherever you initialize your navigator (in my case below ;)
import {screenOptions as MoviesDetailScreenOptions} from
"../screens/MovieDetailScreen";
Here screenOptions is the named-export you are using MoviesDetailScreenOptions is the alias if i am not mistaken.
Create a style for your button label because it now left for you to style it your self and you can style it any way you want.
const styles = {
leftTouch: {
flexDirection: 'row',
...
},
customStyle: {
paddingLeft: 10,
...
}
textStyle: {
width: 60,
fontSize: 14,
......
}
}
const { leftTouch, customStyle, textStyle} = styles;
Instead of Icon you can use <Image /> but am assuming you are using an icon i.e react-native-vector-icon or likes.
N.B Now you have control of everything the button should do especially when pressed.
const MainNavigationOptions = {
headerStyle: {
backgroundColor: colors.CiPrimary
},
headerTitleStyle: {
color: 'white',
height: 50,
width: 140
},
.....
headerLeft: ( <TouchableOpacity style={leftTouch} onPress={() => goBack()} >
<Icon name="ios-arrow-dropleft-circle-outline" size={25} style={customStyle} color="#ffffff" />
<Text numberOfLines={1} style={textStyle}>A Longer Text for testing</Text>
</TouchableOpacity>
)
}
If you mean to shorten the back button label, why not use either:
headerBackTitle (change the Back label for the previous screen, put it in navigationOptions of the previous screen);
headerBackTitleStyle (change the Back label displayed on current screen, put it in navigationOptions of the current screen)