View screen shot here
I need to build this kind of widget to my flutter application.The widget should be can bring across the screen. is its possible? Then How can I do that?
Thanks in advance!
Actually, Flutter can't create this type of widget (widget that you can add on your Home Screen) because Flutter uses his own custom rendering engine.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(“First Widgets")),
body: myWidget(),
),
);
}
}
Widget myWidget() {
return Text(
"Hello, World!",
);
}
}
Related
I want to display the app's icon in my flutter app
I have an app with many icons since I use flutter_launcher_icons
research with google and stackoverflow
I hope you already have set launcher icon if yes then now you can use Use https://pub.dev/packages/application_icon
import 'package:application_icon/application_icon.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
// use AppIcon to show your application icon
child: AppIconImage(),
),
),
);
}
}
Or directly access launcher icon from asset folder
I am new to flutter, BUILT an app showing graphs in flutter. When i run the code a red screen is appearing showing the above error
No MediaQuery widget ancestor found. Scaffold widgets require a MediaQuery widget ancestor.
I have cleared all the errors except this one.
Full link of the code
Check you main.dart, and in the MyApp (Or whatever you named it) class, you should wrap it with MaterialApp. It should fix the problem.
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Graphn',
home: GraphScreen(),
);
}
}
Wrap your scaffold to MatertialApp or you can make a class and in that class call GraphScreen like this
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: GraphScreen(),
);
}
}
I am relatively new to flutter so I don't know why my appbar appears like this
Here is the main.dart file
import 'package:testapp/test_page.dart';
import 'package:flutter/material.dart';
void main() {
runApp(Home());
}
class Home extends
StatelessWidget {
#override
Widget build(BuildContext
context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Test App',
theme: ThemeData(primaryColor: Colors.purple),
home: TestPage(),
);
}
}
Here is my testpage.dart
import 'package:flutter/material.dart';
class TestPage extends
StatelessWidget {
#override
Widget build(BuildContext
context) {
return Scaffold(
primary: false,
appBar: AppBar(
title: Text(' Test app'),
),
body: Center(child: Text('Example Test')),
);
}
}
I tried using SafeArea it didn't work. Thank you
here is what i solved similar issue.
Simply wrap your Scaffold widget with SafeArea Widget
SafeArea(
child: Scaffold()
)
You can find more detail about SafeArea Widget in this link
Flutter SafeArea Widget
Hope it will help
Happy coding
You have primary property set to false.
Whether this scaffold is being displayed at the top of the screen.
If true then the height of the appBar will be extended by the height
of the screen's status bar, i.e. the top padding for MediaQuery.
https://api.flutter.dev/flutter/material/Scaffold/primary.html
change the primary value to true and tell me result:
Scaffold(
primary: true, // this line
appBar: AppBar(......
Edit:
MaterialApp cant be a child of statelessWidget. use it as the main Widget
runApp(MaterialApp(
......
))
Thank you much guys, I created a new project then I realised it was caused by a bug from one of the libraries I installed. It's totally fixed now ♥️
In my example app below, I have two routes: HomeRoute and OtherRoute. I want OtherRoute to always return a result when it is popped from the navigator. In this example, it does this when the RaisedButton in OtherRoute is pressed. However, I also want it to return a result when the back button is pressed. I know I can override the back button on the AppBar, but I also want to override the behavior of the "system" back button on Android.
I know I can use WillPopScope to prevent the system back button from popping the current route, but I do not want this. If possible, I want the system back button to still be able to pop the current route, just with a result included. Is this possible?
Here is my sample app:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test',
home: Scaffold(
appBar: AppBar(title: Text("Home"),),
body: HomeRoute(),
),
);
}
}
class HomeRoute extends StatelessWidget{
#override
Widget build(BuildContext context) {
return RaisedButton(onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context){
return OtherRoute();
})).then((result){
// I want result here to never be null
Scaffold.of(context).showSnackBar(SnackBar(content: Text("$result")));
});
});
}
}
class OtherRoute extends StatelessWidget{
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Other route"),),
body: RaisedButton(onPressed: (){
Navigator.pop(context, "Result!");
})
);
}
}
Wrap your scaffold with WillPopScope in OtherRoute and override onWillPop method as follows:
#override
Widget build(BuildContext context) {
return WillPopScope(
//....
),
onWillPop: () async {
Navigator.pop(context, "Result!");
return false;
},
);
}
I am using ScopedModel and ScopedModelDescendent in my flutter app. The MaterialApp is wrapper within a ScopedModelDescendent widget as shown in the code below.
void main() => runApp(new ScopeModelWrapper());
class ScopeModelWrapper extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ScopedModel<AppModel>(model: AppModel(), child: MyApp());
}
}
class AppModel extends Model {
// Some code specific to model, actually a lot of code.
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ScopedModelDescendant<AppModel>(builder: (context, child, model) {
return MaterialApp(
title: 'Flutter Firebase',
localizationsDelegates: [
AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
Locale("nb", ""),
Locale("en", ""),
],
locale: model.appLocal,
navigatorObservers: <NavigatorObserver>[routeObserver],
theme: new ThemeData(
primarySwatch: Colors.blue,
primaryColor: Colors.blueAccent,
),
home: SelectDenominationPage(),
);
});
}
}
The issue occurs when I navigate to HomePage->Settings (from the SelectDenominationPage). The Settings page (or widget) contains some widgets wrapped inside ScopedModelDescendent widget and that is what causes the error.
I do not understand why it cannot find a ScopedModel because it is right there at the root of the tree.
Any help is highly appreciated.
Update: I moved the AppModel class to its own file and then I changed all my imports from 'appmodel.dart' to 'package:<projectname>/appmodel.dart' and it started working. I still do not understand this completely. If we are referencing to the same file, why is it being treated at a different model then?