简体   繁体   English

使用Web视图链接创建Flutter底部导航栏

[英]Create Flutter bottom navigation bar with web view links

I want to create a bottom navigation bar that it I want each item to display different web view. 我想创建一个底部导航栏,它希望每个项目显示不同的Web视图。

My webview package is webview_flutter.dart 我的webview包是webview_flutter.dart

This is my main file 这是我的主文件

import 'package:flutter/material.dart';
import 'home_widget.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Flutter App',
      home:  Home(),
    );
  }
}

This is my home_widget file 这是我的home_widget文件

import 'package:flutter/material.dart';
import 'placeholder_widget.dart';

class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomeState();
  }
}

class _HomeState extends State<Home> {
  @override
  int _currentIndex = 0;
  final List<Widget> _children = [
    PlaceholderWidget(Colors.white),
    PlaceholderWidget(Colors.deepOrange),
    PlaceholderWidget(Colors.green)
  ];

  Widget build(BuildContext context) {
    return Scaffold(
      body: _children[_currentIndex], // new
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTabTapped, // new
        currentIndex: _currentIndex, // new
        items: [
          BottomNavigationBarItem(
              icon: new Icon(Icons.shopping_cart),
              title: new Text(
                'جدیدترین محصولات',
                style: TextStyle(
                  fontFamily: 'iransans',
                ),
              )),
          BottomNavigationBarItem(
              icon: new Icon(Icons.format_list_bulleted),
              title: new Text(
                'دسته بندی محصولات',
                style: TextStyle(
                  fontFamily: 'iransans',
                ),
              )),
          BottomNavigationBarItem(
              icon: Icon(Icons.person),
              title: new Text(
                'حساب کاربری',
                style: TextStyle(
                  fontFamily: 'iransans',
                ),
              ))
        ],
      ),
    );
  }

  void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }
}

and this is my placeholder_widget file 这是我的placeholder_widget文件

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class PlaceholderWidget extends StatelessWidget {
  final Color color;

  PlaceholderWidget(this.color);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: color,
    );
  }
}

Now I want load different webview url in each tap. 现在,我想在每个点击中加载不同的webview网址。 This code is currently changing the background color but I would like to be able to display a specific webpage at any time. 该代码当前正在更改背景颜色,但我希望能够随时显示特定的网页。 If you want to know how my webview_flutter works, use this code 如果您想知道我的webview_flutter如何工作,请使用此代码

import 'package:webview_flutter/webview_flutter.dart';

  return Scaffold(
  appBar: AppBar(
    title: const Text('Flutter WebView example'),
  ),
  body: const WebView(
    initialUrl: 'https://flutter.io',
    javascriptMode: JavascriptMode.unrestricted,
  ),
);

You should put your webview in placeholder and use variable for url. 您应该将Webview放在占位符中,并使用url作为变量。

class PlaceholderWidget  extends StatelessWidget {
var url;

PlaceholderWidget(this.url);

@override
Widget build(BuildContext context) {
return Container(
        child: WebView(
          initialUrl: url,
          javascriptMode: JavascriptMode.unrestricted,
        ),
  );
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM