White Space in Drawer - Flutter - flutter

I'm using drawer widget...
But there's a white space in the container to the right side...
And here's my code...
home.dart
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Dashboard'),
elevation: 0,
backgroundColor: const Color.fromARGB(255, 10, 11, 102),
),
drawer: const CustomDrawer(),
body: Center(
child: Text('Home'),
),
);
}
}
custom_drawer.dart
class CustomDrawer extends StatelessWidget {
const CustomDrawer({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
Widget _drawerItems({String? text, IconData? icon, String? route}) {
return ListTile(
leading: icon == null ? null : Icon(icon),
title: Text(text!),
onTap: () {
Navigator.of(context)
.pushNamedAndRemoveUntil(route!, (route) => false);
},
);
}
return Drawer(
child: Column(
children: [
// Drawer Header
Container(
height: 150,
color: const Color.fromARGB(255, 10, 11, 102),
child: const Center(
child: Text(
'Welcome',
style: TextStyle(
color: Colors.white,
),
),
),
),
Expanded(
child: ListView(
padding: EdgeInsets.zero,
children: [
// Home
_drawerItems(
text: 'Home',
icon: Icons.home_outlined,
route: homeScreenRoute,
),
ExpansionTile(
title: const Text('Products'),
leading: const Icon(Icons.production_quantity_limits),
children: [
// All Products
_drawerItems(
text: 'All Products',
route: productScreenRoute,
),
// Add Products
_drawerItems(
text: 'Add Products',
route: addProductScreenRoute,
),
],
),
],
),
),
],
),
);
}
}
How can I fix this?
It also sometimes blinking when I am dragging it to left or right...
I'm using my phone (Redmi Note 7) to debug.
So, is this a problem with that?
Or am I using drawer widget in the wrong way?

I've run your code and it works like a charm.
It works well on my phone too ( Redmi K30 4G ).
Here's what it looks like on my phone.
Also, your code is correct and I see nothing wrong with it. May be your phone has a problem.
Here's your output on emulator.
Can you release your app and test it on Redmi Note 7 to see if it's working?
For release mode, run flutter build apk and copy apk from $project_directory\build\app\outputs\app-release.apk.

Although it's working fine for me too, Just try adding this, maybe it can fix it!
Container(
width: double.maxFinite, // this line
height: 150,
color: const Color.fromARGB(255, 10, 11, 102),
child: const Center(
child: Text(
'Welcome',
style: TextStyle(
color: Colors.white,
),
),
),
),

Related

Multiple ExpansionTile with multiple ListTile, how to do a second one

This question might sound dumb, but I am trying to make a list of Expandable list items which in turn have a list of items inside. I have come this far but I am unable to make it a show a second one WITHOUT USING INDEX.
import 'package:flutter/material.dart';
class ExerciseScreen extends StatefulWidget {
const ExerciseScreen({Key? key}) : super(key: key);
#override
State<ExerciseScreen> createState() => _ExerciseScreenState();
}
class _ExerciseScreenState extends State<ExerciseScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Table of Contents CEFR'),
),
body: ListView(
children: const [
ExpansionTile(
leading: Padding(
padding: EdgeInsets.all(8.0),
child: CircleAvatar(
backgroundColor: Color.fromARGB(255, 0, 182, 91),
child: Text(
'A1',
style: TextStyle(fontSize: 15, color: Colors.white),
),
),
),
title: Text(
'To be: is, am, are',
style: TextStyle(fontSize: 21, color: Colors.black),
),
children: [
ListTile(
leading: CircleAvatar(
child: Text('A1'),
),
title: Text('Affirmative ideas'),
subtitle: Text('Singular and plural subjects'),
),
ListTile(
leading: CircleAvatar(
child: Text('A1'),
),
title: Text('Are: plural verb'),
subtitle: Text('Positive ideas using plural subjects'),
),
ListTile(
leading: CircleAvatar(
child: Text('A1'),
),
title: Text('Is : singular verb'),
subtitle: Text('Positive ideas using singular subjects'),
),
ListTile(
leading: CircleAvatar(
child: Text('A1'),
),
title: Text('Questions'),
subtitle: Text('Interrogative ideas - subject/verb/switch '),
),
ListTile(
leading: CircleAvatar(
child: Text('A1'),
),
title: Text('Questions'),
subtitle: Text('Closed questions (yes/no)'),
),
],
),
],
),
);
}
}
What is the problem with just adding a second ExpansionTile to your list? Like so:
import 'package:flutter/material.dart';
class ExerciseScreen extends StatefulWidget {
const ExerciseScreen({Key? key}) : super(key: key);
#override
State<ExerciseScreen> createState() => _ExerciseScreenState();
}
class _ExerciseScreenState extends State<ExerciseScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Table of Contents CEFR'),
),
body: ListView(
children: const [
ExpansionTile(...),
ExpansionTile(...),
],
),
);
}
}
On my side it shows both ExpansionTiles correctly:

Flutter Collapsible Appbar with custom layout and animation

I am trying to create an app bar that looks like the following image when expanded
When collapsing I only need the initial column of text to be available in the app bar, so the image should fade away and the text column to animate and become the Appbar title
I have tried using SliverAppBar with a custom scroll view but the Flexible space's title is not looking good also I don't know how to make the image disappear alone and show only the text when collapsed.
As of now, this is the expanded state
And when I try to collapse it, this happens
Here's the code for the same
CustomScrollView(
slivers: [
SliverAppBar(
pinned: true,
leading: GestureDetector(
onTap: () => Get.back(),
child: const Icon(
Icons.arrow_back,
color: Colors.black,
),
),
flexibleSpace: FlexibleSpaceBar(
title: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
const Expanded(
child: Text.rich(
TextSpan(
text: "Buffet Deal\n",
style:
TextStyle(color: Colors.black, fontSize: 14),
children: [
TextSpan(
text: "Flash Deal",
style: TextStyle(
color: Colors.black, fontSize: 10),
),
]),
),
),
if (_model.imgUrl != null)
CachedNetworkImage(
imageUrl: _model.imgUrl!,
),
const SizedBox(
width: 18,
)
],
),
),
),
expandedHeight: Get.height * 0.2,
backgroundColor: const Color(0xFFFFF4F4)),
SliverToBoxAdapter(
child: Container()
)
],
),
Your help is appreciated. Thanks in advance
To make the SliverAppBar's flexible space image disappear whenever the expanded space collapse just use the FlexibleSpaceBar.background property.
To solve the overlapping text just wrap the FlexibleSpaceBar around a LayoutBuilder to get if it's expanded or not and adjust the text positioning.
Check out the result below (Also, the live demo on DartPad):
import 'dart:math';
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 const MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
final expandedHeight = MediaQuery.of(context).size.height * 0.2;
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
pinned: true,
leading: GestureDetector(
onTap: () => {},
child: const Icon(
Icons.arrow_back,
color: Colors.black,
),
),
flexibleSpace: LayoutBuilder(builder: (context, constraints) {
final fraction =
max(0, constraints.biggest.height - kToolbarHeight) /
(expandedHeight - kToolbarHeight);
return FlexibleSpaceBar(
title: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
Expanded(
child: Padding(
padding: EdgeInsets.only(left: 24 * (1 - fraction)),
child: const Text.rich(
TextSpan(
text: "Buffet Deal\n",
style: TextStyle(
color: Colors.black, fontSize: 14),
children: [
TextSpan(
text: "Flash Deal",
style: TextStyle(
color: Colors.black, fontSize: 10),
),
]),
),
),
),
const SizedBox(width: 18)
],
),
),
background: Align(
alignment: Alignment.centerRight,
child: Image.network(
"https://source.unsplash.com/random/400x225?sig=1&licors",
),
),
);
}),
expandedHeight: MediaQuery.of(context).size.height * 0.2,
backgroundColor: const Color(0xFFFFF4F4),
),
const SliverToBoxAdapter(child: SizedBox(height: 1000))
],
),
);
}
}

Custom widget of app bar with a humburger menu as ElevatedButton with a Drawer doesn't work OnPressed

I am facing a problem with my custom app bar. I would like it to have a drawer but it doesn't work. I just don't know how to make it being able to open.
It is a custom app bar widget named Pasek (which stands for top bar in my language).
It has a logo picture which on click moves you to the first page of the app.
Next to it there is humburger menu with a drawer but I do not know why it doesn't work. I click on it and nothing happens.
Any ideas?
class Pasek extends StatelessWidget with PreferredSizeWidget {
#override
Size get preferredSize => Size.fromHeight(50.0);
const Pasek({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return AppBar(
actions: <Widget>[
Padding(
padding: EdgeInsets.only(right: 35.0),
child: ElevatedButton(
child: Icon(Icons.menu, color: Colors.white),
onPressed: () {
Scaffold(
endDrawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: const <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: Colors.black,
),
child: Text(
'Menu',
style: TextStyle(
color: Colors.pink,
fontSize: 24,
),
),
),
ListTile(
leading: Icon(Icons.message),
title: Text('Messages'),
),
ListTile(
leading: Icon(Icons.account_circle),
title: Text('Profile'),
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
),
],
),
),
);
},
)),
],
backgroundColor: Colors.black,
title: GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => FirstPage(),
),
);
}, // Image tapped
child: Image.asset(
'images/logo.png',
fit: BoxFit.scaleDown,
height: 30,
width: 200,
),
));
}
}

Evade flutter overflow when using row

[this is on desktop,not web or android]
I have following code, which creates a split screen (one side of screen has shortcuts to content and other side shows that content) it also acts somewhat as drawer since it can be closed or opened.
the left pane i reduce in one of two cases, when user clicks arrow button, or when the window side is less then 700. but if i try to take right edge of the screen and move very fast to reduce the size of whole window, the row will overflow and i will get an error.
So far i tryed using several things to achive this layout :
Using Flexible and setting fit and flex properties, (same issue as now since parent of them was again a row)
way that i am showing you now.
Also i could try to limit the size of screen lets say min value is 640 x 480 and that would also somewhat prevent error from happening.
I am just wondering is there another way to achieve this layout .
I also tryed several packages from pub.dev and ALL of them have same overflow error in their implementation .(easy_sidemenu,side_menu_navigation, etc)
my code
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
void main() {
runApp(const ProviderScope(child: MyApp()));
}
class MyApp extends ConsumerWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context, WidgetRef ref) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: MyHomePage(
title: "My home",
),
);
}
}
class MyHomePage extends ConsumerStatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
ConsumerState<MyHomePage> createState() => _MyHomePageState();
}
final List<bool> isSelected = [false, false, false];
class _MyHomePageState extends ConsumerState<MyHomePage> {
#override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(
elevation: 0,
toolbarHeight: 35,
centerTitle: true,
title: Text(widget.title),
),
body: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
AnimatedContainer(
duration: const Duration(milliseconds: 500),
width: width > 700 ? 250 : 50,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: ListView(
padding: const EdgeInsets.symmetric(vertical: 5),
children: [
TextButton(
onPressed: () {},
child: Row(
children: [
Icon(
Icons.home,
color: Theme.of(context).colorScheme.secondary,
size: 30,
),
const Flexible(
child: Padding(
padding: EdgeInsets.all(10.0),
child: Text(
"Home",
maxLines: 1,
style:
TextStyle(overflow: TextOverflow.clip),
),
),
),
],
),
),
]),
),
const SizedBox(
height: 5,
),
TextButton(
onPressed: () {},
child: Row(
children: const [
Icon(
Icons.settings,
size: 30,
),
Flexible(
child: Padding(
padding: EdgeInsets.all(10.0),
child: Text(
"Settings",
maxLines: 1,
style: TextStyle(overflow: TextOverflow.clip),
),
),
),
],
),
),
],
),
),
IconButton(onPressed: () {}, icon: const Icon(Icons.arrow_back)),
Expanded(
child: Container(
color: Colors.red,
)),
],
),
);
}
}

What do I need to do to link to another file with my button widgets?

I am still learning to code. How do I store all my button widgets in another file and import it into my main file? Here is the code:
class _testState extends State<test> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[900],
appBar: AppBar(
title: Text('test'),
backgroundColor: Colors.red[900],
),
body: GridView(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
),
children: [
//buttons
],
),
);
}
}
How can I take the code below and put it in another file and then link it to the file above by the buttons comment?
Padding(
padding: const EdgeInsets.fromLTRB(0.0, 9.0, 0.0, 0.0),
child: TextButton.icon(
onPressed: () => {},
icon: Column(
children: [
Icon(
Icons.add,
color: Colors.white,
size: 75,
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Text(
'Label',
style: TextStyle(
color: Colors.white,
),
),
),
],
),
label: Text(
'', //'Label',
style: TextStyle(
color: Colors.white,
),
),
),
),
-First, create a new file, name it jeff.dart.
-Second, type this in the new file: stless . Press Enter, Flutter will automatically make a new StatelessWidget for you. Change the class name to JeffButton. Now, it look like this :
class JeffButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
);
}
}
Copy and paste the button's code , replace the Container() .Congratulations! You had the JeffButton! Now you can use it everywhere:
class JeffButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 9.0, 0.0, 0.0),
child: TextButton.icon(
onPressed: () => {},
icon: Column(
children: [
Icon(
Icons.add,
color: Colors.white,
size: 75,
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Text(
'Label',
style: TextStyle(
color: Colors.white,
),
),
),
],
),
label: Text(
'', //'Label',
style: TextStyle(
color: Colors.white,
),
),
),
);
}
}
Now just add it to where you need it ;) :
children: [
//buttons
JeffButton(),
],
Remember to import the jeff.dart file to where you use it, you can do it easily by move the cursor to JeffButton(), show quick fixes, choose Import: .... (which is the first options). This is how to show quick fixes if you didn't know: https://flutter.dev/docs/development/tools/flutter-fix