I am making a bus app to learn more about flutter and dart language. So, i came across this problem with the navigation drawer where it doesn't go to the screen it's supposed to go to when I clicked on it. It just stays the same.
This is my code for the main screen.
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
State<StatefulWidget> createState() {
return MainScreen();
}
}
class MainScreen extends State<MyApp> {
final currentTime = DateTime.now();
final busStopController = TextEditingController();
//To customise the greeting according to the time
String greeting() {
var hour = DateTime.now().hour;
if (hour < 12) {
return 'Morning';
}
if (hour < 17) {
return 'Afternoon';
}
return 'Evening';
}
//Strings for user input and busStop
String name = '';
String busStop = '';
//Strings for different bus timings
String sb1timing = '';
String sb2timing = '';
String sb3timing = '';
String sb4timing = '';
//Different icon colors for bus capacity
final Color _iconColorEmpty = Colors.green;
final Color _iconColorHalf = Colors.orange;
final Color _iconColorFull = Colors.red;
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.transparent,
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.black45,
),
child: Text(
'WaitLah! bus services',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 20),
),
),
ListTile(
leading: const Icon(
Icons.directions_walk_outlined,
color: Colors.black,
),
title: const Text(
'Plan Your Journey',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PlanJourneyScreen()));
},
),
And this is my code for the second screen (PlanJourneyScreen)
class PlanJourneyScreen extends StatelessWidget {
const PlanJourneyScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Stack(
children: [
const Backgroundimage(),
Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
elevation: 0.0,
backgroundColor: Colors.transparent,
centerTitle: true,
title: Text(
'Plan Your Journey s10194266d',
style: GoogleFonts.kalam(
textStyle: const TextStyle(
fontSize: 24, fontWeight: FontWeight.bold)),
),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(
height: 30,
),
Text(
'Please state your source and destination.',
textAlign: TextAlign.center,
style: GoogleFonts.montserrat(
textStyle: const TextStyle(
fontSize: 17,
color: Colors.white,
fontWeight: FontWeight.bold)),
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 30, 10, 10),
child: Text(
'Source',
textAlign: TextAlign.left,
style: GoogleFonts.montserrat(
textStyle: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
),
TextField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(10.0)),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 30, 10, 10),
child: Text(
'Destination',
style: GoogleFonts.montserrat(
textStyle: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
),
TextField(
style: const TextStyle(color: Colors.white),
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(10.0)),
),
onTap: () {},
),
Container(
padding: const EdgeInsets.fromLTRB(0, 30, 0, 0),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.white54,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(32.0)),
minimumSize: Size(90, 40)),
onPressed: () {},
child: Text(
'Calculate!',
style: GoogleFonts.montserrat(
textStyle: const TextStyle(
fontSize: 16, fontWeight: FontWeight.bold),
),
),
),
),
Container()
],
),
),
],
);
}
}
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (_) => PlanJourneyScreen(),
),
),
In your PlanJourneyScreen() you return Stack please Wrap it with Scaffold, try below code its working well, refer navigation
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MyWidget(),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.black45,
),
child: Text(
'WaitLah! bus services',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 20),
),
),
ListTile(
leading: const Icon(
Icons.directions_walk_outlined,
color: Colors.black,
),
title: const Text(
'Plan Your Journey',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const PlanJourneyScreen()),
);
},
),
],
),
),
);
}
}
//PlanJourneyScreen Code
class PlanJourneyScreen extends StatelessWidget {
const PlanJourneyScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
elevation: 0.0,
backgroundColor: Colors.transparent,
centerTitle: true,
title: Text(
'Plan Your Journey s10194266d',
),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(
height: 30,
),
Text(
'Please state your source and destination.',
textAlign: TextAlign.center,
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 30, 10, 10),
child: Text(
'Source',
textAlign: TextAlign.left,
),
),
TextField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(10.0)),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 30, 10, 10),
child: Text(
'Destination',
),
),
TextField(
style: const TextStyle(color: Colors.white),
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(10.0)),
),
onTap: () {},
),
Container(
padding: const EdgeInsets.fromLTRB(0, 30, 0, 0),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.white54,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(32.0)),
minimumSize: Size(90, 40)),
onPressed: () {},
child: Text(
'Calculate!',
),
),
),
Container()
],
),
),
],
),
);
}
}
Related
I have some errors when trying to send data over to Firebase. I already put the necessary codes but i cant seem to make it work.
This codes is for a feedback page. It will prompt a alertdialog when users already type their feedback in a textfield. In the alertdialog, their feedback input will be shown along with their rating of the app. Then there's two option - cancel (which goes back to the feedback page without sending data to firebase) and Yes (it sends the data to firebase and will show a snackbar that it is senet successfully)
I have already imported the files too.
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
Is there anything wrong with the way i code?
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(ReportScreen());
}
class ReportScreen extends StatefulWidget {
const ReportScreen({Key? key}) : super(key: key);
#override
State<StatefulWidget> createState() {
return Report();
}
}
class Report extends State<ReportScreen> {
bool abc = false;
final improvementsController = TextEditingController();
String improvementComment = '';
final GlobalKey<FormState> _formkey = GlobalKey();
double _rating = 0;
Future<void> _popup(BuildContext context) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
key: _formkey,
title: const Text('Submit Feedback?'),
content: Text('FeedBack: $improvementComment \nRating: $_rating '),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(
'Cancel',
style: GoogleFonts.montserrat(
textStyle: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 12)),
)),
TextButton(
onPressed: () async {
String message;
final collection =
FirebaseFirestore.instance.collection('feedback');
await collection.doc().set({
'timestamp': FieldValue.serverTimestamp(),
'feedback': improvementsController.text,
});
message = "Feedback submitted successfully";
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(message),
duration: const Duration(seconds: 3),
));
Navigator.pop(context);
},
child: Text(
'Yes',
style: GoogleFonts.montserrat(
textStyle: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 12)),
))
],
);
});
}
#override
void dispose() {
improvementsController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Builder(builder: (context) {
return Stack(
children: [
const Backgroundimage(),
Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
elevation: 0.0,
centerTitle: true,
backgroundColor: Colors.transparent,
title: Text(
'Give Feedback s10194266d',
style: GoogleFonts.kalam(
textStyle: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 24)),
),
),
body: Center(
child: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(0, 30, 0, 0),
child: Text(
'Are you satisfied with the app?',
style: GoogleFonts.montserrat(
textStyle: const TextStyle(
fontSize: 18,
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 20, 0, 20),
child: RatingBar.builder(
initialRating: 3,
itemCount: 5,
minRating: 1,
itemBuilder: (context, _context) {
switch (_context) {
case 0:
return const Icon(
Icons.sentiment_very_dissatisfied,
color: Colors.red,
);
case 1:
return const Icon(
Icons.sentiment_dissatisfied,
color: Colors.redAccent,
);
case 2:
return const Icon(
Icons.sentiment_neutral,
color: Colors.amber,
);
case 3:
return const Icon(
Icons.sentiment_satisfied,
color: Colors.lightGreen,
);
case 4:
return const Icon(
Icons.sentiment_very_satisfied,
color: Colors.green,
);
default:
return const Icon(
Icons.sentiment_very_satisfied,
color: Colors.green,
);
}
},
onRatingUpdate: (rating) {
_rating = rating;
setState(() {});
},
),
),
Text(
'Rating: $_rating',
style: GoogleFonts.montserrat(
fontSize: 13,
fontWeight: FontWeight.bold,
color: Colors.white),
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 20, 0, 20),
child: Text(
'What can the app improve on?',
style: GoogleFonts.montserrat(
textStyle: const TextStyle(
fontSize: 18,
color: Colors.white,
fontWeight: FontWeight.bold)),
),
),
SizedBox(
width: 350,
height: 100,
child: TextField(
maxLines: null,
style: GoogleFonts.montserrat(
textStyle: const TextStyle(color: Colors.white)),
controller: improvementsController,
decoration: InputDecoration(
contentPadding:
const EdgeInsets.symmetric(vertical: 40.0),
hintText: ' Enter your feedback here',
hintStyle: const TextStyle(color: Colors.white),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: const BorderSide(color: Colors.white),
),
),
onChanged: (context) {
improvementComment = context;
},
),
),
Padding(
padding: const EdgeInsets.all(30),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.black54,
shadowColor: Colors.white24,
padding: const EdgeInsets.all(10)),
onPressed: () {
_popup(context);
improvementsController.clear();
},
child: Text('Send \nFeedback',
textAlign: TextAlign.center,
style: GoogleFonts.montserrat(
textStyle: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold),
))),
const SizedBox(width: 40),
ElevatedButton(
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(10),
primary: Colors.black54,
shadowColor: Colors.white24),
onPressed: () {},
child: Text('See Previous\nFeedback',
textAlign: TextAlign.center,
style: GoogleFonts.montserrat(
textStyle: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold),
))),
],
),
)
],
),
),
),
],
);
});
}
}
I am trying to create a scrollable sign up screen for when the keyboard pops up but my SingleChildScrollView widget doesn't seem to be working. I don't know whether this has to do with its placement in the stack or the inner column but help would be much appreciated. I've tried a ListView instead of a column, using the Expanded widget around the SingleChildScrollView but to no success.
Sign Up Screen
Sign Up Screen with Keyboard
An example to copy and run here.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const SignUpScreen(),
);
}
}
class SignUpScreen extends StatefulWidget {
const SignUpScreen({Key? key}) : super(key: key);
#override
_SignUpScreenState createState() => _SignUpScreenState();
}
class _SignUpScreenState extends State<SignUpScreen> {
final _formKey = GlobalKey<FormState>();
bool _obscurePassword = true;
Color _obscureColor = Colors.grey;
InputDecoration textFormFieldDecoration = InputDecoration(
filled: true,
fillColor: const Color(0xFFF5F5F5),
prefixIcon: const Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Icon(
Icons.email,
color: Colors.blueAccent,
size: 26,
),
),
suffixIcon: null,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: const BorderSide(color: Colors.white),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: const BorderSide(color: Colors.white),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(30),
borderSide: const BorderSide(color: Colors.white),
),
hintText: 'E-mail',
hintStyle: const TextStyle(
fontFamily: 'OpenSans',
fontSize: 18,
),
);
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: false,
// Shows circular progress indicator while loading
body:
// A stack lays items over one another.
Stack(
children: [
//The SizedBox provides the colored rounded aesthetic card behind the login.
SizedBox(
height: MediaQuery.of(context).size.height * 0.7,
child: Container(
color: Colors.redAccent,
),
),
Form(
key: _formKey,
child: Column(
// Keeps things in the center vertically
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// Builds App Logo
Row(
// Keeps things in the center horizontally.
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text(
'Heart',
style: TextStyle(
fontSize: 34,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
Text(
'by AHG',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
const SizedBox(height: 30),
// Build sign up container card.
ClipRRect(
borderRadius: const BorderRadius.all(
Radius.circular(20),
),
child: SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height * 0.65,
width: MediaQuery.of(context).size.width * 0.85,
color: Colors.white,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//Sign Up / Login Text
const Text(
'Sign Up',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.w700,
color: Colors.black,
),
),
const SizedBox(height: 25),
// Email form field
TextFormField(
keyboardType: TextInputType.emailAddress,
decoration: textFormFieldDecoration.copyWith(
prefixIcon: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20),
child: Icon(
Icons.email,
color: Theme.of(context)
.colorScheme
.secondary,
size: 26,
),
),
hintText: 'E-mail'),
autocorrect: false,
),
const SizedBox(height: 13),
// Build password form field
TextFormField(
obscureText: _obscurePassword,
keyboardType: TextInputType.text,
decoration: textFormFieldDecoration.copyWith(
prefixIcon: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20),
child: Icon(
Icons.lock,
color: Theme.of(context)
.colorScheme
.secondary,
size: 26,
),
),
suffixIcon: Padding(
padding: const EdgeInsets.only(right: 25),
child: IconButton(
icon: const Icon(
Icons.remove_red_eye_rounded),
color: _obscureColor,
iconSize: 23,
onPressed: () {
setState(() {
_obscurePassword =
!_obscurePassword;
_obscureColor == Colors.grey
? _obscureColor =
Theme.of(context)
.colorScheme
.secondary
: _obscureColor = Colors.grey;
});
},
),
),
hintText: 'Password'),
autocorrect: false,
),
const SizedBox(height: 13),
// Build confirm password form field
TextFormField(
obscureText: _obscurePassword,
keyboardType: TextInputType.text,
decoration: textFormFieldDecoration.copyWith(
prefixIcon: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20),
child: Icon(
Icons.lock,
color: Theme.of(context)
.colorScheme
.secondary,
size: 26,
),
),
suffixIcon: Padding(
padding: const EdgeInsets.only(right: 25),
child: IconButton(
icon: const Icon(
Icons.remove_red_eye_rounded),
color: _obscureColor,
iconSize: 23,
onPressed: () {
setState(() {
_obscurePassword =
!_obscurePassword;
_obscureColor == Colors.grey
? _obscureColor =
Theme.of(context)
.colorScheme
.secondary
: _obscureColor = Colors.grey;
});
},
),
),
hintText: 'Confirm'),
autocorrect: false,
),
// Login Button
const SizedBox(
height: 30,
),
SizedBox(
height: 1.4 *
(MediaQuery.of(context).size.height / 20),
width: 5 *
(MediaQuery.of(context).size.width / 10),
child: ElevatedButton(
child: const Text('Sign Up'),
onPressed: _validateInputs,
),
),
],
),
),
),
),
),
//Sign Up Text
const SizedBox(
height: 20,
),
TextButton(
onPressed: () {},
child: RichText(
text: TextSpan(children: [
TextSpan(
text: 'Don\'t have an account?',
style: TextStyle(
color: Colors.black,
fontSize: MediaQuery.of(context).size.height / 40,
fontWeight: FontWeight.w400,
),
),
TextSpan(
text: ' Login',
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
fontSize: MediaQuery.of(context).size.height / 40,
fontWeight: FontWeight.bold,
),
)
]),
),
),
],
),
)
],
),
),
);
}
void _validateInputs() {
if (_formKey.currentState?.validate() != null) {
final _validForm = _formKey.currentState!.validate();
if (_validForm) {
_formKey.currentState!.save();
}
}
}
}
Change resizeToAvoidBottomInset to true
put SingleChildScrollView before Stack
Like this
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: true,
body: SingleChildScrollView(...)
Put the SingleChildScrollView as the Scaffold's body.
Scaffold(
resizeToAvoidBottomInset: true,
body:
SingleChildScrollView(child: ...)
)
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
I want to validate my textfield(not textFormField) which is in bottomsheet, if textfield is empty it should shows error when I pressed the save button.
I used below code the problem with the code is it shows error only when I press on save button and should close the bottomsheet and when I open bottomsheet then it will show error.
Is there any way to validate the textfield on pressing save button and without closing the bottomsheet.
Thanks in advance.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: AddAddress()));
class AddNotes extends StatefulWidget {
#override
AddAddressState createState() => AddAddressState();
}
class AddAddressState extends State<AddAddress> {
final _text = TextEditingController();
bool _validate = false;
#override
void dispose(){
_text.dispose();
super.dispose();
}
bottomSheet() {
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height * 0.85,
decoration: new BoxDecoration(
color: Colors.transparent,
),
child: Container(
padding: const EdgeInsets.fromLTRB(20, 10, 20, 0),
decoration: new BoxDecoration(
color: Colors.white,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(10.0),
topRight: const Radius.circular(10.0))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text("Address",
textScaleFactor: 1.3,
style: TextStyle(
fontSize: 18,
fontFamily: 'SFProDisplay',
fontWeight: FontWeight.w600,
fontStyle: FontStyle.normal,
letterSpacing: 0.45),
textAlign: TextAlign.center),
Container(
height: 215,
decoration: BoxDecoration(
color: Color(0x7feff1f5),
borderRadius: BorderRadius.circular(6),
),
padding: EdgeInsets.fromLTRB(17, 16, 17, 25),
child: new ConstrainedBox(
constraints: BoxConstraints(maxHeight: 200.0),
child: new Scrollbar(
child: new SingleChildScrollView(
scrollDirection: Axis.vertical,
reverse: true,
child: SizedBox(
height: 175.0,
child: new TextField(
controller: _text,
maxLines: 100,
decoration: InputDecoration(
hintText: "Address",
hintStyle: TextStyle(
color: Color(0x00FFa6a9af),
),
border: InputBorder.none,
errorText: _validate ? 'Notes can\'t be Empty' : null,
),
style: TextStyle(
height: 1.4,
fontSize: 16,
color: Colors.black,
fontFamily: 'regular',
letterSpacing: 0.35,
fontWeight: FontWeight.w400,
),
),
),
),
),
)),
Container(
height: 50,
color: Colors.transparent,
padding: EdgeInsets.fromLTRB(0, 0, 188, 0),
child:ElevatedButton(
child: Text('Save'),
onPressed: (){
setState(() {
_text.text.isEmpty ? _validate = true : _validate = false;
});
},
style: ElevatedButton.styleFrom(
primary: Colors.redAccent,
onPrimary: Colors.white,
textStyle: TextStyle(
fontFamily: 'regular',
fontSize: 16.0,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25.0),
),),
),
),
],
),
));
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
title: Text(
"",
style: TextStyle(color: Colors.black87, fontFamily: "regular"),
textAlign: TextAlign.left,
),
centerTitle: false,
),
body: Center(
child: TextButton(
onPressed: () {
bottomSheet();
},
child: Text("AddAddress"),
),
),
);
}
}
As I checked it is because it doesn't properly rebuild so you have to close it then open it so the state that is change would effect.
I have added a changeNotifier class to keep the state and notify its listeners using provider package you can take look at it:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MaterialApp(home: AddAddress()));
class AddAddress extends StatefulWidget {
#override
AddAddressState createState() => AddAddressState();
}
class AddAddressState extends State<AddAddress> {
#override
void dispose(){
super.dispose();
}
bottomSheet() {
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (BuildContext context) {
return ChangeNotifierProvider(create: (context) => CustomProvider(),child: CustomTextField());
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
title: Text(
"",
style: TextStyle(color: Colors.black87, fontFamily: "regular"),
textAlign: TextAlign.left,
),
centerTitle: false,
),
body: Center(
child: TextButton(
onPressed: () {
bottomSheet();
},
child: Text("AddAddress"),
),
),
);
}
}
class CustomTextField extends StatelessWidget{
#override
Widget build(BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height * 0.85,
decoration: new BoxDecoration(
color: Colors.transparent,
),
child: Container(
padding: const EdgeInsets.fromLTRB(20, 10, 20, 0),
decoration: new BoxDecoration(
color: Colors.white,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(10.0),
topRight: const Radius.circular(10.0))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text("Address",
textScaleFactor: 1.3,
style: TextStyle(
fontSize: 18,
fontFamily: 'SFProDisplay',
fontWeight: FontWeight.w600,
fontStyle: FontStyle.normal,
letterSpacing: 0.45),
textAlign: TextAlign.center),
Container(
height: 215,
decoration: BoxDecoration(
color: Color(0x7feff1f5),
borderRadius: BorderRadius.circular(6),
),
padding: EdgeInsets.fromLTRB(17, 16, 17, 25),
child: new ConstrainedBox(
constraints: BoxConstraints(maxHeight: 200.0),
child: new Scrollbar(
child: new SingleChildScrollView(
scrollDirection: Axis.vertical,
reverse: true,
child: Consumer<CustomProvider>(
builder: (context, item, child) => SizedBox(
height: 175.0,
child: new TextField(
controller: Provider.of<CustomProvider>(context, listen: false).text,
maxLines: 100,
decoration: InputDecoration(
hintText: "Address",
hintStyle: TextStyle(
color: Color(0x00FFa6a9af),
),
border: InputBorder.none,
errorText: item.validate ? 'Notes can\'t be Empty' : null,
),
style: TextStyle(
height: 1.4,
fontSize: 16,
color: Colors.black,
fontFamily: 'regular',
letterSpacing: 0.35,
fontWeight: FontWeight.w400,
),
),
),
),
),
),
)),
Container(
height: 50,
color: Colors.transparent,
padding: EdgeInsets.fromLTRB(0, 0, 188, 0),
child:ElevatedButton(
child: Text('Save'),
onPressed: (){
Provider.of<CustomProvider>(context, listen: false).checkValidation();
},
style: ElevatedButton.styleFrom(
primary: Colors.redAccent,
onPrimary: Colors.white,
textStyle: TextStyle(
fontFamily: 'regular',
fontSize: 16.0,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25.0),
),),
),
),
],
),
));
}
}
class CustomProvider extends ChangeNotifier{
final text = TextEditingController();
bool validate = false;
void checkValidation(){
if(text.text.isEmpty){
validate = true;
}else{
validate = false;
}
notifyListeners();
}
}
i am building a log in ui screen everything is going fine but as i was supposed to use more than one TextField i created a new class of TextField and the code goes as follows
import 'package:gradient_widgets/gradient_widgets.dart';
import 'package:notepad/authentication/textfield.dart';
class LogIn extends StatefulWidget {
LogIn({Key key}) : super(key: key);
#override
_LogInState createState() => _LogInState();
}
class _LogInState extends State<LogIn> {
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: Column(
crossAxisAlignment:
CrossAxisAlignment.start, //TODO: ESSE DACK NA EK BAR
children: [
Container(
padding: EdgeInsets.fromLTRB(20.0, 150.0, 20.0, 0.0),
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Welcome,',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 35.0,
),
),
Text(
'Log In to continue!',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.grey,
),
),
SizedBox(height: 70),
buildTextField(
labelText: 'Email',
),
SizedBox(height: 30),
buildTextField(
labelText: 'Password',
obscureText: true, //TODO: password ko thich krna hy!!!
),
SizedBox(height: 10),
Container(
alignment: Alignment(1.0, 0.0),
child: InkWell(
onTap: () {
//TODO: navigate to forgot password page
},
child: Text(
'Forgot Password?',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.deepOrange,
fontSize: 10.0,
),
),
),
),
SizedBox(height: 70),
Container(
child: GradientButton(
increaseHeightBy: 10.0,
increaseWidthBy: 200.0,
child: Text(
'Log In',
style: TextStyle(fontWeight: FontWeight.bold),
),
callback: () {},
//TODO: LOGIN KA CALLBACK JAYEGA YAHA PR
gradient: LinearGradient(
colors: [
Color(0xFFFD7F2C),
Color(0xFFFF6200),
Color(0xFFFD9346),
],
),
shadowColor: Gradients.backToFuture.colors.last
.withOpacity(0.25),
),
),
SizedBox(
height: 10.0,
),
Container(
child: GradientButton(
increaseHeightBy: 10.0,
increaseWidthBy: 200.0,
child: Text(
'Log In With Google',
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.black),
),
callback: () {},
//TODO: LOGIN KA CALLBACK JAYEGA YAHA PR
gradient: LinearGradient(
colors: [Colors.white, Colors.white]),
),
),
],
),
],
),
),
],
),
);
}
}
and the code of text field class
TextField buildTextField({
#required String labelText,
bool obscureText,
}) {
return TextField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25),
borderSide: BorderSide(color: Colors.white),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25),
borderSide: BorderSide(color: Colors.deepOrange),
),
labelText: labelText,
labelStyle:
TextStyle(fontWeight: FontWeight.bold, color: Colors.deepOrange),
),
keyboardType: TextInputType.emailAddress,
obscureText: obscureText,
);
}
As i am passing obscureText as true in the login page my app is crashing saying obsureText !=null enter image description here
i dont know to can i solve this as i am new to flutter i will be greatful enough to somebody helped me to solve this issue
You are getting that error because you are not passing the value for
bool obscureText,
Make sure to pass the value for obscureText from everywhere the buildTextField is called or set a default value for obscureText like this -
bool obscureText = false,
Please see the working code below :
import 'package:flutter/material.dart';
final Color darkBlue = const Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: const Scaffold(
body: Center(
child: LogIn(),
),
),
);
}
}
class LogIn extends StatefulWidget {
const LogIn({Key key}) : super(key: key);
#override
_LogInState createState() => _LogInState();
}
class _LogInState extends State<LogIn> {
TextField buildTextField({
#required String labelText,
bool obscureText = false,
}) {
return TextField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25),
borderSide: const BorderSide(color: Colors.white),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25),
borderSide: const BorderSide(color: Colors.deepOrange),
),
labelText: labelText,
labelStyle: const TextStyle(
fontWeight: FontWeight.bold, color: Colors.deepOrange),
),
keyboardType: TextInputType.emailAddress,
obscureText: obscureText,
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.fromLTRB(20.0, 150.0, 20.0, 0.0),
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Welcome,',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 35.0,
),
),
const Text(
'Log In to continue!',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.grey,
),
),
const SizedBox(height: 70),
buildTextField(
labelText: 'Email',
),
const SizedBox(height: 30),
buildTextField(
labelText: 'Password',
obscureText: true,
),
const SizedBox(height: 10),
Container(
alignment: const Alignment(1.0, 0.0),
child: InkWell(
onTap: () {},
child: const Text(
'Forgot Password?',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.deepOrange,
fontSize: 10.0,
),
),
),
),
const SizedBox(height: 70),
Container(
child: RaisedButton(
child: const Text(
'Log In',
style: TextStyle(fontWeight: FontWeight.bold),
),
onPressed: () {},
),
),
const SizedBox(
height: 10.0,
),
Container(
child: RaisedButton(
child: const Text(
'Log In With Google',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black),
),
onPressed: () {},
),
),
],
),
],
),
),
],
),
),
);
}
}
you should provide default value for obscureText (bool obscureText=false) in your buildTextField widget.