Using Form in Alertdialog - flutter

I want to create an object in body and use it in alertdialog, how should I do it? For example:
var form = Form(
);
********
return AlertDialog(
content: form,
),
The reason I want to do this is because when I try to use the buildForm structure outside the body it doesn't work. But I can't define it in the body because I want to use floating action button how can I solve it or is there a different way to solve it?
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
tooltip: "New ToDo",
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("New ToDo"),
content: Container(
child: Column(children: [
Form(
key: _formKey,
child: Column(children: [
buildForm(_controllerTitle, "Title")
],),
),
],),
),
actions: [
],
);
},
);
},
),

You can create a method buildForm that will take a TextEditingController and String
Widget buildForm(TextEditingController controller, String title) {
return Container(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(title), // or just use on input decoration
TextFormField(
controller: controller,
decoration: InputDecoration(),
),
],
),
);
}
Customize the way you want.
And can be use like
buildForm(_controllerTitle, "Title"),
class _EXTTEstState extends State<EXTTEst> {
Widget buildForm(TextEditingController controller, String title) {
return Container(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(title), // or just use on input decoration
TextFormField(
controller: controller,
decoration: InputDecoration(),
),
],
),
);
}
final _controllerTitle = TextEditingController();
#override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
tooltip: "New ToDo",
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("New ToDo"),
content: Container(
child: Column(
children: [
Form(
// key: _formKey,
child: Column(
children: [
buildForm(_controllerTitle, "Title"),
],
),
),
],
),
),
actions: [],
);
},
);
},
),
);
}
}

Related

How to open google maps by clicking on a button in flutter

I am new to flutter and I want to open google maps by clicking on button using flutter.
Here is my code and in that code on line no 98 I want to open google maps by clicking on share your location button so please give me a suggestion that which dependencies I should use to do so (google-maps-flutter or google-urls) and what changes I should make in my code.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'maps.dart';
class Intro extends StatefulWidget {
const Intro({Key? key}) : super(key: key);
#override
_IntroState createState() => _IntroState();
}
class _IntroState extends State<Intro> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('\t\t\t\t\t\t\t\t\t\tShree Food'),backgroundColor: Color(0xff01A0C7)),
body: Column(
children:[
Container(
child: Padding(
padding: const EdgeInsets.all(36.0),
child: Center(
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const ListTile(
title: Text('Goal'),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
TextButton(
child: const Text('>'),
onPressed: () {
showAlertDialog(context);
},
),
const SizedBox(width: 8),
],
),
],
),
),
),
),
),
Container(
child: Padding(
padding: const EdgeInsets.all(36.0),
child: Center(
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const ListTile(
title: Text('Vision'),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
TextButton(
child: const Text('>'),
onPressed: () {
showAlertDialog1(context);
},
),
const SizedBox(width: 8),
],
),
],
),
),
),
),
),
Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
GridView.count(
primary: false,
padding: EdgeInsets.all(50),
shrinkWrap: true,
crossAxisCount: 2,
children: [
Column(
children : [
IconButton(
icon: Image.asset(
'images/maps.png',
),
iconSize: 80,
onPressed: () {
**//I want to open google maps by clicking on this button**
},
),
Text('Share Your Location')
],
),
Column(
children : [
IconButton(
icon: Image.asset(
'images/donate.jpg',
),
iconSize: 80,
onPressed: () {
},
),
Text('Donate Zone')
],
),
],
),
],
),
)
]
)
);
}
}
showAlertDialog(BuildContext context) {
// Create button
Widget okButton = FlatButton(
child: Text("OK"),
onPressed: () {
Navigator.of(context).pop(MaterialPageRoute(builder: (context) => Intro()));
},
);
// Create AlertDialog
AlertDialog alert = AlertDialog(
title: Text("Simple Alert"),
content: Text("This is an alert message."),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
showAlertDialog1(BuildContext context) {
// Create button
Widget okButton = FlatButton(
child: Text("OK"),
onPressed: () {
Navigator.of(context).pop(MaterialPageRoute(builder: (context) => Intro()));
},
);
// Create AlertDialog
AlertDialog alert = AlertDialog(
title: Text("Simple Alert"),
content: Text("This is an alert message."),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
if I understood your question , you want to open google maps when pressing on a button , if this is the case you need to use this package :
map_launcher
give it a try

How to avoid bottom overflow when keyboard open (in alert dialog)

In this screen i'm facing overflow every time I open keyboard in the alert text field. I already tried to put SingleChildScrollVIew in all the places and it didn't resolve. This is the component which have the alertdialog:
Future<void> _showAlertDialog() async {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Type the Name', textAlign: TextAlign.center,
style: TextStyle(color: Colors.white)),
content: TextField(
controller: _nameController,
keyboardType: TextInputType.text,
textCapitalization: TextCapitalization.sentences,
)
);
},
);
}
This is the build function:
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TableCalendar(//default code),
Divider(),
_showList(),
],
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add, color: Colors.white,),
onPressed: () => _showCity(context)
),
);
}
The _showAlertDialog is called inside _showCity (see build > floating action button > onpressed)
_showCity:
_showCity(BuildContext context){
_showAlertDialog();
}
But after all attempts, every time I hit the textField in _showAlertDialog and it opens the keyboard, it throws an error:
A RenderFlex overflowed by 33 pixels on the bottom.
How can I handle that?
Update
_showList:
Widget _showList(){
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: _sEv.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () {
},
child: Dismissible(
child: ListTile(
leading: //loremipsum,
title: //blablabla,
subtitle: //dolorsit,
),
),
key: UniqueKey(),
direction: DismissDirection.startToEnd,
background: Container(
alignment: AlignmentDirectional.centerEnd,
color: Colors.redAccent,
child: Icon(Icons.delete_rounded),
),
onDismissed: (DismissDirection direction) {
setState(() {
//my irrelevant code
});
}
),);
},
);
}
UPDATE 2
ScreenShot:
Just wrap the body of this page in SingleChildScrollView like this.
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Scaffold(
resizeToAvoidBottomPadding: false,
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [ TableCalendar(
//default code),
Divider(), _showList(),
],
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add, color: Colors.white,),
onPressed: () => _showCity(context)
),
),
);
}

how to list dynamically radiobuttons contained in listTiles in flutter?

i would like to show a list of radiobuttons? this radiobuttons are contained in ListTile widget, i tried some code but nothing appears :
when i put a single radiobutton , this appears but when i put a listview nothing appears on the page including others widgets
Widget build(BuildContext context) {
return StoreConnector<MMpataState, MMpataViewModel>(
converter: MMpataViewModel.convertStateToViewModel,
builder: (BuildContext context, MMpataViewModel vm) {
// print(vm.state.subscriptions2);
if (vm.state.isSubscriptions2loaded &&
vm.state.subscriptions2.length < 1) {
return Scaffold(
appBar: AppBar(
title: Text("Aucune donnée"),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Center(
child: Text(
"Nous n'avons trouvé aucune souscription pour le produit
sélectionné.",
),
),
),
);
}
return Scaffold(
appBar: AppBar(
title: Text(!vm.state.isSubscriptions2loaded ? "Chargement ... " :
vm.state.subscriptions2[0].product.libelle),
),
body: MMpataLoader(
inAsyncCall: _isLoading || !vm.state.isSubscriptions2loaded,
child:
Column(
children: <Widget>[
Form(
child: new Container(
color: Color(0xffFFFFFF),
child:
Column( crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Column(crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget> [
Padding(padding:EdgeInsets.fromLTRB(25.0, 0.0,
0.0, 0.0),
child: Column(crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget> [
Text(""),
Text(vm.state.subscriptions2[0].product.libelle,
textAlign:TextAlign.left,style:TextStyle()),
Text(vm.state.subscriptions2[0].product.description,
textAlign: TextAlign.left),
Expanded(
child: ListView.builder(shrinkWrap:true,
itemCount: vm.state.subscriptions2.length,
itemBuilder: (context, index) {
return RadioListTile<double>(
title:Text(
"${vm.state.subscriptions2[index].quota.libelle}
(${vm.state.subscriptions2[index].amount} CDF)"),
value: vm.state.subscriptions2[index].amount,
groupValue:vm.state.subscriptions2[index].amount,
onChanged: (double value) {
setState(() {
vm.state.subscriptions2[index].amount = value;
});
},
);
}),
),
])
),
_getActionButtons(vm),
SizedBox(height: 100)
]) ],
),
),
)
],
),
),
);
},
);
}
i have this error while running : RenderBox was not laid out

Fixed buttons between AppBar and SingleChildScrollView (Flutter)

I would like to include buttons between AppBar and ListView. In the example below, the buttons scroll along with the text. I tried to include the SingleChildScrollView within a Column, but was unsuccessful.
I read that the Column widget does not support scrolling. I already searched a lot, but I didn't find an example similar to what I need.
Can someone help me?
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('A Idade do Lobo'),
elevation: 0.0,
backgroundColor: COLOR_MAIN,
),
body: NotificationListener(
onNotification: (notif) {
if (_hasScroll) {
if (notif is ScrollEndNotification && scrollOn) {
Timer(Duration(seconds: 1), () {
_scroll();
setState(() {
_controlButton();
});
});
}
}
return true;
},
child: SingleChildScrollView(
controller: _scrollController,
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Center(
child: new Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new RaisedButton(
onPressed: _showScrollPickerDialog,
child: Text('Rolagem ${_scrollSpeed}'),
),
new RaisedButton(
onPressed: _showTomPickerDialog,
child: Text('TOM ${_tom}'),
),
],
),
),
new Flexible(
fit: FlexFit.loose,
child: new ListView.builder(
shrinkWrap: true,
itemCount: _songDetails.length,
itemBuilder: (context, index) {
return new Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: new EdgeInsets.all(5.0),
child: new RichText(
text: TextSpan(children: [
new TextSpan(
text: '${_songDetails[index].line}',
style: _getStyle(
_songDetails[index].type,
),
),
]),
),
),
],
);
},
),
),
],
),
),
),
floatingActionButton: _controlButton(),
);
}
}
You can use bottom properly of AppBar to achieve desire UI.
Following example clear your idea.
import 'package:flutter/material.dart';
class DeleteWidget extends StatefulWidget {
const DeleteWidget({Key key}) : super(key: key);
#override
_DeleteWidgetState createState() => _DeleteWidgetState();
}
class _DeleteWidgetState extends State<DeleteWidget> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("your title"),
bottom: PreferredSize(
preferredSize: Size(MediaQuery.of(context).size.width, 40),
child: Center(
child: new Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new RaisedButton(
onPressed: () {},
child: Text('Rolagem '),
),
new RaisedButton(
onPressed: () {},
child: Text('TOM '),
),
],
),
),
),
),
body: Container(
child: ListView.builder(
itemBuilder: (context, int index) {
return Text(index.toString());
},
itemCount: 100,
),
));
}
}

Navigation.of(context) not going to another route

I'm trying to go to a new route using the following code but it doesn't work. There are no errors showing up in my IDE and my code seems pretty similar to all the resources I've checked on how to go to a new page.
I've compared my code to others online. I'm guessing its how I've implemented it. I have also tried using:
MaterialPageRoute(builder: (context) => SecondRoute())
But that was not the issue. Here is most of my app:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
Widget setTimer = Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.purpleAccent,width: 3)
),
padding: EdgeInsets.all(32),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Set Timer'),
onPressed: (){
//Go to the second route
Navigator.of(context).pushNamed('/page2');
},
)
],
),
);
return MaterialApp(
title: 'Hands Off',
routes: <String, WidgetBuilder>{
'/page2': (BuildContext context) => SecondRoute(),
},
home: Scaffold(
appBar: AppBar(
title: Text('Timer config'),
),
body: Column(
children: <Widget>[
setTimer,
],
),
),
);
}
}
class SecondRoute extends StatelessWidget{
#override
Widget build(BuildContext context) {
Widget firstRouteButton = Container(
child: Row(
children: <Widget>[
RaisedButton(
child: Text('Go back'),
onPressed: (){
//Go back to main route (first route)
Navigator.of(context).pop(true);
},
)
],
),
);
return Scaffold(
appBar: AppBar(
title: Text('hel'),
),
);
}
}
It should go to a new page but whenever I press the button nothing happens. Can you guys explain why it doesn't work as well please?
Pretty simple.
Use a Builder to pass the context down.
Builder(builder: (BuildContext context) {
return Whatever_Widget;
},)
so the code will be
body: Builder(builder: (BuildContext context) {
return Column(
children: <Widget>[
Container(
decoration: BoxDecoration(border: Border.all(color: Colors.purpleAccent, width: 3)),
padding: EdgeInsets.all(32),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Set Timer'),
onPressed: () {
//Go to the second route
// Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondRoute()));
Navigator.of(context).pushNamed('/page2');
},
)
],
),
),
],
);
},)