Flutter does not recognize valid imports - dart

I have this simple Flutter app.
import 'package:flutter/material.dart';
void main() => runApp(App());
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Sample'),
),
body: Center(
child: Column (
children: <Widget>[
CustomCard(mainTitle: "A simple widget!"),
]
)
),
),
);
}
}
class CustomCard extends StatelessWidget {
final String mainTitle;
CustomCard({#required this.mainTitle});
#override
Widget build(BuildContext context) {
return Container(
height: 48,
width: 200,
child: Text('$mainTitle'),
);
}
}
If I move the CustomCard class into another file, e.g. custom_card.dart, and then do import 'package:myproject/custom_card.dart' I get the error message: "The parameter mainTitle is not defined". If the class CustomCard is left within main.dart everything works.
What am I missing?
Here is my setup.
Flutter 1.2.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 8661d8aecd (3 weeks ago) • 2019-02-14 19:19:53 -0800
Engine • revision 3757390fa4
Tools • Dart 2.1.2 (build 2.1.2-dev.0.0 0a7dcf17eb) •
Windows 10 64x

package:myproject/custom_card.dart is a reference to lib/custom_card.dart under the package root. You also need to ensure that the myproject portion matches the package name in the pubspec.yaml file. See https://www.dartlang.org/tools/pub/package-layout#public-libraries

Related

How do i ask a good method in my case(toast messeage)

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Snack Bar',
theme: ThemeData(primaryColor: Colors.red),
home: MyPage(),
);
}
}
class MyPage extends StatelessWidget {
const MyPage({super.key});
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Toast message'),
centerTitle: true,
),
body: Center(
child: TextButton(
style: TextButton.styleFrom(backgroundColor: Colors.blue),
onPressed: () {
Fluttertoast.showToast(
msg: 'Flutter',
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.black,
textColor: Colors.white,
fontSize: 16.0);
},
child: Text(
'Toast',
style: TextStyle(fontSize: 24),
)),
));
}
}
I have added fluttertoast: ^8.1.2 in yaml
but, If I debug this, the emulator doesn't run.
The reason is that the Cocoapods version is not up to date.
But my Cocoapds is 1.11.3 which is the latest version.
One more thing I don't understand is that the emulator works well when i debug it with code for snackbar, not toast messages.
If I clear fluttertoast: ^8.1.2 from yaml, the emulator runs, but adding it causes an error message for the cocoapods version, so only the toast message is a problem.
How can i solve this problem?
If you are facing issue with latest version, try previous version i.e. fluttertoast: ^8.1.1 or fluttertoast: ^8.1.0 . which may resolve your issue.
As I found this kind or error in repo.

I wanted to create random English words with each letter uppercased, but I failed although I followed exact same code as instruction shows

In Android Studio, I imported a package, and then I added english_words: ^4.0.0
which came out a message:"C:\Users\Miche\Documents\flutter\bin\flutter.bat --no-color pub get
Running "flutter pub get" in startup_name... 521ms
Process finished with exit code 0".
In the main dart:
import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final wordPair = WordPair.random();
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: const Text('Welcome to Flutter'),
),
body: Center(
child: Text('wordPair.asPascalCase'),
),
),
);
}
}
But the emulator came up like wordPair.asPascalCase exactly as child text.
Where am I wrong? Thank you! Please help me :)

Flutter: Dark Theme on iOS

I am creating an app using Flutter.
On iOS however (you can also test it on Android), dark theme is not applied.
Using Android widgets, it is working fine tho.
How can I make the Cupertino widgets using the dark theme? Especially for the popups.
I am using Flutter 1.9.1+hotfix6
E.g. the Cupertino "ActionSheet":
import 'package:flutter/material.dart';
import 'home.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: 'Flutter Demo',
theme: ThemeData.dark(),
darkTheme: ThemeData.dark(),
home: Home(),
);
}
}
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Center(
child: RaisedButton(
child: Text('test'),
onPressed: () {
Widget secondaryButton, confirmButton, popup;
secondaryButton = CupertinoActionSheetAction(
child: Text('secundary'),
onPressed: () {},
);
confirmButton = CupertinoActionSheetAction(
child: Text('test'),
onPressed: () {},
);
popup = CupertinoActionSheet(
title: Text('Title'),
message: Text('Content'),
cancelButton: secondaryButton,
actions: [confirmButton],
);
showCupertinoModalPopup(
context: context, builder: (context) => popup);
},
),
);
}
}
Screenshot:
Check this repo, you can create platform specific layouts only using a single widget that does all the platform specific boilerplate for you.
Also have support for dark mode, at least in iOS.

How to integrate/display facebook videos to flutter

I have this code below, I like to display the videos from facebook to flutter app.
But instead it only display a link to the video, when I click the link it brings me to facebook video. I like to display only the video and not the link, please help.
import 'package:flutter/material.dart';
import 'package:flutter_html_view/flutter_html_view.dart';
//import 'package:html2md/html2md.dart';
//import 'package:flutter_markdown/flutter_markdown.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
String html = '<div class="fb-video" data-href="https://www.facebook.com/FacebookAU/videos/10152167660372415/" data-width="500" data-show-text="true"><blockquote cite="https://www.facebook.com/FacebookAU/videos/10152167660372415/" class="fb-xfbml-parse-ignore">Happy New You<p>Here’s to a healthier, dancier, artier, funner New You in 2014.</p>Posted by Facebook on Wednesday, January 8, 2014</blockquote></div>';
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Plugin example app'),
),
body: new SingleChildScrollView(
child: new Center(
child: new HtmlView(data: html),
),
),
),
);
}
}
Displaying Link
Video is shown
you can try this version
https://pub.dev/packages/flutter_widget_from_html
build HTML content like below:
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
String html = '''
<iframe width="200" height='200'
src="https://www.facebook.com/v2.3/plugins/video.php?
allowfullscreen=false&autoplay=true&href=${FB_VIDEO_URL}" </iframe>
''';
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Plugin example app'),
),
body: new SingleChildScrollView(
child: HtmlWidget(
html,
webView: true,
),
),
),
);
}
}
replace FB_VIDEO_URL by your link
it worked for me
both facebook and youtube (youtube: repace attribute src in tag) video.
hope this helps.

How to load images with image.file

I can't seem to simply load an image from the hard drive to the screen. Image.network seems straightforward. But I can't figure out how to use Image or Image.file. Image seems to require a stream, so I don't think that is what I am looking for.
import 'package:flutter/material.dart';
import 'dart:io';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
File file = new File("Someimage.jpeg");
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Image.file(file), //doesn't work, but no errors
);
}
}
I added Someimage to the pubspec.yaml file, but that doesn't work either:
assets:
- Someimage.jpeg
Can someone give me an example of how this is done? Thanks.
The difference and relation between Image, ImageProvider:
Image:
Creates a widget that displays an image.
To show an image from the network or an asset bundle, consider using [new Image.network] and [new Image.asset] respectively.
So Image is a widget. Like <img> tag in html.
ImageProvider:
Identifies an image without committing to the precise final asset. This allows a set of images to be identified and for the precise image to later be resolved based on the environment, e.g. the device pixel ratio.
So ImageProvider is like the src attribute for an Image.
Now Image takes an argument image which is an ImageProvider.
There are 4 ways of getting the ImageProvider
AssetImage:
Use to load a pre-defined set of images that are packed along with the apk.
e.g. To display Banner Images, some custom icons.
NetworkImage:
Used to load dynamic images from the internet.
FileImage:
Used to load images from the file system in the target device.
e.g. To display a newly downloaded image.
MemoryImage:
Used to load raw image from memory.
e.g. To get the user's wallpaper and load it into a widget.
Now they are all ImageProviders.
Anyone of them can be used as the image attribute to the Image widget.
And the flutter community simplified the syntax by adding extended classes to the Image widget itself.
So
Image.asset(name) is essentially Image(image: AssetImage(name)),
Image.file(path) is essentially Image(image: FileImage(File(path))),
Image.network(url) is essentially Image(image: NetworkImage(url)),
Image.memory(list) is essentially Image(image: MemoryImage(list))
Now it should be clear that
The apk size increases with the number of asset images.
The loading time (what the user sees) for them would be generally in the order
NetworkImage > FileImage > AssetImage > MemoryImage
Here is an example of the use of Image.file. This would not be the recommended way, but the use case could be if you downloaded the image in your app via http and then wanted to display the image (e.g. the image is not stored in assets during install).
In your pubspec.yaml, add :
path_provider: ^0.2.2
Code :
import 'dart:io';
import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
Future<File> _getLocalFile(String filename) async {
String dir = (await getApplicationDocumentsDirectory()).path;
File f = new File('$dir/$filename');
return f;
}
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new FutureBuilder(
future: _getLocalFile("flutter_assets/image.png"),
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
return snapshot.data != null ? new Image.file(snapshot.data) : new Container();
})
);
}
}
To simulate downloading the image you can push the image using adb :
adb push image.png /data/data/com.example.imagetest/app_flutter/flutter_assets/
Here is another example which uses a jpg as a background image. It also applies opacity to the image.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new Scaffold(
resizeToAvoidBottomPadding: false,
appBar: new AppBar(
title: new Text("test"),
),
body: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.6), BlendMode.dstATop),
image: new AssetImage("assets/images/keyboard.jpg"),
fit: BoxFit.cover,
),
),
),
),
);
}
}
child: Image.file(
File('DirectoryLocation/imageName.jpg'),
height: 45.0,
width: 45.0,
),
Try this :
import 'package:flutter/material.dart';
import 'dart:io';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new ImageIcon(
new AssetImage(
"assets/images/image.png"),
size: 24.0,
color: Colors.white),
);
}
}
In pubspec.yaml, you need :
assets:
- assets/images/image.png
Replace
new Image.file(file)
with
FileImage(file)
and that should work for you.
You can add Image.File as Container's child
Container(
padding:
EdgeInsets.zero,
height: 150,
width: 150,
child: Image.file(File(filePath))
)
if your image in the assets you can use the image.asset constructor
Flutter contains assert section inside pubspec.yaml where it contains asserts of your application.
eg:
assets:
- data_repo/img/ic_launcher.png
1. With pubspec.yaml:
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(home: LoadLocalImage()));
}
class LoadLocalImage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Load Local Image"),
),
body: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage('data_repo/img/ic_launcher.png'), fit: BoxFit.cover)),
),
);
}
}
2. With Image.asset:
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(home: LoadLocalImage()));
}
class LoadLocalImage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Load Local Image"),
),
body: Image.asset(
'data_repo/img/ic_launcher.png',
fit: BoxFit.cover,
));
}
}
Folder Structure:
Output:
Please refer below link for more description:
https://flutter.dev/docs/cookbook/images/network-image
use this package first:
import 'package:http/http.dart' show get;
import 'dart:io';
Image loadImageFromFile(String path) {
File file = new File(path);
Image img = Image.file(file);
}
void storeImageToFile(String path,String url) async {
var response = await get(Url);
File file = new File(path);
file.create(recursive: true).then((val) async {
if (await val.exists()) {
await file.writeAsBytesSync(response.bodyBytes);
}
});
}
The assets in the pubspec.yaml must be uncommented.
This will help to load the file image.asset as expected.
In case you are uploading an image using image_picker or something and want to render it from the FileImage(PATH) you get. You might need to use Image(image: FileImage(File(path))) format instead of Image.file(path). I was getting the following error when using Image.file(path):
The argument type 'ImageProvider<Object>' can't be assigned to the parameter type 'File'.

Resources