In flutter, can you set the appbar backgorund to change base on the value of a dropdown box? - flutter

my drop down box cycles through 5 strings
['blue','red','yellow','orange','grey']
I want my appbar title to be that dropdown box and for the value in the dropdown to determine the appbar color
DropDownWidget ddw = DropDownWidget();
var color = {
"blue": Colors.blue,
"red": Colors.red,
"yellow": Colors.yellow,
"orange": Colors.orange,
"grey": Colors.grey,
};
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: ddw,
backgroundColor: color[ddw],
),
}
The dropdown (ddw) shows up as the title, no problem.
I made a dictionary with those strings as the keys and the corresponding color as the value, but I am not able to use the string value of the dropdown to change the background.
Any suggestions?

You can copy paste run full code below
You can call setState in onChanged of DropdownButton
code snippet
appBar: AppBar(
backgroundColor: _appbarColor,
...
DropdownButton<Item>(
hint: Text("Select item"),
value: selectedColor,
onChanged: (Item Value) {
setState(() {
selectedColor = Value;
_appbarColor = selectedColor.color;
});
},
working demo
full code
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class Item {
const Item(this.name, this.color);
final String name;
final Color color;
}
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;
Color _appbarColor = Colors.blue;
Item selectedColor;
List<Item> colorList = <Item>[
const Item('blue', Colors.blue),
const Item('red', Colors.red),
const Item('yellow', Colors.yellow),
];
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: _appbarColor,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButton<Item>(
hint: Text("Select item"),
value: selectedColor,
onChanged: (Item Value) {
setState(() {
selectedColor = Value;
_appbarColor = selectedColor.color;
});
},
items: colorList.map((Item item) {
return DropdownMenuItem<Item>(
value: item,
child: Row(
children: <Widget>[
Container(
height: 15,
width: 15,
color: item.color,
),
SizedBox(
width: 10,
),
Text(
item.name,
style: TextStyle(color: Colors.black),
),
],
),
);
}).toList()),
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),
),
);
}
}

Related

Flutter: Dismissible in PopupMenuButton does not risize while displayed

I want to use a Dismissible Widget in a PopupMenuButton to delete items in the popup list.
My example code (see the example main() code below) works for deleting an item (String) in the PopupMenuButton, but the list is not refreshed and resized letting a blank space in the list while the popup is still displayed.
The list is only displayed resized after the popup is closed and then reopened.
I remove the item from the list in a setState in the onDismissed callback so the widget tree should be rebuild.
How can I update and resize the displayed list in the popup without having to close and reopen it?
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter PopupMenuButton sample',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String dropdownValue = 'Orange';
final _items = [
'Orange',
'Banana',
'Grapes',
'Apple',
'watermelon',
'Pineapple'
];
#override
Widget build(BuildContext context) {
print("build Scaffold");
return Scaffold(
appBar: AppBar(
title: Text("DropDownList Example"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("$dropdownValue"),
const Spacer(),
PopupMenuButton<String>(
icon: Icon(Icons.bookmarks_outlined),
onSelected: (String value) {
setState(() {
dropdownValue = value;
});
print(value);
},
itemBuilder: (BuildContext context) {
return _items.map<PopupMenuItem<String>>((String _value) {
return PopupMenuItem(
value: _value,
child: Dismissible(
key: UniqueKey(),
direction: DismissDirection.endToStart,
onDismissed: (i) {
print("onDismissed $_value");
setState(() {
_items.remove(_value);
});
},
child: ListTile(
title: Text(_value),
trailing: const Icon(Icons.arrow_back),
),
background: Container(
color: Colors.red,
margin: const EdgeInsets.symmetric(horizontal: 15),
alignment: Alignment.centerRight,
child: const Icon(
Icons.delete,
color: Colors.white,
),
),
),
);
}).toList();
},
),
const Spacer(
flex: 10,
),
],
),
),
);
}
}

I wanted to add search bar in my app, but I got some error messages as below

lib/main.dart:143:13: Error: Field 'searchBar' should be initialized because its type 'SearchBar' doesn't allow null.
'SearchBar' is from 'package:flutter_search_bar/src/flutter_search_bar_base.dart' ('../Documents/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_search_bar-2.1.0/lib/src/flutter_search_bar_base.dart').
SearchBar searchBar;
^^^^^^^^^
Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:
package:flutter_search_bar
So I ran dart pub upgrade --null-safety in terminal.
Error: Cannot open file, path = 'C:\Users\Miche\Documents\flutter\version' (OS Error: The system cannot find the file specified.
, errno = 2)
Code: /// Flutter code sample for BottomNavigationBar
// This example shows a [BottomNavigationBar] as it is used within a [Scaffold]
// widget. The [BottomNavigationBar] has three [BottomNavigationBarItem]
// widgets, which means it defaults to [BottomNavigationBarType.fixed], and
// the [currentIndex] is set to index 0. The selected item is
// amber. The `_onItemTapped` function changes the selected item's index
// and displays a corresponding message in the center of the [Scaffold].
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_search_bar/flutter_search_bar.dart';
void main() => runApp(const MyApp()); // This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
#override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
#override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
static const List<Widget> _widgetOptions = <Widget>[
Text(
'Reuse',
style: TextStyle(fontSize: 20),
),
Text(
"Reduce",
style: TextStyle(fontSize: 20),
),
Text(
'Recycle',
style: TextStyle(fontSize: 20),
)
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Food3Rs'),
backgroundColor: Colors.green,
),
drawer: Drawer(
child: ListView(padding: EdgeInsets.zero, children: const <Widget>[
DrawerHeader(
decoration: BoxDecoration(color: Colors.green),
child: Text(
'Food Reduction',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
ListTile(
leading: Icon(Icons.bookmark),
title: Text('Saved Recipes'),
),
ListTile(
leading: Icon(Icons.account_circle),
title: Text('Profile'),
),
ListTile(leading: Icon(Icons.settings), title: Text('Settings'))
]),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.food_bank_outlined),
label: ('Reduce'),
backgroundColor: (Colors.lightGreen),
),
BottomNavigationBarItem(
icon: Icon(Icons.food_bank),
label: ('Reuse'),
),
BottomNavigationBarItem(
icon: Icon(Icons.cloud),
label: ('Recycle'),
backgroundColor: (Colors.lightGreen),
)
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
class SearchBarDemoApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Search Bar Demo',
theme: new ThemeData(primarySwatch: Colors.blue),
home: new SearchBarDemoHome());
}
}
class SearchBarDemoHome extends StatefulWidget {
#override
_SearchBarDemoHomeState createState() => new _SearchBarDemoHomeState();
}
class _SearchBarDemoHomeState extends State<SearchBarDemoHome> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
SearchBar searchBar;
AppBar buildAppBar(BuildContext context) {
return new AppBar(
title: new Text('Search Bar Demo'),
actions: [searchBar.getSearchAction(context)]);
}
void onSubmitted(String value) {
setState(() {
var context = _scaffoldKey.currentContext;
if (context == null) {
return;
}
ScaffoldMessenger.maybeOf(context)
?.showSnackBar(new SnackBar(content: new Text('You wrote "$value"!')));
});
}
_SearchBarDemoHomeState() {
searchBar = new SearchBar(
inBar: false,
buildDefaultAppBar: buildAppBar,
setState: setState,
onSubmitted: onSubmitted,
onCleared: () {
print("Search bar has been cleared");
},
onClosed: () {
print("Search bar has been closed");
});
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: searchBar.build(context),
key: _scaffoldKey,
body: new Center(
child: new Text("Don't look at me! Press the search button!")),
);
}
}
Since you are definately setting the search bar to a non-null value, just not where it's required for the compiler to recognize it, you can use the late keyword:
late SearchBar searchBar;
See the documentation for the late keyword for more information.
The stable version of flutter_search_bar don't support null safety.
Try changing to 3.0.0-dev.1`

how to change properties of list tile from its onTap() function in flutter?

leading: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
// There I want to change title, background color etc
},
),
title: Text('title'),
dense: false,
),
I want to change properties of list tile from its own onTap()
You can copy paste run full code below
You can use StatefulWidget and call setState inside onTap
code snippet
class _ListTileCustomState extends State<ListTileCustom> {
String _title = "title";
Color _color = Colors.blue;
#override
Widget build(BuildContext context) {
return ListTile(
tileColor: _color,
leading: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
setState(() {
_title = "changed";
_color = Colors.red;
});
},
child: Icon(Icons.add)),
title: Text(_title),
dense: false,
);
}
}
working demo
full code
import 'package:flutter/material.dart';
class ListTileCustom extends StatefulWidget {
#override
_ListTileCustomState createState() => _ListTileCustomState();
}
class _ListTileCustomState extends State<ListTileCustom> {
String _title = "title";
Color _color = Colors.blue;
#override
Widget build(BuildContext context) {
return ListTile(
tileColor: _color,
leading: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
setState(() {
_title = "changed";
_color = Colors.red;
});
},
child: Icon(Icons.add)),
title: Text(_title),
dense: false,
);
}
}
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: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ListTileCustom(),
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),
),
);
}
}
you can define one variable before build a method and use that variable to the title of listtile and when you ontap then change the state using setState and It will change the title of your listtie.
leading: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
setState((){
title="your text"
});
},
),
title: Text(title),
dense: false,
),
here title is variable
and ListTile also has property of onTap

Flutter DropdownButtonFormField setState does not work outside of onChanged method

I just created a simple flutter app with two DropdownButtonFormField widgets.
And I want to update both widgets if first one is selected.
Expectation:
select number from the first dropdown menu
same number appears in the second dropdown
Result:
- select number from the first dropdown menu
- only the first dropdown menu is updated
Can somebody please tell me what I am doing wrong when calling setState onChaged method of the first dropdown menu widget?
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 {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
List<int> myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
int currentNumber = 1;
int anotherNumber = 9;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
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,
),
DropdownButtonFormField<int>(
value: currentNumber,
items: myList.map((int value) {
return DropdownMenuItem<int>(
value: value,
child: Container(
width: 200,
child: Text(
value.toString(),
),
),
);
}).toList(),
onChanged: (int newValue) {
setState(() {
currentNumber = newValue;
anotherNumber = newValue;
});
},
),
DropdownButtonFormField<int>(
value: anotherNumber,
items: myList.map((int value) {
return DropdownMenuItem<int>(
value: value,
child: Container(
width: 200,
child: Text(
value.toString(),
),
),
);
}).toList(),
onChanged: (int newValue) {
setState(() {
anotherNumber = newValue;
});
},
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}}

How to create a custom AppBar widget?

I'm new to flutter. I'm trying to create a custom appbar widget and importing the widget in pages.
But I was unable to create the widget.
import 'package:flutter/material.dart';
class AppBar extends StatelessWidget{
#override
Widget build(BuildContext context){
return AppBar(
title: Text('Ordering'),
actions: <Widget>[
IconButton(
onPressed: _incrementCounter,
icon: Icon(Icons.add),
),
BadgeIconButton(
itemCount: _counter,
badgeColor: Color.fromRGBO(37, 134, 16, 1.0),
badgeTextColor: Colors.white,
icon: Icon(Icons.shopping_cart, size: 30.0,),
onPressed: () {}
),
],
);
}
}'
import 'package:flutter/material.dart';
class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {
CustomAppBar({Key key}) : preferredSize = Size.fromHeight(kToolbarHeight), super(key: key);
#override
final Size preferredSize; // default is 56.0
#override
_CustomAppBarState createState() => _CustomAppBarState();
}
class _CustomAppBarState extends State<CustomAppBar>{
#override
Widget build(BuildContext context) {
return AppBar( title: Text("Sample App Bar") );
}
}
Hopefully this helps
class AppBars extends AppBar {
AppBars():super(
iconTheme: IconThemeData(
color: Colors.black, //change your color here
),
backgroundColor: Colors.white,
title: Text(
"this is app bar",
style: TextStyle(color: Color(Constant.colorBlack)),
),
elevation: 0.0,
automaticallyImplyLeading: false,
actions: <Widget>[
IconButton(
icon: Icon(Icons.notifications),
onPressed: () => null,
),
IconButton(
icon: Icon(Icons.person),
onPressed: () => null,
),
],
);
}
Widget build(BuildContext context) {
return new Scaffold(
appBar: setAppBar(),
body: new Container() // add rest of the UI
);
}
Widget setAppBar() {
return new AppBar(
//backgroundColor: Colors.blue,
//automaticallyImplyLeading: true
elevation: 0.0, // for elevation
titleSpacing: 0.0, // if you want remove title spacing with back button
title: UtilCommonWidget.addTextMedium('About US', Colors.white, 20.0, 1),
actions: <Widget>[
addAppBarActionWidgetProfile(icon, 30.0, 30.0, 15.0) // add your custom action widget
],//Action icon search as search icon, notification icon
leading: new Material( //Custom leading icon, such as back icon or other icon
color: Colors.transparent,
child: new InkWell(
onTap: () {
Navigator.of(context).pop();
},
splashColor: UniQueryColors.colorGradientEnd.withOpacity(.5),
child: new Container(
padding: const EdgeInsets.fromLTRB(12.0, 16.0, 16.0, 16.0),
child: UtilCommonWidget.addImage(Constant.iconBack, 19.0, 10.0))
),
)
);
}
I extended AppBar with my custom widget.
Then passed my parameters to the super class.
class CustomAppBar extends AppBar {
CustomAppBar()
: super(
title: Text('MyApp'),
actions: [
IconButton(icon: Icon(Icons.search), onPressed: () {}),
],
);
}
Screenshot (Null Safe):
Full code:
Create this class.
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
final Widget child;
final double height;
CustomAppBar({
required this.child,
this.height = kToolbarHeight,
});
#override
Size get preferredSize => Size.fromHeight(height);
#override
Widget build(BuildContext context) {
return Container(
height: preferredSize.height,
color: Colors.red,
child: child,
);
}
}
Usage:
Scaffold(
appBar: CustomAppBar(
height: 100,
child: Column(
children: [
FlutterLogo(size: 56),
SizedBox(height: 8),
Text('Flutter'),
],
),
),
)
Edit to riftninja's answer :
import 'package:flutter/material.dart';
class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {
CustomAppBar({Key key, double height}) : preferredSize =
Size.fromHeight(height), super(key: key);
#override
//final Size preferredSize; // This didnot work for me.
Size get preferredSize => preferredSize; //This should work.
#override
_CustomAppBarState createState() => _CustomAppBarState();
}
class _CustomAppBarState extends State<CustomAppBar>{
#override
Widget build(BuildContext context) {
return AppBar( title: Text("Sample App Bar") );
}
}
This also works for stateless widget.
widget_appbar.dart
class WidgetAppBar extends StatelessWidget implements PreferredSizeWidget {
final Color? backgroundColor;
final Color? textIconColor;
final String? icon;
final String? title;
final double? height;
final List<Widget>? menuItem;
final bool hideBack;
WidgetAppBar({
this.backgroundColor = whiteColor,
this.textIconColor = headingColor,
this.icon,
this.title = '',
this.menuItem,
this.height: kToolbarHeight,
this.hideBack = false,
});
#override
Size get preferredSize => Size.fromHeight(height!);
#override
Widget build(BuildContext context) {
return AppBar(
actions: menuItem,
toolbarHeight: preferredSize.height,
iconTheme: IconThemeData(
color: textIconColor,
),
leading: hideBack
? Container()
: icon == null
? BackButton()
: IconButton(
icon: Image.asset(
icon!,
height: 18,
width: 18,
),
onPressed: () {
Navigator.pop(context, true);
},
),
title: Text(
title!,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: textIconColor,
),
),
backgroundColor: backgroundColor,
centerTitle: true,
);
}
}
How to use?
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: WidgetAppBar(
icon: ic_back_black,
title: 'Toolbar Title',
),
body: SafeArea(
child: Container(),
),
);
}