Problem in using Provider package in Flutter - flutter

main.dart
void main() {
runApp(ToDo());
}
class ToDo extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => TaskList(),
),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: TaskPage(),
theme: ThemeData.dark().copyWith(
canvasColor: Colors.transparent,
),
),
);
}
}
I am using two Consumer Widget one is returning a Text widget and another is returning a ListView.
class TaskPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xff212121),
body: Container(
padding: EdgeInsets.only(top: 50),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ListTile(
leading: CircleAvatar(
radius: 25,
backgroundColor: Colors.white,
child: Icon(
Icons.check,
color: Colors.black,
size: 40,
),
),
title: Text(
'ToDo',
style: TextStyle(
color: Colors.white,
fontSize: 50,
fontWeight: FontWeight.w900,
),
),
),
SizedBox(
height: 40,
),
Center(
child: Consumer<TaskList>(
builder: (context, list, child) {
print('making new text');
return Text(
'${list.length} Tasks Remaining',
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
);
},
),
),
Expanded(
child: Container(
child: Consumer<TaskList>(
builder: (context, list, child) {
return ListView(
children: list.taskList,
);
},
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),
),
),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
child: Icon(
Icons.add,
color: Colors.white,
),
backgroundColor: Colors.blueGrey,
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) => AddTaskSheet(),
);
},
),
);
}
}
The object which I want to provide to these widget is TaskList containing a List.
class TaskList with ChangeNotifier {
List<TaskTile> taskList = [
TaskTile(taskData: TaskData(task: 'Code')),
TaskTile(taskData: TaskData(task: 'Code Again')),
TaskTile(taskData: TaskData(task: 'Keep Coding')),
];
int get length => taskList.length;
void addTask(TaskTile newTaskTile) {
taskList.add(newTaskTile);
notifyListeners();
}
}
In order to add a task I am using a ModalBottomSheet which contain:
class AddTaskSheet extends StatelessWidget {
#override
Widget build(BuildContext context) {
String newTask;
return Container(
color: Colors.transparent,
child: Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Color(0xff212121),
borderRadius: BorderRadius.only(
topRight: Radius.circular(40.0),
topLeft: Radius.circular(40.0),
),
),
child: Column(
children: <Widget>[
TextField(
onChanged: (value) {
newTask = value;
},
decoration: InputDecoration(
hintText: 'Describe Your Task',
hintStyle: TextStyle(
color: Colors.white38,
),
fillColor: Colors.black,
filled: true,
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: BorderRadius.all(Radius.circular(30)),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white, width: 2.5),
borderRadius: BorderRadius.all(Radius.circular(30)),
),
),
),
SizedBox(height: 30),
AddButton(
onTap: () {
print(newTask);
Provider.of<TaskList>(context, listen: false).addTask(
TaskTile(
taskData: TaskData(task: newTask),
),
);
Navigator.pop(context);
},
),
],
),
),
);
}
}
Here I added a button named AddButton to AddTaskSheet:
import 'package:flutter/material.dart';
class AddButton extends StatelessWidget {
final Function onTap;
AddButton({this.onTap});
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Container(
child: ListTile(
contentPadding: EdgeInsets.symmetric(horizontal: 60),
title: Text(
'ADD NEW TASK',
style: TextStyle(
color: Colors.white70,
fontWeight: FontWeight.bold,
),
),
leading: Icon(
Icons.add,
color: Colors.white70,
),
),
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.all(
Radius.circular(30),
),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.black38,
blurRadius: 5.0,
offset: Offset(-5.0, 15.0),
)
],
),
),
);
}
}
Now Whenever I add a new task to my list then even after calling notifyListener the ListView do not update with new task but on the other hand the Text widget gets update with new size of list.
I don't know whats going wrong.
Can someone please help.

Related

Text disappears when opening Dart Flutter Keyboard

I have a sign up screen like this:
When the keyboard is opened to enter information into the textFormFields here, the screen looks like this:
The text "Welcome to register form" disappeared. What is the cause of this problem? How can I solve it? Thanks in advance for the help.
Codes:
main.dart:
import 'package:flutter/material.dart';
import 'package:simto_todolist/app.dart';
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: splashScreen(),
);
}
}
app.dart:
import 'package:flutter/material.dart';
import 'package:cool_alert/cool_alert.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:timer_count_down/timer_count_down.dart';
import 'package:group_button/group_button.dart';
import 'package:firebase_auth/firebase_auth.dart';
var currentPage;
class splashScreen extends StatefulWidget {
#override
State<splashScreen> createState() => _splashScreenState();
}
final CarouselController _pageController = CarouselController();
class _splashScreenState extends State<splashScreen> {
List splashs = [sp1(), sp2(), sp3()];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Image.asset("logo.png", fit: BoxFit.cover, height: 75,),
centerTitle: true,
backgroundColor: Colors.white,
elevation: 0,
),
backgroundColor: Colors.white,
body: CarouselSlider(
carouselController: _pageController,
items: splashs.map((i) {
return Builder(
builder: (BuildContext context) {
return i;
},
);
}).toList(),
options: CarouselOptions(
height: MediaQuery.of(context).size.height,
viewportFraction: 1,
enableInfiniteScroll: false,
onPageChanged: (index, reason) {
setState(() {
currentPage = index;
print(currentPage);
});
},
),
)
);
}
}
class sp1 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height / 5
),
Image.asset('assets/splash1.png', fit: BoxFit.cover, height: 220,),
Text("Hoş geldin!", style: TextStyle(fontSize: 25, fontFamily: "Roboto-Bold"),),
SizedBox(height: 15),
Expanded(child: Text("Simto: To-do-list uygulamasına hoş geldin.", style: TextStyle(fontSize: 19, fontFamily: "Roboto-Thin"),)),
RaisedButton(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.arrow_forward, color: Colors.white,),
SizedBox(width: 10),
Text("Devam", style: TextStyle(color: Colors.white, fontSize: 20, fontFamily: "Roboto-Thin"),),
],
),
),
color: Colors.blue[400],
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
onPressed: () {
_pageController.nextPage(duration: Duration(milliseconds: 500), curve: Curves.easeIn);
},
),
SizedBox(height: 18),
]
);
}
}
class sp2 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height / 5
),
Image.asset('assets/splash2.png', fit: BoxFit.cover, height: 235,),
Text("Yapılacakları planlayın", style: TextStyle(fontSize: 25, fontFamily: "Roboto-Bold"),),
SizedBox(height: 15),
Expanded(child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.white,
),
child: Text("Düzenli bir gün, yapılacaklar listesi hazırlamakla başlar. Hemen yapılacaklar listesi oluşturun!", style: TextStyle(fontSize: 18, fontFamily: "Roboto-Thin"), textAlign: TextAlign.center,))),
SizedBox(height: 25),
RaisedButton(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.arrow_forward, color: Colors.white,),
SizedBox(width: 10),
Text("Devam", style: TextStyle(color: Colors.white, fontSize: 20, fontFamily: "Roboto-Thin"),),
],
),
),
color: Colors.blue[400],
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
onPressed: () {
_pageController.nextPage(duration: Duration(milliseconds: 500), curve: Curves.easeIn);
},
),
SizedBox(height: 18),
]
);
}
}
class sp3 extends StatefulWidget {
#override
State<sp3> createState() => _sp3State();
}
class _sp3State extends State<sp3> {
#override
Widget build(BuildContext context) {
return Column(
children: [
Image.asset('assets/splash3.png', fit: BoxFit.cover, height: 220,),
Text("Register", style: TextStyle(fontSize: 25, fontFamily: "Roboto-Bold"),), // Kayıt ol
SizedBox(height: 15),
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.white,
),
child: Text("Welcome to register form", style: TextStyle(fontSize: 18, fontFamily: "Roboto-Thin"), textAlign: TextAlign.center,))), // Hemen kayıt ol ve yapılacaklar listeni hazırla!
SizedBox(height: 25),
Padding(
padding: const EdgeInsets.only(left: 11, right: 11),
child: TextFormField(
decoration: InputDecoration(
hintText: "E-mail",
labelStyle: TextStyle(fontSize: 18, fontFamily: "Roboto-Thin", color: Colors.grey),
prefixIcon: Icon(Icons.email, color: Colors.grey,),
border: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
)
),
style: TextStyle(
color: Colors.black,
fontSize: 20
),
),
),
SizedBox(height: 10,),
Padding(
padding: const EdgeInsets.only(left: 11, right: 11),
child: TextFormField(
decoration: InputDecoration(
hintText: "Name-surname", // Ad-soyad
labelStyle: TextStyle(fontSize: 18, fontFamily: "Roboto-Thin", color: Colors.grey),
prefixIcon: Icon(Icons.person, color: Colors.grey, size: 30,),
border: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
),
),
style: TextStyle(
color: Colors.black,
fontSize: 20
),
),
),
// şifre input:
SizedBox(height: 10,),
Padding(
padding: const EdgeInsets.only(left: 11, right: 11),
child: TextFormField(
decoration: InputDecoration(
hintText: "Password", // Şifre
labelStyle: TextStyle(fontSize: 18, fontFamily: "Roboto-Thin", color: Colors.grey),
prefixIcon: Icon(Icons.lock, color: Colors.grey,),
border: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
),
suffixIcon: InkWell(
child: Icon(Icons.remove_red_eye),
)
),
style: TextStyle(
color: Colors.black,
fontSize: 20
),
obscureText: true,
),
),
SizedBox(height: 25,),
RaisedButton(
child: Text("Register"),
onPressed: () {
}
),
],
);
}
}
class loadingAccount extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('assets/splash4.png', fit: BoxFit.cover, height: 200,),
SizedBox(height: 15),
Text("Her şey size özel hazırlanıyor..", style: TextStyle(fontSize: 22, fontFamily: "Roboto-Medium"), textAlign: TextAlign.center,),
SizedBox(height: 15),
CircularProgressIndicator(),
Countdown(
seconds: 4,
interval: Duration(milliseconds: 500),
build: (BuildContext context, double time) {
return SizedBox();
},
onFinished: () {
//register();
print("bitti");
Navigator.push(context,
PageRouteBuilder(
barrierDismissible: false,
opaque: true,
transitionDuration: Duration(milliseconds: 150),
transitionsBuilder: (BuildContext context, Animation<double> animation, Animation <double> secAnimation, Widget child) {
return ScaleTransition(
alignment: Alignment.center,
scale: animation,
child: child,
);
},
pageBuilder: (BuildContext context, Animation<double> animation, Animation <double> secAnimation) {
// return HomePage();
}
),
);
},
),
WillPopScope(
child: Container(),
onWillPop: () {
return Future.value(false);
},
)
],
),
),
);
}
}
My goal is to create a nice sign up screen.
Try wrapping your column with a SingleChildScrollView in SP3 as follows:
return SingleChildScrollView(
child: Column(
children: [<COLUMN-CHILDREN>]
),
);
This will allow your column to expand to its full minimum MainAxisSize when the keyboard is shown, instead of trying to squeeze all the elements onto the screen

How to show an alert dialog after the user click the button in flutter

I'm new in flutter and I'm trying to show a dialog alert for the result if the user click on "Calculate" button. I want to change the "Text" result into the dialog alert like "The prediction of (total days) is (result)" Anyone can help me how to do this? I can't find any suitable resource for this, Thank you
class TransactionYearly extends StatefulWidget {
#override
_TransactionYearlyState createState() => _TransactionYearlyState();
}
class _TransactionYearlyState extends State<TransactionYearly> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final TextEditingController amount = new TextEditingController();
final TextEditingController day = new TextEditingController();
double _result;
#override
void initState() {
super.initState();
}
#override
void dispose() {
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(getTranslated(context, 'prediction_calculation'),),
elevation: 0,
brightness: Brightness.light,
backgroundColor: primary,
leading: IconButton(
onPressed: (){
Navigator.pop(context, MaterialPageRoute(builder: (context) => Transactions()));
},
icon: Icon(Icons.arrow_back_ios,
size: 20,
color: Colors.black,),
),
),
body: SingleChildScrollView(
child: Container(
child: Column(
children: <Widget>[
Container(
height: 300,
child: Image(
image: AssetImage("assets/girlsave.png"),
fit: BoxFit.contain,
),
),
Container(
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
Container(
child: TextFormField(
validator: (input) {
if (input.isEmpty) return 'Please fill up the text fields';
},
controller: amount,
decoration: InputDecoration(
labelText: getTranslated(context, 'amount_text'),
labelStyle: TextStyle(color: Colors.grey),
prefixIcon: Icon(
Icons.attach_money,
color: secondary,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: secondary),
),
),
keyboardType: TextInputType.number,
),
),
Container(
child: TextFormField(
validator: (input) {
if (input.isEmpty)
return 'Please fill up the text fields';
},
controller: day,
decoration: InputDecoration(
labelText: getTranslated(context, 'day_text'),
labelStyle: TextStyle(color: Colors.grey),
prefixIcon: Icon(
Icons.date_range_outlined,
color: secondary,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: secondary),
),
),
keyboardType: TextInputType.number,
),
),
SizedBox(height: 20),
Container(
padding: EdgeInsets.only(
top: 25.0, left: 20.0, right: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget> [
Expanded (
child: ElevatedButton(
onPressed: () {
if(!_formKey.currentState.validate()){
return;
}
_formKey.currentState.save();
calculate();
},
child: Text(getTranslated((context), "calculate_button").toUpperCase(), style: TextStyle (
fontSize: 14,
)),
style: ButtonStyle(
padding: MaterialStateProperty.all<EdgeInsets>(EdgeInsets.all(15)),
foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
backgroundColor: MaterialStateProperty.all<Color>(Colors.pink),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
side: BorderSide(color: secondary)
),
),
),
),
),
SizedBox(width: 20, height: 10),
Expanded(
child: ElevatedButton(
onPressed: () {
amount.clear();
day.clear();
},
child: Text(getTranslated((context), "clear_button").toUpperCase(), style: TextStyle (
fontSize: 14
)),
style: ButtonStyle(
padding: MaterialStateProperty.all<EdgeInsets>(EdgeInsets.all(15)),
foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
backgroundColor: MaterialStateProperty.all<Color>(Colors.pink),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
side: BorderSide(color: secondary)
),
),
),
),
)
],
)
),
Text(
_result == null ? "Enter amount" : "$_result",
)
],
),
),
),
],
),
),
));
}
void calculate(){
double amounts = double.parse(amount.text);
double days = double.parse(day.text);
double result = amounts * days;
_result = result;
setState(() {
});
}
}
After calculate call you can show dialog like this in the onPressed function.
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Result'),
content: Text('Result is $_result'),
actions: [
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go Back'))
],
),
);

why am i getting a null value out of this callback?

I'm trying to move the value of newTaskText from the second class to the first one , I have a callback
which is addTaskCallback in the second class that i use when i press the flat button to move the value up to the first one which is TasksScreen but when i try to print the value it comes out as null , any idea why ?
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:todoey/widgets/tasks_list.dart';
import 'add_task_screen.dart';
import 'package:todoey/models/task.dart';
class TasksScreen extends StatefulWidget {
#override
_TasksScreenState createState() => _TasksScreenState();
}
class _TasksScreenState extends State<TasksScreen> {
List<Task> tasks = [
Task(name: 'Buy Milk'),
Task(name: 'Buy eggs'),
Task(name: 'Buy bread'),
];
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.lightBlueAccent,
//backgroundColor: Colors.lightBlueAccent,
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.lightBlueAccent,
//backgroundColor: Colors.lightBlueAccent,
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) => AddTaskScreen(
(newTaskText) {
print(newTaskText);
},
),
);
},
child: Icon(Icons.add),
),
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.only(
top: 60.0, bottom: 30.0, right: 30.0, left: 30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CircleAvatar(
backgroundColor: Colors.white,
radius: 30.0,
child: Icon(
Icons.list,
color: Colors.lightBlueAccent,
size: 30.0,
),
),
SizedBox(height: 10.0),
Text(
'Todoey',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w700,
fontSize: 50.0,
),
),
Text(
'12 tasks',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
],
),
),
Expanded(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
child: TasksList(tasks),
),
),
],
),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:todoey/models/task.dart';
class AddTaskScreen extends StatelessWidget {
final Function addTaskCallback;
AddTaskScreen(this.addTaskCallback);
#override
Widget build(BuildContext context) {
String newTaskTitle;
return Container(
color: Color(0xff757575),
child: Container(
padding: EdgeInsets.all(20.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Add Task',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.lightBlueAccent,
fontSize: 30.0,
),
),
TextField(
autofocus: true,
textAlign: TextAlign.center,
onChanged: (newText) {
newTaskTitle = newText;
},
),
SizedBox(
height: 10.0,
),
FlatButton(
color: Colors.lightBlueAccent,
onPressed: () {
addTaskCallback(newTaskTitle);
print(newTaskTitle);
},
child: Text(
'Add',
style: TextStyle(
color: Colors.white,
),
),
)
],
),
),
);
}
}
Your problem here is that you defined the var newTaskTitle in the build method. Moving it out from the build method should fix your problem !
There is nothing wrong with your implementation of the callback. The only thing that could be improved is to define the Type of the variable sent in the callback Function with
final Function(String) addTaskCallback;
You could also use a TextController instead of a var updated in the onChanged method.
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
class AddTaskScreen extends StatefulWidget {
final Function(String) addTaskCallback;
AddTaskScreen(this.addTaskCallback);
#override
_AddTaskScreenState createState() => _AddTaskScreenState();
}
class _AddTaskScreenState extends State<AddTaskScreen> {
TextEditingController textController = new TextEditingController();
#override
Widget build(BuildContext context) {
return Container(
color: Color(0xff757575),
child: Container(
padding: EdgeInsets.all(20.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Add Task',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.lightBlueAccent,
fontSize: 30.0,
),
),
TextField(
autofocus: true,
controller: textController,
textAlign: TextAlign.center,
),
SizedBox(
height: 10.0,
),
FlatButton(
color: Colors.lightBlueAccent,
onPressed: () {
widget.addTaskCallback(textController.text);
print(textController.text);
},
child: Text(
'Add',
style: TextStyle(
color: Colors.white,
),
),
)
],
),
),
);
}
}
Have fun with Flutter !

Image not showing up in ListView

I know the code is a little messy, still learning.
I have placed the Image outside of the BoxDecoration but noting is showing up. Nothing shows up in the RUN as an error. I am guessing that it has something to do with the container inside the ListView but haven't been able to find out why its blank.
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
elevation: 0.0,
title: Text("App"),
),
body: SafeArea(
child: Container(
height: 80,
decoration: new BoxDecoration(
color: Colors.blue,
borderRadius: new BorderRadius.only(
bottomLeft: const Radius.circular(40.0),
bottomRight: const Radius.circular(40.0))
),
child: ListView(
children: <Widget>[
Container(
margin: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
width: 120,
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey[300],
offset: Offset(1, 1),
blurRadius: 0
),
], // BoxShadow
borderRadius: BorderRadius.all(Radius.circular(20))
),
child: ListTile(
leading: Icon(Icons.search, color: Colors.red),
title: TextField(
decoration: InputDecoration(
hintText: "What stuff do you want to seach for?",
hintStyle: TextStyle(fontSize: 16, color: Colors.grey),
border: InputBorder.none
),
),
trailing: Icon(Icons.filter_list, color: Colors.red),
),
),
SizedBox(height: 5),
Container(
height: 220,
width: 220,
child: Column(
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.red,
offset: Offset(1, 1),
blurRadius: 4
),
],
),
child: Image.asset("assets/images/1.jpg"),
),
],
),
),
],
),
),
)
);
}
}
This is what I am trying to do.
I updated the structure as a Column with two children:
The blue Container with the TextField
The ListView with the pictures
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: Home(),
),
);
}
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(elevation: 0.0, title: Text("App")),
body: SafeArea(
child: Column(
children: [
Container(
padding: EdgeInsets.all(20.0),
decoration: const BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(40.0),
bottomRight: Radius.circular(40.0),
),
),
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20.0),
),
child: const ListTile(
leading: Icon(Icons.search),
title: TextField(
decoration: InputDecoration(
hintText: "What stuff do you want to seach for?",
hintStyle: TextStyle(fontSize: 16, color: Colors.grey),
border: InputBorder.none,
),
autofocus: true,
),
trailing: Icon(Icons.filter_list),
),
),
),
SizedBox(
height: 120.0,
child: ListView(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
children: List.generate(
20,
(index) => Padding(
padding: const EdgeInsets.all(16.0),
child: Image.asset("assets/images/cartoon.jpg"),
),
),
),
),
],
),
),
);
}
}

How do i center a text field horizontaly and vertically in flutter

Hi i am new to flutter i need to align the text field horizontally and vertically for more elaboration see this
What i am getting:
Text aligned at the very top it needs to be in the center
What i really want:
the text field is aligned both horizontaly and vertically that is exactly what i am looing for
Here is the code:
import 'package:multi_purpose_scope/Animation/FadeAnimation.dart';
import 'package:flutter/material.dart';
void main() => runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
)
);
class HomePage extends StatelessWidget {
gotoSecondActivity(BuildContext context){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondActivity()),
);
}
#override
Widget build(BuildContext context){
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: RaisedButton(
onPressed: () {
gotoSecondActivity(context);
},
child: Container(
child: Column(
children: <Widget>[
Container(
height: 400,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/loginHeader.png'),
fit: BoxFit.fill
)
),
child: Stack(
children: <Widget>[
],
),
),
Padding(
padding: EdgeInsets.all(30.0),
child: Column(
children: <Widget>[
FadeAnimation(1.8, Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: Color.fromRGBO(143, 148, 251, .2),
blurRadius: 20.0,
offset: Offset(0, 10)
)
]
),
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border(bottom: BorderSide(
color: Colors.grey[100]))
),
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Email or Phone number",
hintStyle: TextStyle(
color: Colors.grey[400])
),
),
),
Container(
padding: EdgeInsets.all(8.0),
child: TextField(
obscureText: true,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Password",
hintStyle: TextStyle(
color: Colors.grey[400])
),
),
)
],
),
)),
SizedBox(height: 30,),
FadeAnimation(2, Container(
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [
Color.fromRGBO(214, 0, 27, 1),
Color.fromRGBO(214, 0, 27, 1),
]
)
),
child: Center(
child: Text("Login", style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),),
),
)),
SizedBox(height: 70,),
FadeAnimation(1.5, Text("Forgot Password?",
style: TextStyle(
color: Color.fromRGBO(214, 0, 27, 1)),)),
],
),
)
],
),
),
)
),
);
}
}
class SecondActivity extends StatelessWidget {
gotoRegister(BuildContext context){
Navigator.push(
context,
MaterialPageRoute(builder: (context) =>Register()),
);
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: Container(
height: 174,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/patient_list.png'),
)
),
child: Stack(
children: <Widget>[
Container(
alignment: Alignment.center,
height: 128,
child: Text(
'Patient List',
style: TextStyle(
fontWeight: FontWeight.bold,fontSize: 30,
color: Colors.white
),
),
)
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
gotoRegister(context);
},
tooltip: 'Increment',
child: Icon(Icons.add),
backgroundColor: Color.fromRGBO(214, 0, 27,1),
),
),
);
}
}
class Register extends StatelessWidget {
goBack(BuildContext context) {
Navigator.pop(context);
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child:Container(
height: 174,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/patient_list.png'),
)
),
child: Stack(
children: <Widget>[
Container(
alignment: Alignment.center,
height: 128,
child: Text(
'Registration
style: TextStyle(
fontWeight: FontWeight.bold,fontSize: 30,
color: Colors.white
),
)
),
Container(
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border(bottom: BorderSide(
color: Colors.grey[100]))
),
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Email or Phone number",
hintStyle: TextStyle(
color: Colors.grey[400])
),
),
),
],
),
),
)
),
);
}
}
I searched for this but cant seem to find the solution to my problem.
I have almost tried all the solutions that i can think of but those don't work
Just set the textAlign property to your TextField and also add a border in your decoration.
Sample:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Column(
children: <Widget>[
const SampleTextField(hintText: 'Enter Name'),
const SizedBox(height: 10),
const SampleTextField(hintText: 'Enter MR-Number'),
const SizedBox(height: 10),
const SampleTextField(hintText: 'Enter Phone Number'),
const SizedBox(height: 10),
const SampleTextField(hintText: 'Enter Hospital Name'),
const SizedBox(height: 10),
FlatButton(
onPressed: () {},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
// materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
// visualDensity: VisualDensity.compact,
padding: const EdgeInsets.symmetric(vertical: 20),
color: Colors.red,
child: const Center(
child: Text('Registration'),
),
),
],
),
),
),
),
);
}
}
class SampleTextField extends StatelessWidget {
const SampleTextField({
this.controller,
this.hintText = '',
});
final TextEditingController controller;
final String hintText;
#override
Widget build(BuildContext context) {
return TextField(
controller: controller,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(
width: 1,
color: Colors.black54,
),
),
hintText: hintText,
hintStyle: const TextStyle(
color: Colors.black54,
),
contentPadding: const EdgeInsets.only(left: 20),
),
// textAlign: TextAlign.center, // Align vertically and horizontally
);
}
}
You are using a stack widget so if you want to align and positioned your widget inside the stack see the below options
Wrap widget with Positioned widget if you want to change position
Wrap widget with Align widget for alignment
Check out this Video for how to use Positioned widget
Check out this Video for how to use Align widget