How to customize Dropdown Button and items in flutter? - flutter

Today I tried to design a dropdown button with customized items in it where I can select all items or deselect all items with one click. But I didn't understand the approach how to do it. So please help me guys how to approach the required design and below I placed my design and required design.
and here is my 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(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String dropdownvalue = 'Apple';
var items = ['Apple','Banana','Grapes','Orange','watermelon','Pineapple'];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("DropDownList Example"),
),
body: Container(
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("DropDownButton"),
Container(
height: 40,
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
border: Border.all(
color: Colors.grey, style: BorderStyle.solid, width: 0.80),
),
child: DropdownButtonHideUnderline(
child: DropdownButton(
elevation: 0,
value: dropdownvalue,
icon: Icon(Icons.keyboard_arrow_down),
items:items.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(items)
);
}
).toList(),
onChanged: (String? newValue){
setState(() {
dropdownvalue = newValue!;
});
},
),
),
),
],
),
],
),
),
);
}
}

You can use dropdown_button2 package for this:
import 'package:flutter/material.dart';
import 'package:dropdown_button2/dropdown_button2.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(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String dropdownvalue = 'Apple';
var items = [
'Apple',
'Banana',
'Grapes',
'Orange',
'watermelon',
'Pineapple'
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("DropDownList Example"),
),
body: Container(
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("DropDownButton"),
Container(
height: 40,
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
border: Border.all(color: Colors.grey, style: BorderStyle.solid, width: 0.80),
),
child: DropdownButtonHideUnderline(
child: DropdownButton2(
hint: Text(
'Select Item',
style: TextStyle(
fontSize: 14,
color: Theme.of(context).hintColor,
),
),
items: items
.map((item) => DropdownMenuItem<String>(
value: item,
child: Text(
item,
style: const TextStyle(
fontSize: 14,
),
),
))
.toList(),
value: dropdownvalue,
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
},
buttonHeight: 40,
buttonWidth: 140,
itemHeight: 40,
),
)),
],
),
],
),
),
);
}
}
final result:

Related

How to make dropdown list with images and selected item also show with image in flutter?

Tell me how can I make a dropdown list but so that the elements are in the form of cards with an image? It is also necessary that the selected element be displayed without an arrow and with an image.
The following code would do the trick. The DropdownButton accepts DropdownMenuItem as items. And each menu item is customizable with a widget. Just layout an image and the texts and here you go:
Check also the live demo on DartPad
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int? _value = 0;
void _setValue(int? value) {
setState(() {
_value = value;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color.fromRGBO(50, 47, 61, 1),
body: Center(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: const Color.fromRGBO(57, 56, 69, 1),
borderRadius: BorderRadius.circular(12),
boxShadow: const [
BoxShadow(
color: Colors.black26,
spreadRadius: 5,
blurRadius: 12,
offset: Offset(0, 0))
]),
child: DropdownButton(
itemHeight: 124,
icon: const SizedBox.shrink(),
value: _value,
underline: Container(),
dropdownColor: const Color.fromRGBO(57, 56, 69, 1),
onChanged: _setValue,
borderRadius: BorderRadius.circular(12),
items: [
for (var i = 0; i < 10; i++)
DropdownMenuItem(
value: i,
child: Row(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image(
image: NetworkImage(
'https://source.unsplash.com/random/100x100?sig=$i&cars')),
),
const SizedBox(width: 8),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Test title $i',
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
Text(
'Test $i',
style: const TextStyle(color: Colors.white),
),
],
),
],
))
],
),
),
),
);
}
}
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
#override
_TestState createState() => new _TestState();
}
class _TestState extends State<Test> {
var _img = new Image.network("https://upload.wikimedia.org/wikipedia/commons/thumb/8/89/TUCPamplona10.svg/500px-TUCPamplona10.svg.png");
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text("Test Drop"),),
body: new Center(
child: new Container (
height: 50.0,
child:new DropdownButton(
items: new List.generate(10, (int index){
return new DropdownMenuItem(child: new Container(
padding: const EdgeInsets.only(bottom: 5.0),
height: 100.0,
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
_img,
new Text("Under 10")
],
),
));
})
, onChanged: null),),
),
);
}
}
this gif will give you better idea Demo of Code

How to get titled container in flutter

I want the container to have title over it like in this picture -
I am using stack and positioned to get the same, but instead I am getting this -
Here is my code -
Expanded(
child: Stack(
children: [
Container(
........... //Some Code
),
Positioned(
left: 0,
top: 0,
child: Text("One"),
),
],0
)
),
If I try to position it with top: -5 or -10, this is what I get -
Is there any widget for this? If no, then I think padding is the only option left with me. What should I do?
There is a package flutter_titled_container 1.0.7.
import 'package:flutter/material.dart';
import 'package:flutter_titled_container/flutter_titled_container.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
centerTitle: true,
title: Text('Titled Container'),
),
body: Center(
child: TitledContainer(
titleColor: Colors.blue,
title: 'Container Title',
textAlign: TextAlignTitledContainer.Center,
fontSize: 16.0,
backgroundColor: Colors.white,
child: Container(
width: 250.0,
height: 200.0,
decoration: BoxDecoration(
border: Border.all(
color: Colors.blue,
),
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
),
child: Center(
child: Text(
'Some text',
style: TextStyle(fontSize: 28.0),
),
),
),
),
),
);
}
}
More details on here.
Try below code hope its help to you.
InputDecorator(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Academic Year',
),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: dropdownValue,
isDense: true,
isExpanded: true,
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['2021-2022', '2022-2023', '2023-2024', '2024-2025']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
),
Result screen->
full example:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
#override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const Center(
child: MyStatefulWidget(),
),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
#override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
String dropdownValue = '2021-2022';
#override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(10),
child: InputDecorator(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Academic Year',
),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: dropdownValue,
isDense: true,
isExpanded: true,
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['2021-2022', '2022-2023', '2023-2024', '2024-2025']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
),
);
}
}
You can use InputDecorator
example:
InputDecorator(
child: Text("TEST"),
decoration: InputDecoration(
labelText: "lalala",
),
)
result:
Try to add margin.top to container insteads of negative Positioned.top. With the title, using Positioned.top/left/right to determine size and Align to align it to topleft, using Container with color.white to remove border line.
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Column(
children: [
TitledContainer(
titleText: 'Hello world!',
child: Text('Your content place here! Bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla'),
),
],
),
),
);
}
}
class TitledContainer extends StatelessWidget {
const TitledContainer({required this.titleText, required this.child, this.idden = 8, Key? key}) : super(key: key);
final String titleText;
final double idden;
final Widget child;
#override
Widget build(BuildContext context) {
return Stack(
children: [
Container(
margin: const EdgeInsets.only(top: 8),
padding: EdgeInsets.all(idden),
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(idden * 0.6),
),
child: child,
),
Positioned(
left: 10,
right: 10,
top: 0,
child: Align(
alignment: Alignment.topLeft,
child: Container(
color: Colors.white,
child: Text(titleText, overflow: TextOverflow.ellipsis),
),
),
),
],
);
}
}
Try set margin for Container
Expanded(
child: Stack(
children: [
Container(
margin: EdgeInsets.all(5), //add this code
........... //Some Code
),
Positioned(
left: 0,
top: 0,
child: Text("One"),
),
],0
)
),

Trying to create a method to control font size. Flutter App

I'm trying to create a font size control, the idea is that the user can change the font size of the entire app through the Slider, drag this bar and adjust it like 14px, 16px, 18px, 20px... minimum and maximum. I also read that the best way to make the changes on several screens will be using the provider, what is your opinion on this choice?
This is the starting code.
class Settings extends StatefulWidget {
const Settings({Key? key}) : super(key: key);
#override
State<Settings> createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
double _rating = 20;
#override
void initState() {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
iconTheme: IconThemeData(color: Colors.blue[900]),
title: const Text(
'Settings',
style: TextStyle(
color: Colors.black,
),
),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
title: Text('Button'),
trailing: Icon(
Icons.arrow_forward_ios,
color: Colors.blue,
),
onTap: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
color: Colors.white,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Change font',
style: TextStyle(
),
),
),
Slider(
value: _rating,
min: 0,
max: 28,
divisions: 4,
label: _rating.round().toString(),
onChanged: (newRating) {
setState(() => _rating = newRating);
},
),
],
),
),
);
}
);
},
),
],
),
);
}
}
I have created a provider example it might help you
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(MultiProvider(providers: [
ChangeNotifierProvider(create: (_) => SliderValue()),
], child: 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: SizableText(),
);
}
}
class SliderValue with ChangeNotifier {
double _value = 5;
double get value => _value;
void increment(double val) {
_value = val;
notifyListeners();
}
}
class SizableText extends StatefulWidget {
const SizableText({Key? key}) : super(key: key);
#override
State<SizableText> createState() => _SizableTextState();
}
class _SizableTextState extends State<SizableText> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("AppBar")),
body: Center(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 10),
decoration: new BoxDecoration(
color: Colors.blue,
borderRadius: new BorderRadius.all(new Radius.circular(5.0)),
boxShadow: [
new BoxShadow(
color: Colors.black38,
offset: new Offset(0.0, 2.0),
blurRadius: 10)
]),
child: new Slider(
value: context.watch<SliderValue>().value,
activeColor: Colors.white,
inactiveColor: Colors.white,
onChanged: (double s) {
context.read<SliderValue>().increment(s);
},
divisions: 10,
min: 0.0,
max: 10.0,
),
),
Text1(text: 'Hello'),
Text1(text: 'Hi'),
],
),
),
);
}
}
class Text1 extends StatelessWidget {
Text1({this.text});
final String? text;
#override
Widget build(BuildContext context) {
return Text(text ?? '',
style: TextStyle(fontSize: 10 * context.watch<SliderValue>().value));
}
}
Basic idea is stored fonsize value in somewhere that Text can reach, state management will update the value of fonsize and notify to theres subscription. Im not using provider so im use an other state management is get to do this.
// ignore_for_file: prefer_const_constructors_in_immutables
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class HomeController extends GetxController {
var fontSizeObx = RxDouble(12);
setFontsize(double value) => fontSizeObx.value = value;
}
class HomeRoute extends StatelessWidget {
HomeRoute({Key? key}) : super(key: key);
final controller = Get.put(HomeController());
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Obx(
() => Column(
children: [
Text(
'Hello world',
style: TextStyle(fontSize: controller.fontSizeObx.value),
),
Slider(
value: controller.fontSizeObx.value,
onChanged: controller.setFontsize,
divisions: 10,
min: 10.0,
max: 100.0,
)
],
),
),
),
);
}
}
You can try this
double _value = 5;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("AppBar")),
body: Center(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 10),
decoration: new BoxDecoration(
color: Colors.blue,
borderRadius: new BorderRadius.all(new Radius.circular(5.0)),
boxShadow: [new BoxShadow(color: Colors.black38,
offset: new Offset(0.0, 2.0), blurRadius: 10)]),
child: new Slider(
value: _value,
activeColor: Colors.white,
inactiveColor: Colors.white,
onChanged: (double s) {
setState(() {
_value = s;
});
},
divisions: 10,
min: 0.0,
max: 10.0,
),
),
Text("Hello World", style: TextStyle(fontSize: 10 * _value)),
],
),
),
);
}

I am trying to change the color of a container onTap but for some reason it isnt working

This is the code that I used. I am trying to implement a UI where a quiztaker can see highlighted the option that they selected.
I am new to this please tell me where I went wrong?
#override
Widget build(BuildContext context) {
int selectedOption = 0;
return Scaffold(
key: scaffoldKey,
body: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Card(
clipBehavior: Clip.antiAliasWithSaveLayer,
color: Color(0xFFF5F5F5),
// onTap is here
child: InkWell(
onTap: () { setState(() {
selectedOption = 1;
}
);
},
child: Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: selectedOption == 1 ? Colors.black: Colors.cyan,
),
child: Text(
'Hello World',
style: TextStyle(),
),
),
),
),
],
),
),
);
}
}
You need to move 'selectedOption' variable to outside of 'build' method
because when call 'setState', build is called and variable is reset.
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 selectedOption = 0;
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Card(
clipBehavior: Clip.antiAliasWithSaveLayer,
color: Color(0xFFF5F5F5),
// onTap is here
child: InkWell(
onTap: () {
setState(() {
selectedOption = 1;
});
},
child: Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: selectedOption == 1 ? Colors.black : Colors.cyan,
),
child: Text(
'Hello World',
style: TextStyle(),
),
),
),
),
],
),
),
);
}
Widget _buildBody() {
return Container();
}
}
With the current code Container color is updating only once. You need to change your code as follows to get the required output
return Scaffold(
key: scaffoldKey,
body: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Card(
clipBehavior: Clip.antiAliasWithSaveLayer,
color: Color(0xFFF5F5F5),
// onTap is here
child: InkWell(
onTap: () {
setState(() {
_isSelected = !_isSelected;
});
},
child: Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: _isSelected ? Colors.black : Colors.cyan,
),
child: Text(
'Hello World',
style: TextStyle(),
),
),
),
),
],
),
),
);
Instead of using the int variable you should use a boolean and update its state when tapped.
Please move selectedOption to state class. then setstate() will work.

Layout issue: TabBarView with TextField Keyboard overflow

I'm trying to implement a specific design and as thhe title says I have encountered an issue in building my layout.
When I'm taping inside my TextField the keyboard open itself and create an overflow.
Screenshots
Code
deals_edition_page.dart
import 'package:flutter/material.dart';
import 'package:myuca/ui/manager/deals/edition_informations_tab.dart';
class DealsEditionPage extends StatefulWidget {
#override
State<StatefulWidget> createState() => _DealsEditionPageState();
}
class _DealsEditionPageState extends State<DealsEditionPage> {
MemoryImage _image;
Widget _buildAppbar() {
return AppBar(
elevation: 0,
leading: IconButton(
icon: Icon(Icons.close),
onPressed: () => Navigator.pop(context),
),
title: Text(/* random String */),
actions: [
IconButton(
icon: Icon(Icons.check),
onPressed: () => Navigator.pop(context),
),
],
);
}
Widget _buildHeaderTabBar() {
return SliverAppBar(
automaticallyImplyLeading: false,
expandedHeight: 224,
floating: true,
elevation: 0,
flexibleSpace:
FlexibleSpaceBar(background: Image.memory(_image.bytes, fit: BoxFit.cover)),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: _buildAppbar(),
body: DefaultTabController(
length: 2,
child: CustomScrollView(
slivers: <Widget>[
_buildHeaderTabBar(),
SliverToBoxAdapter(
child: TabBar(
indicatorWeight: 2,
tabs: [
Tab(text: 'Informations'),
Tab(text: 'Informations'),
],
),
),
SliverFillRemaining(
child: Column(
children: [
Expanded(
child: TabBarView(
children: [
EditionInformationsTab(),
Center(child: Text("Tab 2")),
],
),
),
],
),
),
],
),
),
);
}
}
edition_informations_tab.dart
import 'package:flutter/material.dart';
import 'package:myuca/extensions/extensions.dart' show WidgetModifier;
class EditionInformationsTab extends StatefulWidget {
#override
State<StatefulWidget> createState() => _EditionInformationsTabState();
}
class _EditionInformationsTabState extends State<EditionInformationsTab> {
final _list1 = <String>['Culture', 'Test'];
final _list2 = <String>['Etablissement', 'Test 1', 'Test 2'];
List<DropdownMenuItem<String>> _menuItemsGenerator(List<String> values) =>
values
.map<DropdownMenuItem<String>>((e) => DropdownMenuItem<String>(
value: e,
child: Text(e),
))
.toList();
#override
Widget build(BuildContext context) {
return Form(
child: Column(
children: [
DropdownButtonFormField<String>(
value: _list1.first,
decoration: InputDecoration(
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(4))),
items: _menuItemsGenerator(_list1),
onChanged: (_) {},
).padding(EdgeInsets.only(bottom: 24)),
DropdownButtonFormField(
value: _list2.first,
decoration: InputDecoration(
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(4))),
items: _menuItemsGenerator(_list2),
onChanged: (_) {},
).padding(EdgeInsets.only(bottom: 24)),
TextFormField(
maxLength: 30,
decoration: InputDecoration(
labelText: "Nom de l'offre",
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(4))),
),
],
),
).padding(EdgeInsets.only(
top: 16, left: 16, right: 16));
}
}
Any idea on how I could avoid this overflow when the keyboard is displayed ?
You can copy paste run full code below
You can in _EditionInformationsTabState wrap with SingleChildScrollView
class _EditionInformationsTabState extends State<EditionInformationsTab> {
...
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
working demo
full code
import 'package:flutter/material.dart';
class DealsEditionPage extends StatefulWidget {
#override
State<StatefulWidget> createState() => _DealsEditionPageState();
}
class _DealsEditionPageState extends State<DealsEditionPage> {
MemoryImage _image;
Widget _buildAppbar() {
return AppBar(
elevation: 0,
leading: IconButton(
icon: Icon(Icons.close),
onPressed: () => Navigator.pop(context),
),
title: Text("demo"),
actions: [
IconButton(
icon: Icon(Icons.check),
onPressed: () => Navigator.pop(context),
),
],
);
}
Widget _buildHeaderTabBar() {
return SliverAppBar(
automaticallyImplyLeading: false,
expandedHeight: 224,
floating: true,
elevation: 0,
flexibleSpace: FlexibleSpaceBar(
background: Image.network("https://picsum.photos/250?image=9",
fit: BoxFit.cover)),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: _buildAppbar(),
body: DefaultTabController(
length: 2,
child: CustomScrollView(
slivers: <Widget>[
_buildHeaderTabBar(),
SliverToBoxAdapter(
child: TabBar(
indicatorWeight: 2,
tabs: [
Tab(text: 'Informations'),
Tab(text: 'Informations'),
],
),
),
SliverFillRemaining(
child: Column(
children: [
Expanded(
child: TabBarView(
children: [
EditionInformationsTab(),
Center(child: Text("Tab 2")),
],
),
),
],
),
),
],
),
),
);
}
}
class EditionInformationsTab extends StatefulWidget {
#override
State<StatefulWidget> createState() => _EditionInformationsTabState();
}
class _EditionInformationsTabState extends State<EditionInformationsTab> {
final _list1 = <String>['Culture', 'Test'];
final _list2 = <String>['Etablissement', 'Test 1', 'Test 2'];
List<DropdownMenuItem<String>> _menuItemsGenerator(List<String> values) =>
values
.map<DropdownMenuItem<String>>((e) => DropdownMenuItem<String>(
value: e,
child: Text(e),
))
.toList();
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Padding(
padding: EdgeInsets.only(top: 16, left: 16, right: 16),
child: Form(
child: Column(
children: [
Padding(
padding: EdgeInsets.only(bottom: 24),
child: DropdownButtonFormField<String>(
value: _list1.first,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(4))),
items: _menuItemsGenerator(_list1),
onChanged: (_) {},
),
),
Padding(
padding: EdgeInsets.only(bottom: 24),
child: DropdownButtonFormField(
value: _list2.first,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(4))),
items: _menuItemsGenerator(_list2),
onChanged: (_) {},
),
),
TextFormField(
maxLength: 30,
decoration: InputDecoration(
labelText: "Nom de l'offre",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(4))),
),
],
),
),
),
);
}
}
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: DealsEditionPage(),
);
}
}
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>[
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),
),
);
}
}